Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(AbstractBasicRepository): change configuration method #48

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* fix(`BasicRepository`): `remove()` should be `removeElement()`
* feat(`BasicRepository`): add `remove()` method
* test: add basic repository test
* refactor(`AbstractBasicRepository`): change configuration method

## 0.3.0

Expand Down
55 changes: 24 additions & 31 deletions packages/collections-orm/src/AbstractBasicRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,69 +42,62 @@ abstract class AbstractBasicRepository implements BasicRepository
*/
use PageableTrait;

private QueryBuilder $queryBuilder;

private ?int $count = 0;

/**
* @var non-empty-array<string,Order>
* @var int<1,max>
*/
private array $orderBy;
private int $itemsPerPage;

private CountStrategy $countStrategy;
private QueryBuilder $queryBuilder;

/**
* @var int<1,max>
* @var class-string<T>
*/
private int $itemsPerPage = 50;

private CountStrategy $countStrategy = CountStrategy::Restrict;
private string $class;

final public function __construct(
private EntityManagerInterface $entityManager,
) {
$configuration = new BasicRepositoryConfiguration();
$this->configure($configuration);

// set configuration

$this->orderBy = OrderByUtil::normalizeOrderBy($configuration->getOrderBy());
$configuration = $this->configure();
$this->class = $configuration->getClass();
$this->itemsPerPage = $configuration->getItemsPerPage();
$this->countStrategy = $configuration->getCountStrategy();

// set query builder

$criteria = Criteria::create()->orderBy($this->orderBy);
$this->queryBuilder = $this->createQueryBuilder('e')->addCriteria($criteria);
$criteria = Criteria::create()->orderBy($configuration->getOrderBy());
$this->queryBuilder = $this
->createQueryBuilder('e', 'e.' . $configuration->getIndexBy())
->addCriteria($criteria);
}

/**
* @return class-string<T>
* @return BasicRepositoryConfiguration<T>
*/
abstract protected function getClass(): string;

protected function configure(BasicRepositoryConfiguration $configuration): void
{
// override this method to configure the repository
}
abstract protected function configure(): BasicRepositoryConfiguration;

/**
* @param null|non-empty-array<string,Order>|string $orderBy
* @param int<1,max> $itemsPerPage
*/
protected function with(
null|EntityManagerInterface $entityManager = null,
array|string|null $orderBy = null,
?int $itemsPerPage = null,
?CountStrategy $countStrategy = null,
): static {
$clone = clone $this;
$clone->entityManager = $entityManager ?? $this->entityManager;
$clone->orderBy = OrderByUtil::normalizeOrderBy($orderBy ?? $this->orderBy);
$clone->itemsPerPage = $itemsPerPage ?? $this->itemsPerPage;
$clone->countStrategy = $countStrategy ?? $this->countStrategy;

return $clone;
}

/**
* @return class-string<T>
*/
private function getClass(): string
{
/** @var class-string<T> */
return $this->class;
}

//
// accessors for subclasses
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,67 +14,89 @@
namespace Rekalogika\Collections\ORM\Configuration;

use Doctrine\Common\Collections\Order;
use Rekalogika\Contracts\Collections\Exception\InvalidArgumentException;
use Rekalogika\Domain\Collections\Common\CountStrategy;
use Rekalogika\Domain\Collections\Common\Internal\OrderByUtil;

class BasicRepositoryConfiguration
/**
* @template T of object
*/
final readonly class BasicRepositoryConfiguration
{
/**
* @var non-empty-array<string,Order>|string|null
* @var non-empty-array<string,Order>
*/
private array|string|null $orderBy = null;
private array $orderBy;

/**
* @var int<1,max>
* @param class-string<T> $class
* @param null|non-empty-array<string,Order>|string $orderBy
* @param int<1,max> $itemsPerPage
*/
private int $itemsPerPage = 50;

private CountStrategy $countStrategy = CountStrategy::Restrict;
public function __construct(
private string $class,
private string $indexBy = 'id',
array|string|null $orderBy = null,
private int $itemsPerPage = 50,
private CountStrategy $countStrategy = CountStrategy::Restrict,
) {
$this->orderBy = OrderByUtil::normalizeOrderBy($orderBy);

if ($countStrategy === CountStrategy::Provided) {
throw new InvalidArgumentException('CountStrategy::Provided is not supported in repositories');
}
}

/**
* @return non-empty-array<string,Order>|string|null
* @param null|non-empty-array<string,Order>|string $orderBy
* @param null|int<1,max> $itemsPerPage
*/
public function getOrderBy(): null|array|string
public function with(
?string $indexBy = null,
array|string|null $orderBy = null,
?int $itemsPerPage = null,
?CountStrategy $countStrategy = null,
): static {
return new static(
$this->class,
$indexBy ?? $this->indexBy,
$orderBy ?? $this->orderBy,
$itemsPerPage ?? $this->itemsPerPage,
$countStrategy ?? $this->countStrategy,
);
}

public function getIndexBy(): string
{
return $this->orderBy;
return $this->indexBy;
}

/**
* @param non-empty-array<string,Order>|string $orderBy
* @return class-string<T>
*/
public function setOrderBy($orderBy): self
public function getClass(): string
{
$this->orderBy = $orderBy;

return $this;
return $this->class;
}

/**
* @return int<1,max>
* @return non-empty-array<string,Order>
*/
public function getItemsPerPage(): int
public function getOrderBy(): array
{
return $this->itemsPerPage;
return $this->orderBy;
}

/**
* @param int<1,max> $itemsPerPage
* @return int<1,max>
*/
public function setItemsPerPage(int $itemsPerPage): self
public function getItemsPerPage(): int
{
$this->itemsPerPage = $itemsPerPage;

return $this;
return $this->itemsPerPage;
}

public function getCountStrategy(): CountStrategy
{
return $this->countStrategy;
}

public function setCountStrategy(CountStrategy $countStrategy): self
{
$this->countStrategy = $countStrategy;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

namespace Rekalogika\Collections\Tests\App\BasicRepository\Implementation;

use Doctrine\Common\Collections\Order;
use Rekalogika\Collections\ORM\AbstractBasicRepository;
use Rekalogika\Collections\ORM\Configuration\BasicRepositoryConfiguration;
use Rekalogika\Collections\Tests\App\BasicRepository\CitizenBasicRepository;
use Rekalogika\Collections\Tests\App\Entity\Citizen;

Expand All @@ -22,8 +24,12 @@
*/
class DefaultCitizenBasicRepository extends AbstractBasicRepository implements CitizenBasicRepository
{
protected function getClass(): string
protected function configure(): BasicRepositoryConfiguration
{
return Citizen::class;
return new BasicRepositoryConfiguration(
class: Citizen::class,
indexBy: 'id',
orderBy: ['id' => Order::Ascending]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

namespace Rekalogika\Collections\Tests\App\BasicRepository\Implementation;

use Doctrine\Common\Collections\Order;
use Rekalogika\Collections\ORM\AbstractBasicRepository;
use Rekalogika\Collections\ORM\Configuration\BasicRepositoryConfiguration;
use Rekalogika\Collections\Tests\App\Entity\Country;
use Rekalogika\Collections\Tests\App\BasicRepository\CountryBasicRepository;

Expand All @@ -22,8 +24,12 @@
*/
class DefaultCountryBasicRepository extends AbstractBasicRepository implements CountryBasicRepository
{
protected function getClass(): string
protected function configure(): BasicRepositoryConfiguration
{
return Country::class;
return new BasicRepositoryConfiguration(
class: Country::class,
indexBy: 'id',
orderBy: ['id' => Order::Ascending]
);
}
}