Trigger an event when a folder was created

parent 071db56d
<?php
namespace Modules\Media\Events;
use Modules\Media\Entities\File;
class FolderWasCreated
{
/**
* @var File
*/
public $folder;
/**
* @var array
*/
public $data;
public function __construct(File $folder, array $data)
{
$this->folder = $folder;
$this->data = $data;
}
}
......@@ -3,17 +3,22 @@
namespace Modules\Media\Repositories\Eloquent;
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Media\Events\FolderWasCreated;
use Modules\Media\Repositories\FolderRepository;
class EloquentFolderRepository extends EloquentBaseRepository implements FolderRepository
{
public function create($data)
{
return $this->model->create([
$folder = $this->model->create([
'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(new FolderWasCreated($folder, $data));
return $folder;
}
}
......@@ -2,6 +2,8 @@
namespace Modules\Media\Tests;
use Illuminate\Support\Facades\Event;
use Modules\Media\Events\FolderWasCreated;
use Modules\Media\Repositories\Eloquent\EloquentFolderRepository;
use Modules\Media\Repositories\FolderRepository;
use PHPUnit\Framework\TestCase;
......@@ -36,6 +38,18 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
$this->assertEquals(0, $folder->folder_id);
}
/** @test */
public function it_triggers_event_on_created_folder()
{
Event::fake();
$folder = $this->folder->create(['name' => 'My Folder']);
Event::assertDispatched(FolderWasCreated::class, function ($e) use ($folder) {
return $e->folder->id === $folder->id;
});
}
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