src/EventSubscriber/EasyAdminMediaObjectSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\MediaObject;
  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 EasyAdminMediaObjectSubscriber implements EventSubscriberInterface
  9. {
  10.     protected $container;
  11.     
  12.     protected $toolsMediaService;
  13.     protected $webContentUserService;
  14.     
  15.     public function __construct(ContainerInterface $container)
  16.     {
  17.         $this->container $container;
  18.         $this->toolsMediaService $this->container->get('app.tools.media');
  19.         $this->webContentUserService $this->container->get('app.web_content.user');
  20.     }
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             BeforeEntityPersistedEvent::class => ['setNewMediaObject'],
  25.             BeforeEntityUpdatedEvent::class => ['setEditMediaObject']
  26.         ];
  27.     }
  28.     public function setNewMediaObject(BeforeEntityPersistedEvent $event)
  29.     {
  30.         
  31.         $entity $event->getEntityInstance();
  32.         if (!($entity instanceof MediaObject)) {
  33.             return;
  34.         }
  35.         
  36.         $this->toolsMediaService->defineEntityMediaFromFile($entity);
  37.     }
  38.     public function setEditMediaObject(BeforeEntityUpdatedEvent $event)
  39.     {
  40.         
  41.         $entity $event->getEntityInstance();
  42.         if (!($entity instanceof MediaObject)) {
  43.             return;
  44.         }
  45.         
  46.         $this->toolsMediaService->defineEntityMediaFromFile($entity);
  47.     }
  48.     // public function preRemove(LifecycleEventArgs $args)
  49.     // {
  50.     //     $entity = $args->getEntity();
  51.     //     if (!$entity instanceof MediaObject) {
  52.     //         return;
  53.     //     }
  54.     //     $this->toolsMediaService->remove($entity);
  55.     // }
  56.     // private function generatesMultipleFormats($entity)
  57.     // {
  58.     //   if (!in_array($entity->getEncodingFormat(), $this->toolsMediaService->getMimetypesRules())) {
  59.     //     throw new \RuntimeException('Encoding format is not accepted');
  60.     //   }
  61.     //   $this->toolsMediaService->generatesMultipleFormats($entity);
  62.     // }
  63.     // public function setSlide(BeforeEntityPersistedEvent $event)
  64.     // {
  65.     //     $entity = $event->getEntityInstance();
  66.     //     if (!($entity instanceof Slide)) {
  67.     //         return;
  68.     //     }
  69.     
  70.     //   dump('this is Slide eventSubscriber');
  71.     //   die;
  72.     // }
  73.    
  74. }