Skip to content

Commit

Permalink
refactor!: Change Repository interface.
Browse files Browse the repository at this point in the history
This is a breaking change since both method signatures of `collect()`
and `paginate()` have been changed.
  • Loading branch information
b00gizm committed Jan 12, 2023
1 parent 191d600 commit 4e1b14a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 85 deletions.
16 changes: 10 additions & 6 deletions src/Contracts/Domain/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
52 changes: 12 additions & 40 deletions src/Infrastructure/InMemory/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<T>
Expand All @@ -50,45 +37,30 @@ 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
);
}

/**
* @inheritDoc
*/
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();
}

/**
Expand Down
44 changes: 5 additions & 39 deletions tests/Infrastructure/InMemory/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}

Expand All @@ -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));
}

Expand All @@ -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);
}
}

0 comments on commit 4e1b14a

Please sign in to comment.