From 4e1b14a7e16f4bf99748d764bbb14832661e4087 Mon Sep 17 00:00:00 2001 From: Pascal Cremer Date: Thu, 12 Jan 2023 23:06:37 +0100 Subject: [PATCH] refactor!: Change Repository interface. This is a breaking change since both method signatures of `collect()` and `paginate()` have been changed. --- src/Contracts/Domain/Repository.php | 16 +++--- src/Infrastructure/InMemory/Repository.php | 52 +++++-------------- .../InMemory/RepositoryTest.php | 44 ++-------------- 3 files changed, 27 insertions(+), 85 deletions(-) diff --git a/src/Contracts/Domain/Repository.php b/src/Contracts/Domain/Repository.php index 4950040..439e5a6 100644 --- a/src/Contracts/Domain/Repository.php +++ b/src/Contracts/Domain/Repository.php @@ -5,24 +5,28 @@ namespace GeekCell\Ddd\Contracts\Domain; use Countable; +use GeekCell\Ddd\Domain\Collection; use IteratorAggregate; interface Repository extends Countable, IteratorAggregate { /** - * Returns a new instance of the repository without pagination. + * Returns a collection of items. * - * @return static + * @return Collection */ - public function collect(): static; + public function collect(): Collection; /** - * Returns a new instance of the repository with pagination. + * Returns a paginator to paginate the items. * * @param int $itemsPerPage * @param int $currentPage * - * @return static + * @return Paginator */ - public function paginate(int $itemsPerPage, int $currentPage = 1): static; + public function paginate( + int $itemsPerPage, + int $currentPage = 1 + ): Paginator; } diff --git a/src/Infrastructure/InMemory/Repository.php b/src/Infrastructure/InMemory/Repository.php index e696fac..f492fc7 100644 --- a/src/Infrastructure/InMemory/Repository.php +++ b/src/Infrastructure/InMemory/Repository.php @@ -5,7 +5,9 @@ namespace GeekCell\Ddd\Infrastructure\InMemory; use Assert\Assert; +use GeekCell\Ddd\Contracts\Domain\Paginator; use GeekCell\Ddd\Contracts\Domain\Repository as RepositoryInterface; +use GeekCell\Ddd\Domain\Collection; use GeekCell\Ddd\Infrastructure\InMemory\Paginator as InMemoryPaginator; use Traversable; @@ -16,21 +18,6 @@ abstract class Repository implements RepositoryInterface */ protected array $items = []; - /** - * @var int|null - */ - protected ?int $itemsPerPage = null; - - /** - * @var int|null - */ - protected ?int $currentPage = null; - - /** - * @var bool - */ - protected bool $isPaginated = false; - /** * @template T of object * @extends IteratorAggregate @@ -50,27 +37,22 @@ public function __construct( /** * @inheritDoc */ - public function collect(): static + public function collect(): Collection { - $clone = clone $this; - $clone->itemsPerPage = null; - $clone->currentPage = null; - $clone->isPaginated = false; - - return $clone; + $collectionClass = $this->collectionType; + return new $collectionClass($this->items); } /** * @inheritDoc */ - public function paginate(int $itemsPerPage, int $currentPage = 1): static + public function paginate(int $itemsPerPage, int $currentPage = 1): Paginator { - $clone = clone $this; - $clone->itemsPerPage = $itemsPerPage; - $clone->currentPage = $currentPage; - $clone->isPaginated = true; - - return $clone; + return new InMemoryPaginator( + $this->collect(), + $itemsPerPage, + $currentPage + ); } /** @@ -78,17 +60,7 @@ public function paginate(int $itemsPerPage, int $currentPage = 1): static */ public function getIterator(): Traversable { - $collectionClass = $this->collectionType; - $collection = new $collectionClass($this->items); - if ($this->isPaginated) { - return new InMemoryPaginator( - $collection, - $this->itemsPerPage, - $this->currentPage - ); - } - - return $collection; + return $this->collect()->getIterator(); } /** diff --git a/tests/Infrastructure/InMemory/RepositoryTest.php b/tests/Infrastructure/InMemory/RepositoryTest.php index 5f727b7..cd2dd03 100644 --- a/tests/Infrastructure/InMemory/RepositoryTest.php +++ b/tests/Infrastructure/InMemory/RepositoryTest.php @@ -43,21 +43,6 @@ public function setItems(array $items): void { $this->items = $items; } - - public function isPaginated(): bool - { - return $this->isPaginated; - } - - public function getItemsPerPage(): ?int - { - return $this->itemsPerPage; - } - - public function getCurrentPage(): ?int - { - return $this->currentPage; - } } class InMemoryRepositoryTest extends TestCase @@ -111,9 +96,7 @@ public function testCollectAndCount(): void $result = $repository->collect(); // Then - $this->assertFalse($result->isPaginated()); - $this->assertNull($result->getItemsPerPage()); - $this->assertNull($result->getCurrentPage()); + $this->assertInstanceOf(CounterCollection::class, $result); $this->assertEquals(count($this->items), count($result)); } @@ -131,9 +114,7 @@ public function testPaginateAndCount(): void $result = $repository->paginate(2); // Then - $this->assertTrue($result->isPaginated()); - $this->assertEquals(2, $result->getItemsPerPage()); - $this->assertEquals(1, $result->getCurrentPage()); + $this->assertInstanceOf(InMemoryPaginator::class, $result); $this->assertEquals(2, count($result)); } @@ -142,27 +123,12 @@ public function testGetIterator(): void // Given $repository = new InMemoryTestRepository(); $repository->setItems($this->items); + $collection = $repository->collect(); // When - $result = $repository->collect(); - - // Then - $this->assertInstanceOf(Collection::class, $result->getIterator()); - } - - public function testGetIteratorWithPagination(): void - { - // Given - $repository = new InMemoryTestRepository(); - $repository->setItems($this->items); - - // When - $result = $repository->paginate(2); + $result = $repository->getIterator(); // Then - $this->assertInstanceOf( - InMemoryPaginator::class, - $result->getIterator() - ); + $this->assertEquals($collection->getIterator(), $result); } }