Repository is supposed to generate IDs

This commit is contained in:
Dominik Liebler
2018-06-14 17:45:45 +02:00
parent 4cb353fe3e
commit 2ab82dbfbf
6 changed files with 78 additions and 44 deletions

View File

@@ -25,12 +25,17 @@ class PostRepository
$this->persistence = $persistence;
}
public function generateId(): int
{
return $this->persistence->generateId();
}
public function findById(int $id): Post
{
$arrayData = $this->persistence->retrieve($id);
if (is_null($arrayData)) {
throw new \InvalidArgumentException(sprintf('Post with ID %d does not exist', $id));
try {
$arrayData = $this->persistence->retrieve($id);
} catch (\OutOfBoundsException $e) {
throw new \OutOfBoundsException(sprintf('Post with id %d does not exist', $id), 0, $e);
}
return Post::fromState($arrayData);
@@ -38,11 +43,10 @@ class PostRepository
public function save(Post $post)
{
$id = $this->persistence->persist([
$this->persistence->persist([
'id' => $post->getId(),
'text' => $post->getText(),
'title' => $post->getTitle(),
]);
$post->setId($id);
}
}