Adding event before a file is updated

parent 7876e92b
<?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;
}
}
......@@ -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', []));
......
......@@ -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([
......
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