Skip to content

Commit

Permalink
feat(Doctrine): Roadiz repository extends ServiceEntityRepository. …
Browse files Browse the repository at this point in the history
…Removed `Paginator` return type from all `findBy` and `searchBy` methods.
  • Loading branch information
ambroisemaupate committed Jun 7, 2024
1 parent e8e01de commit b7a91a9
Show file tree
Hide file tree
Showing 21 changed files with 205 additions and 286 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,67 @@

namespace RZ\Roadiz\CoreBundle\Api\TreeWalker\Definition;

use Doctrine\ORM\Tools\Pagination\Paginator;
use RZ\Roadiz\CoreBundle\Api\TreeWalker\NodeSourceWalkerContext;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Entity\NodeType;
use RZ\TreeWalker\Definition\ContextualDefinitionTrait;
use RZ\TreeWalker\WalkerContextInterface;

final class MultiTypeChildrenDefinition
{
use ContextualDefinitionTrait;

private array $types;
private bool $onlyVisible;

/**
* @param WalkerContextInterface $context
* @param array<string> $types
* @param bool $onlyVisible
*/
public function __construct(WalkerContextInterface $context, array $types, bool $onlyVisible = true)
{
public function __construct(
WalkerContextInterface $context,
private readonly array $types,
private readonly bool $onlyVisible = true
) {
$this->context = $context;
$this->types = $types;
$this->onlyVisible = $onlyVisible;
}

/**
* @param NodesSources $source
* @return array|Paginator
* @return array<NodesSources>
*/
public function __invoke(NodesSources $source)
public function __invoke(NodesSources $source): array
{
if ($this->context instanceof NodeSourceWalkerContext) {
$this->context->getStopwatch()->start(self::class);
$bag = $this->context->getNodeTypesBag();
$criteria = [
'node.parent' => $source->getNode(),
'translation' => $source->getTranslation(),
'node.nodeType' => array_map(function (string $singleType) use ($bag) {
return $bag->get($singleType);
}, $this->types)
];
if ($this->onlyVisible) {
$criteria['node.visible'] = true;
}
$children = $this->context->getNodeSourceApi()->getBy($criteria, [
if (!($this->context instanceof NodeSourceWalkerContext)) {
throw new \InvalidArgumentException('Context should be instance of ' . NodeSourceWalkerContext::class);
}

$this->context->getStopwatch()->start(self::class);
$bag = $this->context->getNodeTypesBag();
/** @var NodeType[] $nodeTypes */
$nodeTypes = array_map(function (string $singleType) use ($bag) {
return $bag->get($singleType);
}, $this->types);
$criteria = [
'node.parent' => $source->getNode(),
'translation' => $source->getTranslation(),
'node.nodeType' => $nodeTypes,
];
if ($this->onlyVisible) {
$criteria['node.visible'] = true;
}
if (count($nodeTypes) === 1) {
$entityName = $nodeTypes[0]->getSourceEntityFullQualifiedClassName();
} else {
$entityName = NodesSources::class;
}
// @phpstan-ignore-next-line
$children = $this->context
->getManagerRegistry()
->getRepository($entityName)
->findBy($criteria, [
'node.position' => 'ASC',
]);
$this->context->getStopwatch()->stop(self::class);
$this->context->getStopwatch()->stop(self::class);

return $children;
}
throw new \InvalidArgumentException('Context should be instance of ' . NodeSourceWalkerContext::class);
return $children;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace RZ\Roadiz\CoreBundle\Api\TreeWalker\Definition;

use ArrayIterator;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Exception;
use RZ\Roadiz\CoreBundle\Api\TreeWalker\NodeSourceWalkerContext;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
Expand All @@ -27,31 +25,27 @@ public function __construct(bool $onlyVisible = true)
*/
public function __invoke(NodesSources $source): array
{
if ($this->context instanceof NodeSourceWalkerContext) {
$this->context->getStopwatch()->start(self::class);
$criteria = [
'node.parent' => $source->getNode(),
'translation' => $source->getTranslation(),
'node.nodeType.reachable' => false,
];
if ($this->onlyVisible) {
$criteria['node.visible'] = true;
}
$children = $this->context->getNodeSourceApi()->getBy($criteria, [
if (!($this->context instanceof NodeSourceWalkerContext)) {
throw new \InvalidArgumentException('Context should be instance of ' . NodeSourceWalkerContext::class);
}

$this->context->getStopwatch()->start(self::class);
$criteria = [
'node.parent' => $source->getNode(),
'translation' => $source->getTranslation(),
'node.nodeType.reachable' => false,
];
if ($this->onlyVisible) {
$criteria['node.visible'] = true;
}
// @phpstan-ignore-next-line
$children = $this->context->getManagerRegistry()
->getRepository(NodesSources::class)
->findBy($criteria, [
'node.position' => 'ASC',
]);
$this->context->getStopwatch()->stop(self::class);

if ($children instanceof Paginator) {
$iterator = $children->getIterator();
if ($iterator instanceof ArrayIterator) {
return $iterator->getArrayCopy();
}
// @phpstan-ignore-next-line
return iterator_to_array($iterator);
}
return $children;
}
throw new \InvalidArgumentException('Context should be instance of ' . NodeSourceWalkerContext::class);
$this->context->getStopwatch()->stop(self::class);

return $children;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace RZ\Roadiz\CoreBundle\Api\TreeWalker\Definition;

use ArrayIterator;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Exception;
use RZ\Roadiz\CoreBundle\Api\TreeWalker\NodeSourceWalkerContext;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
Expand All @@ -27,31 +25,27 @@ public function __construct(bool $onlyVisible = true)
*/
public function __invoke(NodesSources $source): array
{
if ($this->context instanceof NodeSourceWalkerContext) {
$this->context->getStopwatch()->start(self::class);
$criteria = [
'node.parent' => $source->getNode(),
'translation' => $source->getTranslation(),
'node.nodeType.reachable' => true,
];
if ($this->onlyVisible) {
$criteria['node.visible'] = true;
}
$children = $this->context->getNodeSourceApi()->getBy($criteria, [
if (!($this->context instanceof NodeSourceWalkerContext)) {
throw new \InvalidArgumentException('Context should be instance of ' . NodeSourceWalkerContext::class);
}

$this->context->getStopwatch()->start(self::class);
$criteria = [
'node.parent' => $source->getNode(),
'translation' => $source->getTranslation(),
'node.nodeType.reachable' => true,
];
if ($this->onlyVisible) {
$criteria['node.visible'] = true;
}
// @phpstan-ignore-next-line
$children = $this->context->getManagerRegistry()
->getRepository(NodesSources::class)
->findBy($criteria, [
'node.position' => 'ASC',
]);
$this->context->getStopwatch()->stop(self::class);

if ($children instanceof Paginator) {
$iterator = $children->getIterator();
if ($iterator instanceof ArrayIterator) {
return $iterator->getArrayCopy();
}
// @phpstan-ignore-next-line
return iterator_to_array($iterator);
}
return $children;
}
throw new \InvalidArgumentException('Context should be instance of ' . NodeSourceWalkerContext::class);
$this->context->getStopwatch()->stop(self::class);

return $children;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,16 @@

class NodeSourceWalkerContext implements WalkerContextInterface
{
private Stopwatch $stopwatch;
private NodeTypes $nodeTypesBag;
private NodeSourceApi $nodeSourceApi;
private RequestStack $requestStack;
private ManagerRegistry $managerRegistry;
private CacheItemPoolInterface $cacheAdapter;
private NodeTypeResolver $nodeTypeResolver;
private PreviewResolverInterface $previewResolver;

public function __construct(
Stopwatch $stopwatch,
NodeTypes $nodeTypesBag,
NodeSourceApi $nodeSourceApi,
RequestStack $requestStack,
ManagerRegistry $managerRegistry,
CacheItemPoolInterface $cacheAdapter,
NodeTypeResolver $nodeTypeResolver,
PreviewResolverInterface $previewResolver
private readonly Stopwatch $stopwatch,
private readonly NodeTypes $nodeTypesBag,
private readonly NodeSourceApi $nodeSourceApi,
private readonly RequestStack $requestStack,
private readonly ManagerRegistry $managerRegistry,
private readonly CacheItemPoolInterface $cacheAdapter,
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly PreviewResolverInterface $previewResolver
) {
$this->stopwatch = $stopwatch;
$this->nodeTypesBag = $nodeTypesBag;
$this->nodeSourceApi = $nodeSourceApi;
$this->requestStack = $requestStack;
$this->managerRegistry = $managerRegistry;
$this->cacheAdapter = $cacheAdapter;
$this->nodeTypeResolver = $nodeTypeResolver;
$this->previewResolver = $previewResolver;
}

/**
Expand All @@ -65,6 +48,7 @@ public function getNodeTypesBag(): NodeTypes

/**
* @return NodeSourceApi
* @deprecated Use getManagerRegistry
*/
public function getNodeSourceApi(): NodeSourceApi
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,16 @@

final class NodeSourceWalkerContextFactory implements WalkerContextFactoryInterface
{
private Stopwatch $stopwatch;
private NodeTypes $nodeTypesBag;
private NodeSourceApi $nodeSourceApi;
private RequestStack $requestStack;
private ManagerRegistry $managerRegistry;
private CacheItemPoolInterface $cacheAdapter;
private NodeTypeResolver $nodeTypeResolver;
private PreviewResolverInterface $previewResolver;

public function __construct(
Stopwatch $stopwatch,
NodeTypes $nodeTypesBag,
NodeSourceApi $nodeSourceApi,
RequestStack $requestStack,
ManagerRegistry $managerRegistry,
CacheItemPoolInterface $cacheAdapter,
NodeTypeResolver $nodeTypeResolver,
PreviewResolverInterface $previewResolver
private readonly Stopwatch $stopwatch,
private readonly NodeTypes $nodeTypesBag,
private readonly NodeSourceApi $nodeSourceApi,
private readonly RequestStack $requestStack,
private readonly ManagerRegistry $managerRegistry,
private readonly CacheItemPoolInterface $cacheAdapter,
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly PreviewResolverInterface $previewResolver
) {
$this->stopwatch = $stopwatch;
$this->nodeTypesBag = $nodeTypesBag;
$this->nodeSourceApi = $nodeSourceApi;
$this->requestStack = $requestStack;
$this->managerRegistry = $managerRegistry;
$this->cacheAdapter = $cacheAdapter;
$this->nodeTypeResolver = $nodeTypeResolver;
$this->previewResolver = $previewResolver;
}

public function createWalkerContext(): WalkerContextInterface
Expand Down
17 changes: 4 additions & 13 deletions lib/RoadizCoreBundle/src/Api/TreeWalker/TreeWalkerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,17 @@

final class TreeWalkerGenerator
{
private NodeSourceApi $nodeSourceApi;
private NodeTypes $nodeTypesBag;
private WalkerContextInterface $walkerContext;
private CacheItemPoolInterface $cacheItemPool;

/**
* @var array<class-string, DefinitionFactoryConfiguration>
*/
private array $walkerDefinitionFactories = [];

public function __construct(
NodeSourceApi $nodeSourceApi,
NodeTypes $nodeTypesBag,
WalkerContextInterface $walkerContext,
CacheItemPoolInterface $cacheItemPool
private readonly NodeSourceApi $nodeSourceApi,
private readonly NodeTypes $nodeTypesBag,
private readonly WalkerContextInterface $walkerContext,
private readonly CacheItemPoolInterface $cacheItemPool
) {
$this->nodeSourceApi = $nodeSourceApi;
$this->nodeTypesBag = $nodeTypesBag;
$this->walkerContext = $walkerContext;
$this->cacheItemPool = $cacheItemPool;
}

/**
Expand Down
5 changes: 2 additions & 3 deletions lib/RoadizCoreBundle/src/ListManager/EntityListManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace RZ\Roadiz\CoreBundle\ListManager;

use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
use Doctrine\Persistence\ObjectManager;
use RZ\Roadiz\Core\AbstractEntities\PersistableInterface;
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
Expand Down Expand Up @@ -212,9 +211,9 @@ public function getPageCount(): int
/**
* Return filtered entities.
*
* @return array<T>|DoctrinePaginator<T>
* @return array<T>
*/
public function getEntities(): array|DoctrinePaginator
public function getEntities(): array
{
if ($this->pagination === true && null !== $this->paginator) {
$this->paginator->setItemsPerPage($this->getItemPerPage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace RZ\Roadiz\CoreBundle\ListManager;

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

interface EntityListManagerInterface
{
public const ITEM_PER_PAGE = 20;
Expand Down Expand Up @@ -104,9 +102,9 @@ public function getPageCount(): int;
/**
* Return filtered entities.
*
* @return array|DoctrinePaginator
* @return array
*/
public function getEntities(): array|DoctrinePaginator;
public function getEntities(): array;

/**
* Configure a custom item count per page.
Expand Down
3 changes: 1 addition & 2 deletions lib/RoadizCoreBundle/src/ListManager/NodePaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace RZ\Roadiz\CoreBundle\ListManager;

use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Repository\NodeRepository;
Expand Down Expand Up @@ -33,7 +32,7 @@ public function setTranslation(TranslationInterface $translation = null): self
return $this;
}

public function findByAtPage(array $order = [], int $page = 1): array|DoctrinePaginator
public function findByAtPage(array $order = [], int $page = 1): array
{
if (null !== $this->searchPattern) {
return $this->searchByAtPage($order, $page);
Expand Down
Loading

0 comments on commit b7a91a9

Please sign in to comment.