deblan.io-murph/src/EventSubscriber/Blog/CategoryEventSubscriber.php
2022-04-25 16:36:25 +02:00

48 lines
1.1 KiB
PHP

<?php
namespace App\EventSubscriber\Blog;
use App\Core\Entity\EntityInterface;
use App\Core\Event\EntityManager\EntityManagerEvent;
use App\Core\EventSubscriber\EntityManagerEventSubscriber;
use App\Core\Slugify\Slugify;
use App\Entity\Blog\Category;
/**
* class CategoryEventSubscriber.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class CategoryEventSubscriber extends EntityManagerEventSubscriber
{
protected Slugify $slugify;
public function __construct(Slugify $slugify)
{
$this->slugify = $slugify;
}
public function supports(EntityInterface $entity)
{
return $entity instanceof Category;
}
public function onPreUpdate(EntityManagerEvent $event)
{
if (!$this->supports($event->getEntity())) {
return;
}
if ($event->getEntity()->getSlug()) {
return;
}
$event->getEntity()->setSlug($this->slugify->slugify($event->getEntity()->getTitle()));
}
public function onPreCreate(EntityManagerEvent $event)
{
return $this->onPreUpdate($event);
}
}