src/EventSubscriber/EasyAdminArticleSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Article;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  8. class EasyAdminArticleSubscriber implements EventSubscriberInterface
  9. {
  10.     protected $container;
  11.     protected $webContentSEOService;
  12.     protected $webContentArticleService;
  13.     public function __construct(ContainerInterface $container)
  14.     {
  15.         $this->container $container;
  16.         $this->webContentSEOService $this->container->get('app.web_content.seo');
  17.         $this->webContentArticleService $this->container->get('app.web_content.article');
  18.         $this->webContentUserService $this->container->get('app.web_content.user');
  19.     }
  20.     
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             BeforeEntityPersistedEvent::class => ['setNewArticle'],
  25.             BeforeEntityUpdatedEvent::class => ['setEditArticle']
  26.         ];
  27.     }
  28.     public function setNewArticle(BeforeEntityPersistedEvent $event)
  29.     {
  30.         $entity $event->getEntityInstance();
  31.         if (!($entity instanceof Article)) {
  32.             return;
  33.         }
  34.     
  35.        $this->webContentArticleService->moreData($entity);
  36.        $this->webContentSEOService->defineMetaData($entity);
  37.        $this->webContentUserService->defineAuthor($entity);
  38.     }
  39.     public function setEditArticle(BeforeEntityUpdatedEvent $event)
  40.     {
  41.         $entity $event->getEntityInstance();
  42.         if (!($entity instanceof Article)) {
  43.             return;
  44.         }
  45.     
  46.        $this->webContentArticleService->moreData($entity);
  47.        $this->webContentSEOService->defineMetaData($entity);
  48.        $this->webContentUserService->defineAuthor($entity);
  49.     }
  50. }