2021-03-24 19:10:26 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Core\EventSuscriber\Site;
|
|
|
|
|
2021-05-12 12:04:03 +02:00
|
|
|
use App\Core\Cache\SymfonyCacheManager;
|
2021-03-24 19:10:26 +01:00
|
|
|
use App\Core\Entity\EntityInterface;
|
|
|
|
use App\Core\Entity\Site\Menu;
|
|
|
|
use App\Core\Event\EntityManager\EntityManagerEvent;
|
|
|
|
use App\Core\EventSuscriber\EntityManagerEventSubscriber;
|
|
|
|
use App\Core\Factory\Site\NodeFactory;
|
|
|
|
use App\Core\Manager\EntityManager;
|
|
|
|
use App\Core\Repository\Site\NodeRepository;
|
|
|
|
use App\Core\Slugify\CodeSlugify;
|
2022-03-01 19:02:11 +01:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2021-03-24 19:10:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* class MenuEventSubscriber.
|
|
|
|
*
|
|
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
|
|
*/
|
|
|
|
class MenuEventSubscriber extends EntityManagerEventSubscriber
|
|
|
|
{
|
|
|
|
protected NodeFactory $nodeFactory;
|
|
|
|
protected NodeRepository $nodeRepository;
|
|
|
|
protected EntityManager $entityManager;
|
|
|
|
protected CodeSlugify $slugify;
|
2021-03-29 13:58:05 +02:00
|
|
|
protected SymfonyCacheManager $cacheManager;
|
2022-03-01 19:02:11 +01:00
|
|
|
protected TranslatorInterface $translation;
|
2021-03-24 19:10:26 +01:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
NodeFactory $nodeFactory,
|
|
|
|
NodeRepository $nodeRepository,
|
|
|
|
EntityManager $entityManager,
|
2021-03-29 13:58:05 +02:00
|
|
|
CodeSlugify $slugify,
|
2022-03-01 19:02:11 +01:00
|
|
|
SymfonyCacheManager $cacheManager,
|
|
|
|
TranslatorInterface $translator
|
2021-03-24 19:10:26 +01:00
|
|
|
) {
|
|
|
|
$this->nodeFactory = $nodeFactory;
|
|
|
|
$this->nodeRepository = $nodeRepository;
|
|
|
|
$this->entityManager = $entityManager;
|
|
|
|
$this->slugify = $slugify;
|
2021-03-29 13:58:05 +02:00
|
|
|
$this->cacheManager = $cacheManager;
|
2022-03-01 19:02:11 +01:00
|
|
|
$this->translator = $translator;
|
2021-03-24 19:10:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function support(EntityInterface $entity)
|
|
|
|
{
|
|
|
|
return $entity instanceof Menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onPreUpdate(EntityManagerEvent $event)
|
|
|
|
{
|
|
|
|
if (!$this->support($event->getEntity())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$menu = $event->getEntity();
|
|
|
|
$menu->setCode($this->slugify->slugify($menu->getCode()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onCreate(EntityManagerEvent $event)
|
|
|
|
{
|
|
|
|
if (!$this->support($event->getEntity())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$menu = $event->getEntity();
|
|
|
|
|
2021-03-29 13:58:05 +02:00
|
|
|
if (count($menu->getNodes()) > 2) {
|
2021-03-24 19:10:26 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-29 13:58:05 +02:00
|
|
|
$rootNode = $menu->getRootNode();
|
|
|
|
|
|
|
|
if (!$rootNode) {
|
|
|
|
$rootNode = $this->nodeFactory->create($menu);
|
|
|
|
}
|
|
|
|
|
2021-03-24 19:10:26 +01:00
|
|
|
$childNode = $this->nodeFactory->create($menu, '/');
|
|
|
|
$childNode
|
|
|
|
->setParent($rootNode)
|
2022-03-01 19:02:11 +01:00
|
|
|
->setLabel($this->translator->trans('First element'))
|
2021-03-24 19:10:26 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
$menu->setRootNode($rootNode);
|
|
|
|
|
|
|
|
$this->entityManager->getEntityManager()->persist($rootNode);
|
|
|
|
$this->entityManager->getEntityManager()->persist($childNode);
|
|
|
|
|
|
|
|
$this->entityManager->getEntityManager()->persist($menu);
|
|
|
|
$this->entityManager->flush();
|
|
|
|
|
|
|
|
$this->nodeRepository->persistAsFirstChild($childNode, $rootNode);
|
2021-03-29 13:58:05 +02:00
|
|
|
|
2021-04-04 20:09:29 +02:00
|
|
|
$this->cacheManager->cleanRouting();
|
2021-03-24 19:10:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onUpdate(EntityManagerEvent $event)
|
|
|
|
{
|
|
|
|
return $this->onCreate($event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onPreCreate(EntityManagerEvent $event)
|
|
|
|
{
|
|
|
|
return $this->onPreUpdate($event);
|
|
|
|
}
|
|
|
|
}
|