Trigger event when a folder is being deleted

parent 45f07bb8
<?php
namespace Modules\Media\Events;
use Modules\Media\Entities\File;
class FolderIsDeleting
{
/**
* @var File
*/
public $folder;
public function __construct(File $folder)
{
$this->folder = $folder;
}
}
......@@ -5,6 +5,7 @@ namespace Modules\Media\Repositories\Eloquent;
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Media\Entities\File;
use Modules\Media\Events\FolderIsCreating;
use Modules\Media\Events\FolderIsDeleting;
use Modules\Media\Events\FolderIsUpdating;
use Modules\Media\Events\FolderWasCreated;
use Modules\Media\Events\FolderWasUpdated;
......@@ -65,6 +66,8 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
public function destroy($folder)
{
event(new FolderIsDeleting($folder));
return $folder->delete();
}
......
......@@ -5,6 +5,7 @@ namespace Modules\Media\Tests;
use Illuminate\Support\Facades\Event;
use Modules\Media\Entities\File;
use Modules\Media\Events\FolderIsCreating;
use Modules\Media\Events\FolderIsDeleting;
use Modules\Media\Events\FolderIsUpdating;
use Modules\Media\Events\FolderWasCreated;
use Modules\Media\Events\FolderWasUpdated;
......@@ -248,6 +249,19 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
$this->assertCount(0, $this->folder->all());
}
/** @test */
public function it_triggers_event_when_folder_is_deleting()
{
Event::fake();
$folder = $this->folder->create(['name' => 'My Folder']);
$this->folder->destroy($folder);
Event::assertDispatched(FolderIsDeleting::class, function ($e) use ($folder) {
return $e->folder->id === $folder->id;
});
}
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