Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Platform
Commits
2cce4a47
Unverified
Commit
2cce4a47
authored
Jul 11, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding event before a file is updated
parent
7876e92b
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
110 additions
and
1 deletion
+110
-1
FileIsUpdating.php
Modules/Media/Events/FileIsUpdating.php
+62
-0
EloquentFileRepository.php
...es/Media/Repositories/Eloquent/EloquentFileRepository.php
+3
-1
EloquentFileRepositoryTest.php
Modules/Media/Tests/EloquentFileRepositoryTest.php
+45
-0
No files found.
Modules/Media/Events/FileIsUpdating.php
0 → 100644
View file @
2cce4a47
<?php
namespace
Modules\Media\Events
;
use
Modules\Core\Contracts\EntityIsChanging
;
use
Modules\Media\Entities\File
;
final
class
FileIsUpdating
implements
EntityIsChanging
{
/**
* @var File
*/
private
$file
;
/**
* @var array
*/
private
$attributes
;
/**
* @var array
*/
private
$original
;
public
function
__construct
(
File
$file
,
array
$data
)
{
$this
->
file
=
$file
;
$this
->
attributes
=
$data
;
$this
->
original
=
$data
;
}
/**
* @return array
*/
public
function
getOriginal
()
{
return
$this
->
original
;
}
/**
* @return array
*/
public
function
getAttributes
()
{
return
$this
->
attributes
;
}
/**
* @param array $attributes
*/
public
function
setAttributes
(
array
$attributes
)
{
$this
->
attributes
+=
$attributes
;
}
/**
* @return File
*/
public
function
getFile
()
{
return
$this
->
file
;
}
}
Modules/Media/Repositories/Eloquent/EloquentFileRepository.php
View file @
2cce4a47
...
...
@@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Collection;
use
Modules\Core\Repositories\Eloquent\EloquentBaseRepository
;
use
Modules\Media\Entities\File
;
use
Modules\Media\Events\FileIsCreating
;
use
Modules\Media\Events\FileIsUpdating
;
use
Modules\Media\Events\FileWasCreated
;
use
Modules\Media\Events\FileWasUpdated
;
use
Modules\Media\Helpers\FileHelper
;
...
...
@@ -22,7 +23,8 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
*/
public
function
update
(
$file
,
$data
)
{
$file
->
update
(
$data
);
event
(
$event
=
new
FileIsUpdating
(
$file
,
$data
));
$file
->
update
(
$event
->
getAttributes
());
$file
->
setTags
(
array_get
(
$data
,
'tags'
,
[]));
...
...
Modules/Media/Tests/EloquentFileRepositoryTest.php
View file @
2cce4a47
...
...
@@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Event;
use
Mockery
;
use
Modules\Media\Entities\File
;
use
Modules\Media\Events\FileIsCreating
;
use
Modules\Media\Events\FileIsUpdating
;
use
Modules\Media\Events\FileWasCreated
;
use
Modules\Media\Events\FileWasUpdated
;
use
Modules\Media\Repositories\FileRepository
;
...
...
@@ -166,6 +167,50 @@ class EloquentFileRepositoryTest extends MediaTestCase
});
}
/** @test */
public
function
it_triggers_event_when_file_is_updating
()
{
Event
::
fake
();
$file
=
$this
->
file
->
createFromFile
(
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'myfile.jpg'
));
$this
->
file
->
update
(
$file
,
[
'en'
=>
[
'description'
=>
'My cool file!'
,
'alt_attribute'
=>
'My cool file!'
,
'keywords'
=>
'My cool file!'
,
]
]);
Event
::
assertDispatched
(
FileIsUpdating
::
class
,
function
(
$e
)
use
(
$file
)
{
return
$e
->
getFile
()
->
id
===
$file
->
id
&&
$e
->
getAttributes
()[
'en'
][
'description'
]
===
'My cool file!'
;
});
}
/** @test */
public
function
it_can_change_properties_before_update
()
{
Event
::
listen
(
FileIsUpdating
::
class
,
function
(
FileIsUpdating
$event
)
{
$event
->
setAttributes
([
'filename'
=>
'bettername.jpg'
,
'en'
=>
[
'description'
=>
'Hello World'
,
]
]);
});
$file
=
$this
->
file
->
createFromFile
(
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'myfile.jpg'
));
$this
->
file
->
update
(
$file
,
[
'en'
=>
[
'description'
=>
'My cool file!'
,
'alt_attribute'
=>
'My cool file!'
,
'keywords'
=>
'My cool file!'
,
]
]);
$this
->
assertEquals
(
'bettername.jpg'
,
$file
->
filename
);
}
private
function
createFile
(
$fileName
=
'random/name.jpg'
)
{
return
File
::
create
([
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment