Skip to content

Commit

Permalink
chore: Deprecate Handlers methods
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Mar 14, 2024
1 parent 57a33f2 commit 9d0c560
Show file tree
Hide file tree
Showing 9 changed files with 446 additions and 85 deletions.
22 changes: 8 additions & 14 deletions src/EntityHandler/NodeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ public function getReverseNodesFromFieldName(string $fieldName): array
* @param Translation $translation
*
* @return null|NodesSources
* @deprecated Use Node::getNodeSourcesByTranslation() instead.
*/
public function getNodeSourceByTranslation($translation): ?NodesSources
{
Expand Down Expand Up @@ -397,17 +398,6 @@ public function archiveWithChildren(): self
return $this;
}

/**
* Return if is in Newsletter Node.
*
* @deprecated Just here not to break themes.
* @return bool
*/
public function isRelatedToNewsletter(): bool
{
return false;
}

/**
* Return if part of Node offspring.
*
Expand Down Expand Up @@ -538,6 +528,7 @@ public function cleanRootNodesPositions(bool $setPositions = true): float
* Return all node offspring id.
*
* @return array
* @deprecated Use NodeRepository::findAllOffspringIdByNode() instead.
*/
public function getAllOffspringId(): array
{
Expand Down Expand Up @@ -589,6 +580,7 @@ public function duplicate(): Node
*
* @return Node|null
* @throws NonUniqueResultException
* @deprecated Use NodeRepository::findPreviousNode() instead.
*/
public function getPrevious(
?array $criteria = null,
Expand Down Expand Up @@ -626,10 +618,12 @@ public function getPrevious(
/**
* Get next node from hierarchy.
*
* @param array|null $criteria
* @param array|null $order
* @param array|null $criteria
* @param array|null $order
*
* @return Node|null
* @throws NonUniqueResultException
* @deprecated Use NodeRepository::findNextNode() instead.
*/
public function getNext(
?array $criteria = null,
Expand Down Expand Up @@ -664,7 +658,7 @@ public function getNext(
/**
* @return NodeRepository
*/
public function getRepository(): NodeRepository
protected function getRepository(): NodeRepository
{
return $this->objectManager->getRepository(Node::class);
}
Expand Down
11 changes: 10 additions & 1 deletion src/EntityHandler/NodesSourcesHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public function addDocumentForField(
*
* @param string $fieldName Name of the node-type field
* @return array<Document>
* @deprecated Use directly NodesSources::getDocumentsByFieldsWithName
*/
public function getDocumentsFromFieldName(string $fieldName): array
{
Expand Down Expand Up @@ -168,7 +169,7 @@ public function getParent(): ?NodesSources
*
* @param array|null $criteria
* @return array<NodesSources>
* @throws \Doctrine\ORM\NonUniqueResultException
* @deprecated Use NodesSourcesRepository::findParents
*/
public function getParents(
array $criteria = null
Expand Down Expand Up @@ -213,6 +214,7 @@ public function getParents(
* @param array|null $order Non default ordering
*
* @return array<object|NodesSources>
* @deprecated Use TreeWalker or NodesSourcesRepository::findChildren
*/
public function getChildren(
array $criteria = null,
Expand Down Expand Up @@ -248,6 +250,7 @@ public function getChildren(
* @param array|null $order
*
* @return NodesSources|null
* @deprecated Use NodesSourcesRepository::findFirstChild
*/
public function getFirstChild(
array $criteria = null,
Expand Down Expand Up @@ -282,6 +285,7 @@ public function getFirstChild(
* @param array|null $order
*
* @return NodesSources|null
* @deprecated Use NodesSourcesRepository::findLastChild
*/
public function getLastChild(
array $criteria = null,
Expand Down Expand Up @@ -317,6 +321,7 @@ public function getLastChild(
* @param array|null $order
*
* @return NodesSources|null
* @deprecated Use NodesSourcesRepository::findFirstSibling
*/
public function getFirstSibling(
array $criteria = null,
Expand All @@ -339,6 +344,7 @@ public function getFirstSibling(
* @param array|null $order
*
* @return NodesSources|null
* @deprecated Use NodesSourcesRepository::findLastSibling
*/
public function getLastSibling(
array $criteria = null,
Expand All @@ -361,6 +367,7 @@ public function getLastSibling(
* @param array|null $order
*
* @return NodesSources|null
* @deprecated Use NodesSourcesRepository::findPrevious
*/
public function getPrevious(
array $criteria = null,
Expand Down Expand Up @@ -407,6 +414,7 @@ public function getPrevious(
* @param array|null $order
*
* @return NodesSources|null
* @deprecated Use NodesSourcesRepository::findNext
*/
public function getNext(
array $criteria = null,
Expand Down Expand Up @@ -446,6 +454,7 @@ public function getNext(
* Get node tags with current source translation.
*
* @return iterable<Tag>
* @deprecated Use TagRepository::findByNodesSources
*/
public function getTags(): iterable
{
Expand Down
16 changes: 4 additions & 12 deletions src/Message/Handler/ApplyRealmNodeInheritanceMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@

final class ApplyRealmNodeInheritanceMessageHandler implements MessageHandlerInterface
{
private ManagerRegistry $managerRegistry;
private HandlerFactoryInterface $handlerFactory;

public function __construct(ManagerRegistry $managerRegistry, HandlerFactoryInterface $handlerFactory)
public function __construct(private readonly ManagerRegistry $managerRegistry)
{
$this->managerRegistry = $managerRegistry;
$this->handlerFactory = $handlerFactory;
}

public function __invoke(ApplyRealmNodeInheritanceMessage $message): void
Expand Down Expand Up @@ -53,15 +48,12 @@ public function __invoke(ApplyRealmNodeInheritanceMessage $message): void
return;
}

/** @var NodeHandler $nodeHandler */
$nodeHandler = $this->handlerFactory->getHandler($node);
$childrenIds = $nodeHandler->getAllOffspringId();
$nodeRepository = $this->managerRegistry->getRepository(Node::class);
$childrenIds = $nodeRepository->findAllOffspringIdByNode($node);

foreach ($childrenIds as $childId) {
/** @var Node|null $child */
$child = $this->managerRegistry
->getRepository(Node::class)
->find($childId);
$child = $nodeRepository->find($childId);
if (null === $child) {
continue;
}
Expand Down
12 changes: 3 additions & 9 deletions src/Message/Handler/CleanRealmNodeInheritanceMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@

final class CleanRealmNodeInheritanceMessageHandler implements MessageHandlerInterface
{
private ManagerRegistry $managerRegistry;
private HandlerFactoryInterface $handlerFactory;

public function __construct(ManagerRegistry $managerRegistry, HandlerFactoryInterface $handlerFactory)
public function __construct(private readonly ManagerRegistry $managerRegistry)
{
$this->managerRegistry = $managerRegistry;
$this->handlerFactory = $handlerFactory;
}

public function __invoke(CleanRealmNodeInheritanceMessage $message): void
Expand All @@ -40,9 +35,8 @@ public function __invoke(CleanRealmNodeInheritanceMessage $message): void
throw new UnrecoverableMessageHandlingException('Realm does not exist');
}

/** @var NodeHandler $nodeHandler */
$nodeHandler = $this->handlerFactory->getHandler($node);
$childrenIds = $nodeHandler->getAllOffspringId();
$nodeRepository = $this->managerRegistry->getRepository(Node::class);
$childrenIds = $nodeRepository->findAllOffspringIdByNode($node);

$realmNodes = $this->managerRegistry
->getRepository(RealmNode::class)
Expand Down
70 changes: 70 additions & 0 deletions src/Repository/NodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -1159,4 +1159,74 @@ protected function classicLikeComparison(
$qb->orWhere($qb->expr()->like('LOWER(' . $alias . '.nodeName)', $qb->expr()->literal($value)));
return $qb;
}

/**
* Get previous node from hierarchy
*/
public function findPreviousNode(
Node $node,
?array $criteria = null,
?array $order = null
): ?Node {
if ($node->getPosition() <= 1) {
return null;
}
if (null === $order) {
$order = [];
}

if (null === $criteria) {
$criteria = [];
}

$criteria['parent'] = $node->getParent();
/*
* Use < operator to get first previous nodeSource
* even if it’s not the previous position index
*/
$criteria['position'] = [
'<',
$node->getPosition(),
];

$order['position'] = 'DESC';

return $this->findOneBy(
$criteria,
$order
);
}

/**
* Get next node from hierarchy.
*/
public function findNextNode(
Node $node,
?array $criteria = null,
?array $order = null
): ?Node {
if (null === $criteria) {
$criteria = [];
}
if (null === $order) {
$order = [];
}

$criteria['parent'] = $node->getParent();

/*
* Use > operator to get first next nodeSource
* even if it’s not the next position index
*/
$criteria['position'] = [
'>',
$node->getPosition(),
];
$order['position'] = 'ASC';

return $this->findOneBy(
$criteria,
$order
);
}
}
Loading

0 comments on commit 9d0c560

Please sign in to comment.