Skip to content

Commit

Permalink
Add callback to pass on updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswilddev committed Jul 23, 2024
1 parent 8d5f6a7 commit 944224e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions laravel/src/SyncApiCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions laravel/src/SyncApiCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
16 changes: 15 additions & 1 deletion laravel/src/SyncApiCollectionMediaCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class SyncApiCollectionMediaCollection implements SyncApiCollectionMediaCollecti

private string $routeFragment;

private array $onUpsertOrDelete;

private function getMediaUuidField(): string
{
if (config('react-native-sync')) {
Expand Down Expand Up @@ -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,
);
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 944224e

Please sign in to comment.