Trigger an event when a file was created

parent 3b844234
<?php
namespace Modules\Media\Events;
use Modules\Media\Entities\File;
class FileWasCreated
{
/**
* @var File
*/
public $file;
public function __construct(File $file)
{
$this->file = $file;
}
}
...@@ -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)
......
...@@ -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([
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment