Skip to content

Commit

Permalink
Merge pull request #158 from pellaras/master
Browse files Browse the repository at this point in the history
Propagate deletations and restoriations when using ModelTrait
  • Loading branch information
baopham authored Sep 27, 2018
2 parents 8b0c18e + 4c5b11b commit 7367d68
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/ModelObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,51 @@ public function __construct(DynamoDbClientInterface $dynamoDb)
$this->attributeFilter = $dynamoDb->getAttributeFilter();
}

public function saved($model)
private function saveToDynamoDb($model)
{
$attrs = $model->attributesToArray();
// $this->attributeFilter->filter($attrs);

try {
$this->dynamoDbClient->putItem([
'TableName' => $model->getDynamoDbTableName(),
'Item' => $this->marshaler->marshalItem($attrs),
]);
} catch (Exception $e) {
Log::info($e);
Log::error($e);
}
}

private function deleteFromDynamoDb($model)
{
$key = [$model->getKeyName() => $model->getKey()];

try {
$this->dynamoDbClient->deleteItem([
'TableName' => $model->getDynamoDbTableName(),
'Key' => $this->marshaler->marshalItem($key),
]);
} catch (Exception $e) {
Log::error($e);
}
}

public function created($model)
{
$this->saveToDynamoDb($model);
}

public function updated($model)
{
$this->saveToDynamoDb($model);
}

public function deleted($model)
{
$this->deleteFromDynamoDb($model);
}

public function restored($model)
{
$this->saveToDynamoDb($model);
}
}

0 comments on commit 7367d68

Please sign in to comment.