murph-core/src/core/EventSubscriber/Site/NavigationEventSubscriber.php

45 lines
1.1 KiB
PHP
Raw Normal View History

2022-03-13 19:32:32 +01:00
<?php
namespace App\Core\EventSubscriber\Site;
use App\Core\Entity\EntityInterface;
use App\Core\Entity\Site\Navigation;
use App\Core\Event\EntityManager\EntityManagerEvent;
use App\Core\EventSubscriber\EntityManagerEventSubscriber;
use App\Core\Manager\EntityManager;
use App\Core\Slugify\CodeSlugify;
/**
* class NavigationEventSubscriber.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class NavigationEventSubscriber extends EntityManagerEventSubscriber
{
public function __construct(
protected EntityManager $entityManager,
protected CodeSlugify $slugify
2022-03-13 19:32:32 +01:00
) {
}
2022-04-25 16:32:05 +02:00
public function supports(EntityInterface $entity): bool
2022-03-13 19:32:32 +01:00
{
return $entity instanceof Navigation;
}
public function onPreUpdate(EntityManagerEvent $event)
{
2022-04-25 16:32:05 +02:00
if (!$this->supports($event->getEntity())) {
2022-03-13 19:32:32 +01:00
return;
}
$menu = $event->getEntity();
$menu->setCode($this->slugify->slugify($menu->getCode()));
}
public function onPreCreate(EntityManagerEvent $event)
{
return $this->onPreUpdate($event);
}
}