Skip to content

Commit

Permalink
Merge branch feature/doctrinelisteners into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Jun 1, 2024
1 parent 7342e35 commit 52dc8fe
Showing 1 changed file with 34 additions and 50 deletions.
84 changes: 34 additions & 50 deletions src/Core/Events/LeafEntityLifeCycleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,56 @@

namespace RZ\Roadiz\Core\Events;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\Event\PrePersistEventArgs;
use Doctrine\ORM\Events;
use RZ\Roadiz\Core\AbstractEntities\AbstractEntity;
use RZ\Roadiz\Core\AbstractEntities\LeafInterface;
use RZ\Roadiz\Core\Handlers\HandlerFactoryInterface;

/**
* @package RZ\Roadiz\Core\Events
*/
class LeafEntityLifeCycleSubscriber implements EventSubscriber
#[AsDoctrineListener(event: Events::prePersist)]

Check failure on line 14 in src/Core/Events/LeafEntityLifeCycleSubscriber.php

View workflow job for this annotation

GitHub Actions / run-tests (8.1)

Attribute class Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener does not exist.

Check failure on line 14 in src/Core/Events/LeafEntityLifeCycleSubscriber.php

View workflow job for this annotation

GitHub Actions / run-tests (8.2)

Attribute class Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener does not exist.

Check failure on line 14 in src/Core/Events/LeafEntityLifeCycleSubscriber.php

View workflow job for this annotation

GitHub Actions / run-tests (8.3)

Attribute class Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener does not exist.
final class LeafEntityLifeCycleSubscriber
{
private HandlerFactoryInterface $handlerFactory;

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

/**
* {@inheritdoc}
*/
public function getSubscribedEvents(): array
public function prePersist(PrePersistEventArgs $event): void
{
return [
Events::prePersist,
];
}
$entity = $event->getObject();

/**
* @param LifecycleEventArgs $event
* @return void
*/
public function prePersist(LifecycleEventArgs $event)
{
$entity = $event->getEntity();
if ($entity instanceof AbstractEntity && $entity instanceof LeafInterface) {
/*
* Automatically set position only if not manually set before.
*/
try {
$handler = $this->handlerFactory->getHandler($entity);
if (!($entity instanceof AbstractEntity) || !($entity instanceof LeafInterface)) {
return;
}

if ($entity->getPosition() === 0.0) {
/*
* Get the last index after last tag in parent
*/
$lastPosition = $handler->cleanPositions(false);
if ($lastPosition > 1 && null !== $entity->getParent()) {
/*
* Need to decrement position because current tag is already
* in parent's children collection count.
*/
$entity->setPosition($lastPosition - 1);
} else {
$entity->setPosition($lastPosition);
}
} elseif ($entity->getPosition() === 0.5) {
/*
* Automatically set position only if not manually set before.
*/
try {
$handler = $this->handlerFactory->getHandler($entity);

if ($entity->getPosition() === 0.0) {
/*
* Get the last index after last tag in parent
*/
$lastPosition = $handler->cleanPositions(false);
if ($lastPosition > 1 && null !== $entity->getParent()) {
/*
* Position is set to 0.5, so we need to
* shift all tags to the bottom.
* Need to decrement position because current tag is already
* in parent's children collection count.
*/
$handler->cleanPositions(true);
$entity->setPosition($lastPosition - 1);
} else {
$entity->setPosition($lastPosition);
}
} catch (\InvalidArgumentException $e) {
} elseif ($entity->getPosition() === 0.5) {
/*
* Position is set to 0.5, so we need to
* shift all tags to the bottom.
*/
$handler->cleanPositions(true);
}
} catch (\InvalidArgumentException $e) {
}
}
}

0 comments on commit 52dc8fe

Please sign in to comment.