Adding an hookable event when a file is creating

parent 17af3d58
<?php
namespace Modules\Media\Events;
use Modules\Core\Contracts\EntityIsChanging;
final class FileIsCreating implements EntityIsChanging
{
/**
* Contains the attributes which can be changed by other listeners
* @var array
*/
private $attributes;
/**
* Contains the original attributes which cannot be changed
* @var array
*/
private $original;
public function __construct(array $attributes)
{
$this->attributes = $attributes;
$this->original = $attributes;
}
/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* @param array $attributes
*/
public function setAttributes(array $attributes)
{
$this->attributes = array_merge($this->attributes, $attributes);
}
/**
* @return array
*/
public function getOriginal()
{
return $this->original;
}
}
......@@ -5,6 +5,7 @@ namespace Modules\Media\Repositories\Eloquent;
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\FileWasCreated;
use Modules\Media\Helpers\FileHelper;
use Modules\Media\Repositories\FileRepository;
......@@ -42,15 +43,18 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
$fileName = $this->getNewUniqueFilename($fileName);
}
$file = $this->model->create([
$data = [
'filename' => $fileName,
'path' => config('asgard.media.config.files-path') . "{$fileName}",
'extension' => substr(strrchr($fileName, '.'), 1),
'mimetype' => $file->getClientMimeType(),
'filesize' => $file->getFileInfo()->getSize(),
'folder_id' => 0,
]);
];
event($event = new FileIsCreating($data));
$file = $this->model->create($event->getAttributes());
event(new FileWasCreated($file));
return $file;
......
......@@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Storage;
use Mockery;
use Modules\Media\Entities\File;
use Modules\Media\Events\FileIsCreating;
use Modules\Media\Events\FileWasCreated;
use Modules\Media\Repositories\FileRepository;
use Symfony\Component\Finder\SplFileInfo;
......@@ -128,6 +129,30 @@ class FileRepositoryTest extends MediaTestCase
});
}
/** @test */
public function it_triggers_event_when_file_is_creating()
{
Event::fake();
$file = $this->file->createFromFile(\Illuminate\Http\UploadedFile::fake()->image('myfile.jpg'));
Event::assertDispatched(FileIsCreating::class, function ($e) use ($file) {
return $e->getAttributes()['filename'] === $file->filename;
});
}
/** @test */
public function it_can_change_data_when_it_is_creating_event()
{
Event::listen(FileIsCreating::class, function (FileIsCreating $event) {
$event->setAttributes(['filename' => 'imabettername.jpg']);
});
$file = $this->file->createFromFile(\Illuminate\Http\UploadedFile::fake()->image('myfile.jpg'));
$this->assertEquals('imabettername.jpg', $file->filename);
}
private function createFile($fileName = 'random/name.jpg')
{
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