diff --git a/src/CollectionExtension.php b/src/CollectionExtension.php index 4c318af..d1af021 100644 --- a/src/CollectionExtension.php +++ b/src/CollectionExtension.php @@ -19,6 +19,11 @@ class CollectionExtension return fn ($callback) => $this->registerCustomCallback('storing', $callback); } + public function destroyed() + { + return fn ($callback) => $this->registerCustomCallback('destroyed', $callback); + } + public function withDefaultProperties() { return fn ($callback) => $this->registerCustomCallback('withDefaultProperties', $callback); @@ -67,6 +72,7 @@ class CollectionExtension $this->customCallbacks = collect([ 'forceFileName' => fn ($model, $name) => $name, 'stored' => fn ($event) => true, + 'destroyed' => fn ($event) => true, 'storing' => fn ($adder, $name) => $adder, 'withDefaultProperties' => fn ($path) => [], 'withPropertyValidation' => fn ($path) => [], diff --git a/src/MediaController.php b/src/MediaController.php index b140a7a..b92f82a 100644 --- a/src/MediaController.php +++ b/src/MediaController.php @@ -5,7 +5,6 @@ namespace Zoomyboy\MedialibraryHelper; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Storage; use Illuminate\Validation\Rule; use Illuminate\Validation\ValidationException; use Spatie\LaravelData\DataCollection; @@ -98,7 +97,10 @@ class MediaController public function destroy(Media $media, Request $request): JsonResponse { $this->authorize('destroyMedia', [$media->model, $media->collection_name]); + $model = $media->model->fresh(); + $collection = $model->getMediaCollection($media->collection_name); $media->delete(); + $collection->runCallback('destroyed', $media->model->fresh()); return response()->json([]); } diff --git a/tests/Events/MediaDestroyed.php b/tests/Events/MediaDestroyed.php new file mode 100644 index 0000000..51ff85d --- /dev/null +++ b/tests/Events/MediaDestroyed.php @@ -0,0 +1,22 @@ +auth()->registerModel(); + $this->auth()->registerModel()->withoutExceptionHandling(); $post = $this->newPost(); $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('multipleForced'); $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('multipleForced'); @@ -33,3 +36,15 @@ test('it needs authorization', function () { $this->deleteJson("/mediaupload/{$media->id}")->assertStatus(403); }); + +test('it fires event', function () { + Event::fake(); + $this->auth()->registerModel()->withoutExceptionHandling(); + $post = $this->newPost(); + $post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('singleWithEvent'); + $media = $post->getFirstMedia('singleWithEvent'); + + $this->deleteJson("/mediaupload/{$media->id}")->assertStatus(200); + + Event::assertDispatched(MediaDestroyed::class, fn ($event) => $event->model->is($post)); +}); diff --git a/tests/Models/Post.php b/tests/Models/Post.php index 304dc4b..9951ce2 100644 --- a/tests/Models/Post.php +++ b/tests/Models/Post.php @@ -8,6 +8,7 @@ use Illuminate\Support\Str; use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\InteractsWithMedia; use Spatie\MediaLibrary\MediaCollections\Models\Media; +use Zoomyboy\MedialibraryHelper\Tests\Events\MediaDestroyed; use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored; class Post extends Model implements HasMedia @@ -46,7 +47,8 @@ class Post extends Model implements HasMedia $this->addMediaCollection('singleWithEvent')->singleFile()->stored(function (Media $media) { Event::dispatch(new MediaStored($media)); - }); + }) + ->destroyed(fn ($model) => Event::dispatch(new MediaDestroyed($model))); $this->addMediaCollection('multipleFilesWithEvent')->stored(function (Media $media) { Event::dispatch(new MediaStored($media));