From 944224eab007dc55721093b551beb700b6d25e05 Mon Sep 17 00:00:00 2001 From: jameswilddev Date: Tue, 23 Jul 2024 14:00:45 +0100 Subject: [PATCH] Add callback to pass on updates. --- laravel/src/SyncApiCollection.php | 4 ++++ laravel/src/SyncApiCollectionInterface.php | 3 +++ laravel/src/SyncApiCollectionMediaCollection.php | 16 +++++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/laravel/src/SyncApiCollection.php b/laravel/src/SyncApiCollection.php index da807f1..f1d53ff 100644 --- a/laravel/src/SyncApiCollection.php +++ b/laravel/src/SyncApiCollection.php @@ -47,17 +47,21 @@ public function __construct( * @param string $name The name of the media collection. * @param int $syncCapabilities The actions available to API * consumers. + * @param callable $onUpsertOrDelete Invoked when media is upserted or + * deleted. * @return SyncApiCollectionMediaCollection The created media collection. */ public function withMediaCollection( string $name, int $syncCapabilities, + ?callable $onUpsertOrDelete, ): SyncApiCollectionMediaCollection { $syncApiCollectionMediaCollection = new SyncApiCollectionMediaCollection( $this, $name, $syncCapabilities, $this->routeFragment, + $onUpsertOrDelete, ); $this->syncApiCollectionMediaCollections[] = $syncApiCollectionMediaCollection; diff --git a/laravel/src/SyncApiCollectionInterface.php b/laravel/src/SyncApiCollectionInterface.php index ad6d472..3ee86d9 100644 --- a/laravel/src/SyncApiCollectionInterface.php +++ b/laravel/src/SyncApiCollectionInterface.php @@ -12,9 +12,12 @@ interface SyncApiCollectionInterface extends SyncApiInterface * Adds a new media collection to this collection. * @param string $name The name of the media collection. * @param int $syncCapabilities The actions available to API consumers. + * @param callable $onUpsertOrDelete Invoked when media is upserted or + * deleted. */ function withMediaCollection( string $name, int $syncCapabilities, + ?callable $onUpsertOrDelete, ): SyncApiCollectionMediaCollection; } diff --git a/laravel/src/SyncApiCollectionMediaCollection.php b/laravel/src/SyncApiCollectionMediaCollection.php index da53ae7..36ca683 100644 --- a/laravel/src/SyncApiCollectionMediaCollection.php +++ b/laravel/src/SyncApiCollectionMediaCollection.php @@ -21,6 +21,8 @@ class SyncApiCollectionMediaCollection implements SyncApiCollectionMediaCollecti private string $routeFragment; + private array $onUpsertOrDelete; + private function getMediaUuidField(): string { if (config('react-native-sync')) { @@ -56,20 +58,24 @@ public function __construct( string $name, int $syncCapabilities, ?string $routeFragment, + ?callable $onUpsertOrDelete, ) { $this->syncApiCollection = $syncApiCollection; $this->name = $name; $this->syncCapabilities = $syncCapabilities; $this->routeFragment = $routeFragment ?? Str::kebab(Str::pluralStudly(class_basename($this->syncApiCollection->modelClass))); + $this->onUpsertOrDelete = $onUpsertOrDelete === null ? [] : [$onUpsertOrDelete]; } public function withMediaCollection( string $name, int $syncCapabilities, + ?callable $onUpsertOrDelete, ): SyncApiCollectionMediaCollection { return $this->syncApiCollection->withMediaCollection( $name, $syncCapabilities, + $onUpsertOrDelete, ); } @@ -200,11 +206,15 @@ function (string $modelUuid, string $mediaUuid) { $extension = ''; } - $model + $media = $model ->addMediaFromString($data) ->usingName($mediaUuid . $extension) ->withProperties(['uuid' => $mediaUuid]) ->toMediaCollection($this->name); + + foreach ($this->onUpsertOrDelete as $callback) { + $callback($media); + } } } else { throw new ModelNotFoundException(); @@ -237,6 +247,10 @@ function (string $modelUuid, string $mediaUuid) { if ($media !== null) { $media->delete(); + + foreach ($this->onUpsertOrDelete as $callback) { + $callback($media); + } } } else { throw new ModelNotFoundException();