<?php
namespace App\EventSubscriber;
use App\Entity\Article;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
class EasyAdminArticleSubscriber implements EventSubscriberInterface
{
protected $container;
protected $webContentSEOService;
protected $webContentArticleService;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->webContentSEOService = $this->container->get('app.web_content.seo');
$this->webContentArticleService = $this->container->get('app.web_content.article');
$this->webContentUserService = $this->container->get('app.web_content.user');
}
public static function getSubscribedEvents()
{
return [
BeforeEntityPersistedEvent::class => ['setNewArticle'],
BeforeEntityUpdatedEvent::class => ['setEditArticle']
];
}
public function setNewArticle(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof Article)) {
return;
}
$this->webContentArticleService->moreData($entity);
$this->webContentSEOService->defineMetaData($entity);
$this->webContentUserService->defineAuthor($entity);
}
public function setEditArticle(BeforeEntityUpdatedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof Article)) {
return;
}
$this->webContentArticleService->moreData($entity);
$this->webContentSEOService->defineMetaData($entity);
$this->webContentUserService->defineAuthor($entity);
}
}