Adding a folder is creating hook

parent e2f783e3
<?php
namespace Modules\Media\Events;
use Modules\Core\Contracts\EntityIsChanging;
use Modules\Core\Events\AbstractEntityHook;
class FolderIsCreating extends AbstractEntityHook implements EntityIsChanging
{
}
......@@ -3,6 +3,7 @@
namespace Modules\Media\Repositories\Eloquent;
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Media\Events\FolderIsCreating;
use Modules\Media\Events\FolderWasCreated;
use Modules\Media\Repositories\FolderRepository;
......@@ -10,12 +11,14 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
{
public function create($data)
{
$folder = $this->model->create([
$data = [
'filename' => array_get($data, 'name'),
'path' => config('asgard.media.config.files-path') . str_slug(array_get($data, 'name')),
'is_folder' => true,
'folder_id' => array_get($data, 'parent_id'),
]);
];
event($event = new FolderIsCreating($data));
$folder = $this->model->create($event->getAttributes());
event(new FolderWasCreated($folder, $data));
......
......@@ -3,6 +3,7 @@
namespace Modules\Media\Tests;
use Illuminate\Support\Facades\Event;
use Modules\Media\Events\FolderIsCreating;
use Modules\Media\Events\FolderWasCreated;
use Modules\Media\Repositories\Eloquent\EloquentFolderRepository;
use Modules\Media\Repositories\FolderRepository;
......@@ -50,6 +51,31 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
});
}
/** @test */
public function it_triggers_an_event_when_folder_is_creating()
{
Event::fake();
$folder = $this->folder->create(['name' => 'My Folder']);
Event::assertDispatched(FolderIsCreating::class, function ($e) use ($folder) {
return $e->getAttribute('filename') === $folder->filename;
});
}
/** @test */
public function it_can_change_folder_data_before_creating_folder()
{
Event::listen(FolderIsCreating::class, function (FolderIsCreating $event) {
$filename = $event->getAttribute('filename');
$event->setAttributes(['filename' => strtoupper($filename)]);
});
$folder = $this->folder->create(['name' => 'My Folder']);
$this->assertEquals('MY FOLDER', $folder->filename);
}
private function resetDatabase()
{
// Makes sure the migrations table is created
......
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