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
17af3d58
Unverified
Commit
17af3d58
authored
Jul 11, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Trigger an event when a file was created
parent
3b844234
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
39 additions
and
2 deletions
+39
-2
FileWasCreated.php
Modules/Media/Events/FileWasCreated.php
+18
-0
EloquentFileRepository.php
...es/Media/Repositories/Eloquent/EloquentFileRepository.php
+7
-2
FileRepositoryTest.php
Modules/Media/Tests/FileRepositoryTest.php
+14
-0
No files found.
Modules/Media/Events/FileWasCreated.php
0 → 100644
View file @
17af3d58
<?php
namespace
Modules\Media\Events
;
use
Modules\Media\Entities\File
;
class
FileWasCreated
{
/**
* @var File
*/
public
$file
;
public
function
__construct
(
File
$file
)
{
$this
->
file
=
$file
;
}
}
Modules/Media/Repositories/Eloquent/EloquentFileRepository.php
View file @
17af3d58
...
@@ -5,6 +5,7 @@ namespace Modules\Media\Repositories\Eloquent;
...
@@ -5,6 +5,7 @@ namespace Modules\Media\Repositories\Eloquent;
use
Illuminate\Database\Eloquent\Collection
;
use
Illuminate\Database\Eloquent\Collection
;
use
Modules\Core\Repositories\Eloquent\EloquentBaseRepository
;
use
Modules\Core\Repositories\Eloquent\EloquentBaseRepository
;
use
Modules\Media\Entities\File
;
use
Modules\Media\Entities\File
;
use
Modules\Media\Events\FileWasCreated
;
use
Modules\Media\Helpers\FileHelper
;
use
Modules\Media\Helpers\FileHelper
;
use
Modules\Media\Repositories\FileRepository
;
use
Modules\Media\Repositories\FileRepository
;
use
Symfony\Component\HttpFoundation\File\UploadedFile
;
use
Symfony\Component\HttpFoundation\File\UploadedFile
;
...
@@ -41,14 +42,18 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
...
@@ -41,14 +42,18 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
$fileName
=
$this
->
getNewUniqueFilename
(
$fileName
);
$fileName
=
$this
->
getNewUniqueFilename
(
$fileName
);
}
}
return
$this
->
model
->
create
([
$file
=
$this
->
model
->
create
([
'filename'
=>
$fileName
,
'filename'
=>
$fileName
,
'path'
=>
config
(
'asgard.media.config.files-path'
)
.
"
{
$fileName
}
"
,
'path'
=>
config
(
'asgard.media.config.files-path'
)
.
"
{
$fileName
}
"
,
'extension'
=>
substr
(
strrchr
(
$fileName
,
"."
),
1
),
'extension'
=>
substr
(
strrchr
(
$fileName
,
'.'
),
1
),
'mimetype'
=>
$file
->
getClientMimeType
(),
'mimetype'
=>
$file
->
getClientMimeType
(),
'filesize'
=>
$file
->
getFileInfo
()
->
getSize
(),
'filesize'
=>
$file
->
getFileInfo
()
->
getSize
(),
'folder_id'
=>
0
,
'folder_id'
=>
0
,
]);
]);
event
(
new
FileWasCreated
(
$file
));
return
$file
;
}
}
public
function
destroy
(
$file
)
public
function
destroy
(
$file
)
...
...
Modules/Media/Tests/FileRepositoryTest.php
View file @
17af3d58
...
@@ -2,8 +2,11 @@
...
@@ -2,8 +2,11 @@
namespace
Modules\Media\Tests
;
namespace
Modules\Media\Tests
;
use
Illuminate\Support\Facades\Event
;
use
Illuminate\Support\Facades\Storage
;
use
Mockery
;
use
Mockery
;
use
Modules\Media\Entities\File
;
use
Modules\Media\Entities\File
;
use
Modules\Media\Events\FileWasCreated
;
use
Modules\Media\Repositories\FileRepository
;
use
Modules\Media\Repositories\FileRepository
;
use
Symfony\Component\Finder\SplFileInfo
;
use
Symfony\Component\Finder\SplFileInfo
;
use
Symfony\Component\HttpFoundation\File\UploadedFile
;
use
Symfony\Component\HttpFoundation\File\UploadedFile
;
...
@@ -114,6 +117,17 @@ class FileRepositoryTest extends MediaTestCase
...
@@ -114,6 +117,17 @@ class FileRepositoryTest extends MediaTestCase
$this
->
assertCount
(
0
,
$this
->
file
->
all
());
$this
->
assertCount
(
0
,
$this
->
file
->
all
());
}
}
/** @test */
public
function
it_triggers_event_when_file_was_created
()
{
Event
::
fake
();
$file
=
$this
->
file
->
createFromFile
(
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'myfile.jpg'
));
Event
::
assertDispatched
(
FileWasCreated
::
class
,
function
(
$e
)
use
(
$file
)
{
return
$e
->
file
->
id
===
$file
->
id
;
});
}
private
function
createFile
(
$fileName
=
'random/name.jpg'
)
private
function
createFile
(
$fileName
=
'random/name.jpg'
)
{
{
return
File
::
create
([
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