From 405a167908dcf46c77e49a45bfc68a6e9da57306 Mon Sep 17 00:00:00 2001 From: Eugene Ivanov <93479789+evgeek@users.noreply.github.com> Date: Mon, 7 Aug 2023 15:44:36 +0300 Subject: [PATCH] Custom destroy attributes (#23) --- .../Laravel/Contracts/SyncableModel.php | 19 ++++++++++++------- src/Integration/Laravel/Syncer.php | 8 ++++---- src/Integration/Laravel/TableSyncable.php | 5 +++++ .../EnsureConsistencyPublisherTest.php | 2 +- .../Integration/TableSyncObserverTest.php | 2 +- .../Laravel/Traits/SpyPublisher.php | 2 +- 6 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/Integration/Laravel/Contracts/SyncableModel.php b/src/Integration/Laravel/Contracts/SyncableModel.php index f2acee5..ab43d0b 100644 --- a/src/Integration/Laravel/Contracts/SyncableModel.php +++ b/src/Integration/Laravel/Contracts/SyncableModel.php @@ -7,37 +7,42 @@ interface SyncableModel { /** - * @var string + * @return string */ public function getKeyName(); /** - * @var string + * @return string */ public function getKey(); /** - * @var static|null + * @return static|null */ public function fresh(); /** - * @var array + * @return array */ public function getTableSyncableAttributes(); /** - * @var string + * @return array + */ + public function getTableSyncableDestroyAttributes(); + + /** + * @return string */ public function classForSync(); /** - * @var bool + * @return bool */ public function exists(); /** - * @var string + * @return string */ public function routingKey(); } diff --git a/src/Integration/Laravel/Syncer.php b/src/Integration/Laravel/Syncer.php index e34ebdf..56c4207 100644 --- a/src/Integration/Laravel/Syncer.php +++ b/src/Integration/Laravel/Syncer.php @@ -48,11 +48,11 @@ private function syncTable(SyncableModel $model, string $event): void private function getSyncableAttributes(SyncableModel $model): array { - if (!$model->exists()) { - return $this->pkAttributes($model); - } + $syncableAttributes = $model->exists() ? + $model->getTableSyncableAttributes() : + $model->getTableSyncableDestroyAttributes(); - return array_merge($this->pkAttributes($model), $model->getTableSyncableAttributes()); + return array_merge($this->pkAttributes($model), $syncableAttributes); } private function needsPublishAttributes(SyncableModel $model, array $attributes): bool diff --git a/src/Integration/Laravel/TableSyncable.php b/src/Integration/Laravel/TableSyncable.php index d5f2eee..816fd11 100644 --- a/src/Integration/Laravel/TableSyncable.php +++ b/src/Integration/Laravel/TableSyncable.php @@ -20,6 +20,11 @@ public function getTableSyncableAttributes(): array return $this->getAttributes(); } + public function getTableSyncableDestroyAttributes(): array + { + return []; + } + public function classForSync(): string { return static::class; diff --git a/tests/functional/Laravel/Integration/Publishers/EnsureConsistencyPublisherTest.php b/tests/functional/Laravel/Integration/Publishers/EnsureConsistencyPublisherTest.php index 365fd35..d9a13bd 100644 --- a/tests/functional/Laravel/Integration/Publishers/EnsureConsistencyPublisherTest.php +++ b/tests/functional/Laravel/Integration/Publishers/EnsureConsistencyPublisherTest.php @@ -24,7 +24,7 @@ protected function setUp(): void { parent::setUp(); - $this->spyPublisher = $this->makeSpyPublsiher(); + $this->spyPublisher = $this->makeSpyPublisher(); $this->app->bind(Publisher::class, function () { return $this->spyPublisher; diff --git a/tests/functional/Laravel/Integration/TableSyncObserverTest.php b/tests/functional/Laravel/Integration/TableSyncObserverTest.php index 77a5297..0d6c1a2 100644 --- a/tests/functional/Laravel/Integration/TableSyncObserverTest.php +++ b/tests/functional/Laravel/Integration/TableSyncObserverTest.php @@ -22,7 +22,7 @@ protected function setUp(): void { parent::setUp(); - $this->spyPublisher = $this->makeSpyPublsiher(); + $this->spyPublisher = $this->makeSpyPublisher(); $this->app->bind(Publisher::class, function () { return $this->spyPublisher; diff --git a/tests/functional/Laravel/Traits/SpyPublisher.php b/tests/functional/Laravel/Traits/SpyPublisher.php index 2cc7105..a7d68e3 100644 --- a/tests/functional/Laravel/Traits/SpyPublisher.php +++ b/tests/functional/Laravel/Traits/SpyPublisher.php @@ -9,7 +9,7 @@ trait SpyPublisher { - protected function makeSpyPublsiher(): Publisher + protected function makeSpyPublisher(): Publisher { return new class() implements Publisher { public $messages;