deblan.io-murph/src/EventSuscriber/Blog/CategoryEventSubscriber.php

52 lines
1.2 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\Entity\Blog\Post;
use App\Repository\Blog\PostRepositoryQuery;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use App\Entity\Blog\Category;
use App\Core\Slugify\Slugify;
/**
* 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);
}
}