deblan.io-murph/src/EventSuscriber/Blog/CategoryEventSubscriber.php
2021-03-30 13:40:46 +02:00

48 lines
1.1 KiB
PHP

<?php
namespace App\EventSuscriber\Blog;
use App\Core\Entity\EntityInterface;
use App\Core\Event\EntityManager\EntityManagerEvent;
use App\Core\EventSuscriber\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 support(EntityInterface $entity)
{
return $entity instanceof Category;
}
public function onPreUpdate(EntityManagerEvent $event)
{
if (!$this->support($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);
}
}