mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-03 13:37:27 +02:00
Repository is supposed to generate IDs
This commit is contained in:
@@ -5,7 +5,7 @@ namespace DesignPatterns\More\Repository\Domain;
|
|||||||
class Post
|
class Post
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var int|null
|
* @var int
|
||||||
*/
|
*/
|
||||||
private $id;
|
private $id;
|
||||||
|
|
||||||
@@ -19,6 +19,15 @@ class Post
|
|||||||
*/
|
*/
|
||||||
private $text;
|
private $text;
|
||||||
|
|
||||||
|
public static function draft(int $id, string $title, string $text): Post
|
||||||
|
{
|
||||||
|
return new self(
|
||||||
|
$id,
|
||||||
|
$title,
|
||||||
|
$text
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public static function fromState(array $state): Post
|
public static function fromState(array $state): Post
|
||||||
{
|
{
|
||||||
return new self(
|
return new self(
|
||||||
@@ -29,22 +38,17 @@ class Post
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int|null $id
|
* @param int $id
|
||||||
* @param string $text
|
* @param string $text
|
||||||
* @param string $title
|
* @param string $title
|
||||||
*/
|
*/
|
||||||
public function __construct($id, string $title, string $text)
|
private function __construct(int $id, string $title, string $text)
|
||||||
{
|
{
|
||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
$this->text = $text;
|
$this->text = $text;
|
||||||
$this->title = $title;
|
$this->title = $title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setId(int $id)
|
|
||||||
{
|
|
||||||
$this->id = $id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getId(): int
|
public function getId(): int
|
||||||
{
|
{
|
||||||
return $this->id;
|
return $this->id;
|
||||||
|
@@ -14,20 +14,22 @@ class MemoryStorage implements Storage
|
|||||||
*/
|
*/
|
||||||
private $lastId = 0;
|
private $lastId = 0;
|
||||||
|
|
||||||
public function persist(array $data): int
|
public function generateId(): int
|
||||||
{
|
{
|
||||||
$this->lastId++;
|
$this->lastId++;
|
||||||
|
|
||||||
$data['id'] = $this->lastId;
|
|
||||||
$this->data[$this->lastId] = $data;
|
|
||||||
|
|
||||||
return $this->lastId;
|
return $this->lastId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function persist(array $data)
|
||||||
|
{
|
||||||
|
$this->data[$this->lastId] = $data;
|
||||||
|
}
|
||||||
|
|
||||||
public function retrieve(int $id): array
|
public function retrieve(int $id): array
|
||||||
{
|
{
|
||||||
if (!isset($this->data[$id])) {
|
if (!isset($this->data[$id])) {
|
||||||
throw new \OutOfRangeException(sprintf('No data found for ID %d', $id));
|
throw new \OutOfBoundsException(sprintf('No data found for ID %d', $id));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->data[$id];
|
return $this->data[$id];
|
||||||
@@ -36,7 +38,7 @@ class MemoryStorage implements Storage
|
|||||||
public function delete(int $id)
|
public function delete(int $id)
|
||||||
{
|
{
|
||||||
if (!isset($this->data[$id])) {
|
if (!isset($this->data[$id])) {
|
||||||
throw new \OutOfRangeException(sprintf('No data found for ID %d', $id));
|
throw new \OutOfBoundsException(sprintf('No data found for ID %d', $id));
|
||||||
}
|
}
|
||||||
|
|
||||||
unset($this->data[$id]);
|
unset($this->data[$id]);
|
||||||
|
@@ -25,12 +25,17 @@ class PostRepository
|
|||||||
$this->persistence = $persistence;
|
$this->persistence = $persistence;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function generateId(): int
|
||||||
|
{
|
||||||
|
return $this->persistence->generateId();
|
||||||
|
}
|
||||||
|
|
||||||
public function findById(int $id): Post
|
public function findById(int $id): Post
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$arrayData = $this->persistence->retrieve($id);
|
$arrayData = $this->persistence->retrieve($id);
|
||||||
|
} catch (\OutOfBoundsException $e) {
|
||||||
if (is_null($arrayData)) {
|
throw new \OutOfBoundsException(sprintf('Post with id %d does not exist', $id), 0, $e);
|
||||||
throw new \InvalidArgumentException(sprintf('Post with ID %d does not exist', $id));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Post::fromState($arrayData);
|
return Post::fromState($arrayData);
|
||||||
@@ -38,11 +43,10 @@ class PostRepository
|
|||||||
|
|
||||||
public function save(Post $post)
|
public function save(Post $post)
|
||||||
{
|
{
|
||||||
$id = $this->persistence->persist([
|
$this->persistence->persist([
|
||||||
|
'id' => $post->getId(),
|
||||||
'text' => $post->getText(),
|
'text' => $post->getText(),
|
||||||
'title' => $post->getTitle(),
|
'title' => $post->getTitle(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$post->setId($id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -4,7 +4,9 @@ namespace DesignPatterns\More\Repository;
|
|||||||
|
|
||||||
interface Storage
|
interface Storage
|
||||||
{
|
{
|
||||||
public function persist(array $data): int;
|
public function generateId(): int;
|
||||||
|
|
||||||
|
public function persist(array $data);
|
||||||
|
|
||||||
public function retrieve(int $id): array;
|
public function retrieve(int $id): array;
|
||||||
|
|
||||||
|
44
More/Repository/Tests/PostRepositoryTest.php
Normal file
44
More/Repository/Tests/PostRepositoryTest.php
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\More\Repository\Tests;
|
||||||
|
|
||||||
|
use DesignPatterns\More\Repository\MemoryStorage;
|
||||||
|
use DesignPatterns\More\Repository\Domain\Post;
|
||||||
|
use DesignPatterns\More\Repository\PostRepository;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class PostRepositoryTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var PostRepository
|
||||||
|
*/
|
||||||
|
private $repository;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->repository = new PostRepository(new MemoryStorage());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCanGenerateId()
|
||||||
|
{
|
||||||
|
$this->assertEquals(1, $this->repository->generateId());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \OutOfBoundsException
|
||||||
|
* @expectedExceptionMessage Post with id 42 does not exist
|
||||||
|
*/
|
||||||
|
public function testThrowsExceptionWhenTryingToFindPostWhichDoesNotExist()
|
||||||
|
{
|
||||||
|
$this->repository->findById(42);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCanPersistPostDraft()
|
||||||
|
{
|
||||||
|
$postId = $this->repository->generateId();
|
||||||
|
$post = Post::draft($postId, 'Repository Pattern', 'Design Patterns PHP');
|
||||||
|
$this->repository->save($post);
|
||||||
|
|
||||||
|
$this->assertEquals($postId, $this->repository->findById($postId)->getId());
|
||||||
|
}
|
||||||
|
}
|
@@ -1,22 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\More\Repository\Tests;
|
|
||||||
|
|
||||||
use DesignPatterns\More\Repository\MemoryStorage;
|
|
||||||
use DesignPatterns\More\Repository\Domain\Post;
|
|
||||||
use DesignPatterns\More\Repository\PostRepository;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
|
||||||
|
|
||||||
class RepositoryTest extends TestCase
|
|
||||||
{
|
|
||||||
public function testCanPersistAndFindPost()
|
|
||||||
{
|
|
||||||
$repository = new PostRepository(new MemoryStorage());
|
|
||||||
$post = new Post(null, 'Repository Pattern', 'Design Patterns PHP');
|
|
||||||
|
|
||||||
$repository->save($post);
|
|
||||||
|
|
||||||
$this->assertEquals(1, $post->getId());
|
|
||||||
$this->assertEquals($post->getId(), $repository->findById(1)->getId());
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user