Skip to content

Commit

Permalink
refactor: use configure() method for subclasses to configure the re…
Browse files Browse the repository at this point in the history
…pository (#41)
  • Loading branch information
priyadi authored Jun 21, 2024
1 parent 7e74432 commit 44478c2
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* cleanup: orderBy should be non-empty-array
* feat: initial version of basic repository
* refactor: consolidate repeated orderBy logic to `OrderByUtil`
* refactor: use `configure()` method for subclasses to configure the repository

## 0.3.0

Expand Down
63 changes: 34 additions & 29 deletions packages/collections-orm/src/AbstractBasicRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Doctrine\Common\Collections\Order;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
use Rekalogika\Collections\ORM\Configuration\BasicRepositoryConfiguration;
use Rekalogika\Collections\ORM\Trait\QueryBuilderTrait;
use Rekalogika\Contracts\Collections\BasicRepository;
use Rekalogika\Contracts\Collections\Exception\NotFoundException;
Expand Down Expand Up @@ -48,26 +49,43 @@ abstract class AbstractBasicRepository implements BasicRepository
/**
* @var non-empty-array<string,Order>
*/
private readonly array $orderBy;
private array $orderBy;

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

private CountStrategy $countStrategy = CountStrategy::Restrict;

final public function __construct(
private EntityManagerInterface $entityManager,
array|string|null $orderBy = null,
private readonly int $itemsPerPage = 50,
private readonly CountStrategy $countStrategy = CountStrategy::Restrict,
) {
// handle orderBy
$configuration = new BasicRepositoryConfiguration();
$this->configure($configuration);

// set configuration

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

$this->orderBy = OrderByUtil::normalizeOrderBy($orderBy);
// set query builder

$criteria = Criteria::create()->orderBy($this->orderBy);
$this->queryBuilder = $this->createQueryBuilder('e')->addCriteria($criteria);
}

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

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

/**
* @param null|non-empty-array<string,Order>|string $orderBy
* @param int<1,max> $itemsPerPage
Expand All @@ -78,27 +96,14 @@ protected function with(
?int $itemsPerPage = null,
?CountStrategy $countStrategy = null,
): static {
// @phpstan-ignore-next-line
return new static(
entityManager: $entityManager ?? $this->entityManager,
orderBy: $orderBy ?? $this->orderBy,
itemsPerPage: $itemsPerPage ?? $this->itemsPerPage,
countStrategy: $countStrategy ?? $this->countStrategy,
);
}
$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;

//
// mandatory
//

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

//
// misc
//
return $clone;
}

//
// accessors for subclasses
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/collections package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Collections\ORM\Configuration;

use Doctrine\Common\Collections\Order;
use Rekalogika\Domain\Collections\Common\CountStrategy;

class BasicRepositoryConfiguration
{
/**
* @var non-empty-array<string,Order>|string|null
*/
private array|string|null $orderBy = null;

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

private CountStrategy $countStrategy = CountStrategy::Restrict;

/**
* @return non-empty-array<string,Order>|string|null
*/
public function getOrderBy(): null|array|string
{
return $this->orderBy;
}

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

return $this;
}

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

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

return $this;
}

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

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

return $this;
}
}

0 comments on commit 44478c2

Please sign in to comment.