62 lines
1.6 KiB
PHP
62 lines
1.6 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\EventSuscriber\Site;
|
||
|
|
||
|
use App\Entity\EntityInterface;
|
||
|
use App\Entity\Site\Node;
|
||
|
use App\Event\EntityManager\EntityManagerEvent;
|
||
|
use App\EventSuscriber\EntityManagerEventSubscriber;
|
||
|
use App\Factory\Site\NodeFactory;
|
||
|
use App\Manager\EntityManager;
|
||
|
use App\Repository\Site\NodeRepository;
|
||
|
|
||
|
/**
|
||
|
* class NodeEventSubscriber.
|
||
|
*
|
||
|
* @author Simon Vieille <simon@deblan.fr>
|
||
|
*/
|
||
|
class NodeEventSubscriber extends EntityManagerEventSubscriber
|
||
|
{
|
||
|
protected NodeFactory $nodeFactory;
|
||
|
protected EntityManager $entityManager;
|
||
|
|
||
|
public function __construct(
|
||
|
NodeFactory $nodeFactory,
|
||
|
NodeRepository $nodeRepository,
|
||
|
EntityManager $entityManager
|
||
|
) {
|
||
|
$this->nodeFactory = $nodeFactory;
|
||
|
$this->nodeRepository = $nodeRepository;
|
||
|
$this->entityManager = $entityManager;
|
||
|
}
|
||
|
|
||
|
public function support(EntityInterface $entity)
|
||
|
{
|
||
|
return $entity instanceof Node;
|
||
|
}
|
||
|
|
||
|
public function onDelete(EntityManagerEvent $event)
|
||
|
{
|
||
|
if (!$this->support($event->getEntity())) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
$menu = $event->getEntity()->getMenu();
|
||
|
$rootNode = $menu->getRootNode();
|
||
|
|
||
|
if (0 !== count($rootNode->getChildren())) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
$childNode = $this->nodeFactory->create($menu);
|
||
|
$childNode
|
||
|
->setParent($rootNode)
|
||
|
->setLabel('Premier élément')
|
||
|
;
|
||
|
|
||
|
$this->entityManager->update($rootNode, false);
|
||
|
$this->entityManager->create($childNode, false);
|
||
|
$this->nodeRepository->persistAsFirstChild($childNode, $rootNode);
|
||
|
}
|
||
|
}
|