Skip to content

Commit

Permalink
Custom destroy attributes (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeek committed Aug 7, 2023
1 parent 4770e96 commit 405a167
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
19 changes: 12 additions & 7 deletions src/Integration/Laravel/Contracts/SyncableModel.php
Expand Up @@ -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();
}
8 changes: 4 additions & 4 deletions src/Integration/Laravel/Syncer.php
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/Integration/Laravel/TableSyncable.php
Expand Up @@ -20,6 +20,11 @@ public function getTableSyncableAttributes(): array
return $this->getAttributes();
}

public function getTableSyncableDestroyAttributes(): array
{
return [];
}

public function classForSync(): string
{
return static::class;
Expand Down
Expand Up @@ -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;
Expand Down
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/Laravel/Traits/SpyPublisher.php
Expand Up @@ -9,7 +9,7 @@

trait SpyPublisher
{
protected function makeSpyPublsiher(): Publisher
protected function makeSpyPublisher(): Publisher
{
return new class() implements Publisher {
public $messages;
Expand Down

0 comments on commit 405a167

Please sign in to comment.