Skip to content

Commit 944224e

Browse files
committed
Add callback to pass on updates.
1 parent 8d5f6a7 commit 944224e

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

laravel/src/SyncApiCollection.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,21 @@ public function __construct(
4747
* @param string $name The name of the media collection.
4848
* @param int $syncCapabilities The actions available to API
4949
* consumers.
50+
* @param callable $onUpsertOrDelete Invoked when media is upserted or
51+
* deleted.
5052
* @return SyncApiCollectionMediaCollection The created media collection.
5153
*/
5254
public function withMediaCollection(
5355
string $name,
5456
int $syncCapabilities,
57+
?callable $onUpsertOrDelete,
5558
): SyncApiCollectionMediaCollection {
5659
$syncApiCollectionMediaCollection = new SyncApiCollectionMediaCollection(
5760
$this,
5861
$name,
5962
$syncCapabilities,
6063
$this->routeFragment,
64+
$onUpsertOrDelete,
6165
);
6266

6367
$this->syncApiCollectionMediaCollections[] = $syncApiCollectionMediaCollection;

laravel/src/SyncApiCollectionInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ interface SyncApiCollectionInterface extends SyncApiInterface
1212
* Adds a new media collection to this collection.
1313
* @param string $name The name of the media collection.
1414
* @param int $syncCapabilities The actions available to API consumers.
15+
* @param callable $onUpsertOrDelete Invoked when media is upserted or
16+
* deleted.
1517
*/
1618
function withMediaCollection(
1719
string $name,
1820
int $syncCapabilities,
21+
?callable $onUpsertOrDelete,
1922
): SyncApiCollectionMediaCollection;
2023
}

laravel/src/SyncApiCollectionMediaCollection.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class SyncApiCollectionMediaCollection implements SyncApiCollectionMediaCollecti
2121

2222
private string $routeFragment;
2323

24+
private array $onUpsertOrDelete;
25+
2426
private function getMediaUuidField(): string
2527
{
2628
if (config('react-native-sync')) {
@@ -56,20 +58,24 @@ public function __construct(
5658
string $name,
5759
int $syncCapabilities,
5860
?string $routeFragment,
61+
?callable $onUpsertOrDelete,
5962
) {
6063
$this->syncApiCollection = $syncApiCollection;
6164
$this->name = $name;
6265
$this->syncCapabilities = $syncCapabilities;
6366
$this->routeFragment = $routeFragment ?? Str::kebab(Str::pluralStudly(class_basename($this->syncApiCollection->modelClass)));
67+
$this->onUpsertOrDelete = $onUpsertOrDelete === null ? [] : [$onUpsertOrDelete];
6468
}
6569

6670
public function withMediaCollection(
6771
string $name,
6872
int $syncCapabilities,
73+
?callable $onUpsertOrDelete,
6974
): SyncApiCollectionMediaCollection {
7075
return $this->syncApiCollection->withMediaCollection(
7176
$name,
7277
$syncCapabilities,
78+
$onUpsertOrDelete,
7379
);
7480
}
7581

@@ -200,11 +206,15 @@ function (string $modelUuid, string $mediaUuid) {
200206
$extension = '';
201207
}
202208

203-
$model
209+
$media = $model
204210
->addMediaFromString($data)
205211
->usingName($mediaUuid . $extension)
206212
->withProperties(['uuid' => $mediaUuid])
207213
->toMediaCollection($this->name);
214+
215+
foreach ($this->onUpsertOrDelete as $callback) {
216+
$callback($media);
217+
}
208218
}
209219
} else {
210220
throw new ModelNotFoundException();
@@ -237,6 +247,10 @@ function (string $modelUuid, string $mediaUuid) {
237247

238248
if ($media !== null) {
239249
$media->delete();
250+
251+
foreach ($this->onUpsertOrDelete as $callback) {
252+
$callback($media);
253+
}
240254
}
241255
} else {
242256
throw new ModelNotFoundException();

0 commit comments

Comments
 (0)