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