Skip to content

Commit

Permalink
chore: Improved ListManager template and method return types
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Jun 2, 2024
1 parent a3cf412 commit 6c11340
Show file tree
Hide file tree
Showing 18 changed files with 123 additions and 185 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"ext-json": "*",
"ext-openssl": "*",
"ext-simplexml": "*",
"ext-mbstring": "*",
"ext-zip": "*",
"api-platform/core": "~3.2.14",
"codercat/jwk-to-pem": "^1.0",
Expand Down
1 change: 1 addition & 0 deletions lib/RoadizCoreBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"ext-iconv": "*",
"ext-zip": "*",
"ext-json": "*",
"ext-mbstring": "*",
"api-platform/core": "~3.2.14",
"doctrine/annotations": "^1.0",
"doctrine/doctrine-bundle": "^2.8.1",
Expand Down
8 changes: 1 addition & 7 deletions lib/RoadizCoreBundle/src/Api/ListManager/SolrPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@
namespace RZ\Roadiz\CoreBundle\Api\ListManager;

use ApiPlatform\State\Pagination\PaginatorInterface;
use Doctrine\Common\Collections\ArrayCollection;

final class SolrPaginator implements PaginatorInterface, \IteratorAggregate
{
private bool $handled = false;
private SolrSearchListManager $listManager;

/**
* @param SolrSearchListManager $listManager
*/
public function __construct(SolrSearchListManager $listManager)
public function __construct(private readonly SolrSearchListManager $listManager)
{
$this->listManager = $listManager;
}

protected function handleOnce(): void
Expand Down
14 changes: 4 additions & 10 deletions lib/RoadizCoreBundle/src/Api/ListManager/SolrSearchListManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,19 @@

final class SolrSearchListManager extends AbstractEntityListManager
{
protected SearchHandlerInterface $searchHandler;
protected ?SearchResultsInterface $searchResults;
private array $criteria;
private bool $searchInTags;
private ?string $query = null;

public function __construct(
?Request $request,
SearchHandlerInterface $searchHandler,
array $criteria = [],
bool $searchInTags = true
private readonly SearchHandlerInterface $searchHandler,
private readonly array $criteria = [],
private readonly bool $searchInTags = true
) {
parent::__construct($request);
$this->searchHandler = $searchHandler;
$this->criteria = $criteria;
$this->searchInTags = $searchInTags;
}

public function handle(bool $disabled = false)
public function handle(bool $disabled = false): void
{
if ($this->request === null) {
throw new \InvalidArgumentException('Cannot handle a NULL request.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ protected function doFetchItems(array $options = []): EntityListManagerInterface
$this->configureOptions($resolver);
$this->options = $resolver->resolve($options);

/** @var EntityListManager<PersistableInterface> $listManager */
$listManager = new EntityListManager(
$this->requestStack->getCurrentRequest(),
$this->managerRegistry->getManager(),
Expand Down
40 changes: 9 additions & 31 deletions lib/RoadizCoreBundle/src/ListManager/AbstractEntityListManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

abstract class AbstractEntityListManager implements EntityListManagerInterface
{
protected ?Request $request = null;
protected bool $pagination = true;
protected ?array $queryArray = null;
protected ?int $currentPage = null;
Expand All @@ -19,12 +18,8 @@ abstract class AbstractEntityListManager implements EntityListManagerInterface
protected bool $allowRequestSorting = true;
protected bool $allowRequestSearching = true;

/**
* @param Request|null $request
*/
public function __construct(?Request $request)
public function __construct(protected readonly ?Request $request = null)
{
$this->request = $request;
$this->displayNotPublishedNodes = false;
$this->displayAllNodesStatuses = false;
if (null !== $request) {
Expand All @@ -35,13 +30,13 @@ public function __construct(?Request $request)
$this->itemPerPage = static::ITEM_PER_PAGE;
}

public function setAllowRequestSorting(bool $allowRequestSorting)
public function setAllowRequestSorting(bool $allowRequestSorting): self
{
$this->allowRequestSorting = $allowRequestSorting;
return $this;
}

public function setAllowRequestSearching(bool $allowRequestSearching)
public function setAllowRequestSearching(bool $allowRequestSearching): self
{
$this->allowRequestSearching = $allowRequestSearching;
return $this;
Expand All @@ -55,11 +50,7 @@ public function isDisplayingNotPublishedNodes(): bool
return $this->displayNotPublishedNodes;
}

/**
* @param bool $displayNotPublishedNodes
* @return EntityListManagerInterface
*/
public function setDisplayingNotPublishedNodes(bool $displayNotPublishedNodes)
public function setDisplayingNotPublishedNodes(bool $displayNotPublishedNodes): self
{
$this->displayNotPublishedNodes = $displayNotPublishedNodes;
return $this;
Expand All @@ -76,11 +67,8 @@ public function isDisplayingAllNodesStatuses(): bool
/**
* Switch repository to disable any security on Node status. To use ONLY in order to
* view deleted and archived nodes.
*
* @param bool $displayAllNodesStatuses
* @return EntityListManagerInterface
*/
public function setDisplayingAllNodesStatuses(bool $displayAllNodesStatuses)
public function setDisplayingAllNodesStatuses(bool $displayAllNodesStatuses): self
{
$this->displayAllNodesStatuses = $displayAllNodesStatuses;
return $this;
Expand All @@ -89,7 +77,7 @@ public function setDisplayingAllNodesStatuses(bool $displayAllNodesStatuses)
/**
* @inheritDoc
*/
public function setPage(int $page)
public function setPage(int $page): self
{
if ($page < 1) {
throw new \RuntimeException("Page cannot be lesser than 1.", 1);
Expand All @@ -107,19 +95,13 @@ protected function getPage(): int
return $this->currentPage;
}

/**
* @return EntityListManagerInterface
*/
public function enablePagination()
public function enablePagination(): self
{
$this->pagination = true;
return $this;
}

/**
* @inheritDoc
*/
public function disablePagination()
public function disablePagination(): self
{
$this->setPage(1);
$this->pagination = false;
Expand Down Expand Up @@ -192,12 +174,8 @@ protected function getItemPerPage(): int

/**
* Configure a custom item count per page.
*
* @param int $itemPerPage
*
* @return EntityListManagerInterface
*/
public function setItemPerPage(int $itemPerPage)
public function setItemPerPage(int $itemPerPage): self
{
if ($itemPerPage < 1) {
throw new \RuntimeException("Item count per page cannot be lesser than 1.", 1);
Expand Down
58 changes: 27 additions & 31 deletions lib/RoadizCoreBundle/src/ListManager/EntityListManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,41 @@
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Entity\NodeType;
use RZ\Roadiz\CoreBundle\Repository\NodeRepository;
use RZ\Roadiz\CoreBundle\Repository\StatusAwareRepository;
use Symfony\Component\DependencyInjection\Attribute\Exclude;
use Symfony\Component\HttpFoundation\Request;

/**
* Perform basic filtering and search over entity listings.
*
* @template T of PersistableInterface
*/
#[Exclude]
class EntityListManager extends AbstractEntityListManager
{
/**
* @var class-string<PersistableInterface>
* @var Paginator<T>|null
*/
protected string $entityName;
protected ObjectManager $entityManager;
protected ?Paginator $paginator = null;
protected ?array $orderingArray = null;
protected ?array $filteringArray = null;
protected ?array $assignation = null;
protected ?TranslationInterface $translation = null;

/**
* @param Request|null $request
* @param ObjectManager $entityManager
* @param class-string<PersistableInterface> $entityName
* @param array $preFilters
* @param array $preOrdering
* @param class-string<T> $entityName
* @param array $filteringArray
* @param array $orderingArray
*/
public function __construct(
?Request $request,
ObjectManager $entityManager,
string $entityName,
array $preFilters = [],
array $preOrdering = []
protected readonly ObjectManager $entityManager,
protected readonly string $entityName,
protected array $filteringArray = [],
protected array $orderingArray = []
) {
parent::__construct($request);
$this->entityName = $entityName;
$this->entityManager = $entityManager;
$this->orderingArray = $preOrdering;
$this->filteringArray = $preFilters;
$this->assignation = [];
}

Expand All @@ -65,7 +60,7 @@ public function getTranslation(): ?TranslationInterface
* @param TranslationInterface|null $translation
* @return $this
*/
public function setTranslation(TranslationInterface $translation = null)
public function setTranslation(TranslationInterface $translation = null): self
{
$this->translation = $translation;

Expand All @@ -77,8 +72,9 @@ public function setTranslation(TranslationInterface $translation = null)
*
* @param bool $disabled Disable pagination and filtering over GET params
* @return void
* @throws \ReflectionException
*/
public function handle(bool $disabled = false)
public function handle(bool $disabled = false): void
{
// transform the key chroot in parent
if (array_key_exists('chroot', $this->filteringArray)) {
Expand Down Expand Up @@ -142,14 +138,15 @@ protected function handleOrderingParam(string $field, string $ordering): void
}


/**
* @throws \ReflectionException
*/
protected function createPaginator(): void
{
if (
$this->entityName === Node::class ||
$this->entityName === 'RZ\Roadiz\CoreBundle\Entity\Node' ||
$this->entityName === '\RZ\Roadiz\CoreBundle\Entity\Node' ||
$this->entityName === "Node"
) {
$reflectionClass = new \ReflectionClass($this->entityName);

if ($this->entityName === Node::class) {
// @phpstan-ignore-next-line
$this->paginator = new NodePaginator(
$this->entityManager,
$this->entityName,
Expand All @@ -158,14 +155,13 @@ protected function createPaginator(): void
);
$this->paginator->setTranslation($this->translation);
} elseif (
$this->entityName == NodesSources::class ||
$this->entityName == 'RZ\Roadiz\CoreBundle\Entity\NodesSources' ||
$this->entityName == '\RZ\Roadiz\CoreBundle\Entity\NodesSources' ||
$this->entityName == "NodesSources" ||
str_contains($this->entityName, NodeType::getGeneratedEntitiesNamespace())
$this->entityName === NodesSources::class ||
$reflectionClass->isSubclassOf(NodesSources::class)
) {
// @phpstan-ignore-next-line
$this->paginator = new NodesSourcesPaginator(
$this->entityManager,
// @phpstan-ignore-next-line
$this->entityName,
$this->itemPerPage,
$this->filteringArray
Expand Down Expand Up @@ -216,7 +212,7 @@ public function getPageCount(): int
/**
* Return filtered entities.
*
* @return array|DoctrinePaginator
* @return array<T>|DoctrinePaginator<T>
*/
public function getEntities(): array|DoctrinePaginator
{
Expand Down
22 changes: 12 additions & 10 deletions lib/RoadizCoreBundle/src/ListManager/EntityListManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace RZ\Roadiz\CoreBundle\ListManager;

use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;

interface EntityListManagerInterface
{
public const ITEM_PER_PAGE = 20;
Expand All @@ -12,13 +14,13 @@ interface EntityListManagerInterface
* @param bool $allowRequestSorting
* @return $this
*/
public function setAllowRequestSorting(bool $allowRequestSorting);
public function setAllowRequestSorting(bool $allowRequestSorting): self;

/**
* @param bool $allowRequestSearching
* @return $this
*/
public function setAllowRequestSearching(bool $allowRequestSearching);
public function setAllowRequestSearching(bool $allowRequestSearching): self;

/**
* @return bool
Expand All @@ -29,7 +31,7 @@ public function isDisplayingNotPublishedNodes(): bool;
* @param bool $displayNotPublishedNodes
* @return EntityListManagerInterface
*/
public function setDisplayingNotPublishedNodes(bool $displayNotPublishedNodes);
public function setDisplayingNotPublishedNodes(bool $displayNotPublishedNodes): self;

/**
* @return bool
Expand All @@ -43,15 +45,15 @@ public function isDisplayingAllNodesStatuses(): bool;
* @param bool $displayAllNodesStatuses
* @return EntityListManagerInterface
*/
public function setDisplayingAllNodesStatuses(bool $displayAllNodesStatuses);
public function setDisplayingAllNodesStatuses(bool $displayAllNodesStatuses): self;

/**
* Handle request to find filter to apply to entity listing.
*
* @param bool $disabled Disable pagination and filtering over GET params
* @return void
*/
public function handle(bool $disabled = false);
public function handle(bool $disabled = false): void;

/**
* Configure a custom current page.
Expand All @@ -60,12 +62,12 @@ public function handle(bool $disabled = false);
*
* @return EntityListManagerInterface
*/
public function setPage(int $page);
public function setPage(int $page): self;

/**
* @return EntityListManagerInterface
*/
public function disablePagination();
public function disablePagination(): self;

/**
* Get Twig assignation to render list details.
Expand Down Expand Up @@ -102,9 +104,9 @@ public function getPageCount(): int;
/**
* Return filtered entities.
*
* @return array|\Doctrine\ORM\Tools\Pagination\Paginator
* @return array|DoctrinePaginator
*/
public function getEntities();
public function getEntities(): array|DoctrinePaginator;

/**
* Configure a custom item count per page.
Expand All @@ -113,5 +115,5 @@ public function getEntities();
*
* @return EntityListManagerInterface
*/
public function setItemPerPage(int $itemPerPage);
public function setItemPerPage(int $itemPerPage): self;
}
Loading

0 comments on commit 6c11340

Please sign in to comment.