<?php
declare(strict_types = 1);
namespace App\EventSubscriber;
use App\Domain\Entity\Job;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\String\Slugger\SluggerInterface;
class EasyAdminSubscriber implements EventSubscriberInterface
{
public function __construct(
SluggerInterface $slugger
)
{}
public static function getSubscribedEvents()
{
return [
BeforeEntityPersistedEvent::class => ['eventBeforePersiste'],
BeforeEntityUpdatedEvent::class => ['eventBeforeUpdated']
];
}
public function eventBeforePersiste(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof Job)) {
return;
}
$slug = $this->slugger->slug($entity->getName());
$entity->setSlug($slug->toString());
}
public function eventBeforeUpdated(BeforeEntityUpdatedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof Job)) {
return;
}
$slug = $this->slugger->slug($entity->getName());
$entity->setSlug($slug->toString());
}
}