<?php
namespace App\EventSubscriber;
use App\Entity\MediaObject;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
class EasyAdminMediaObjectSubscriber implements EventSubscriberInterface
{
protected $container;
protected $toolsMediaService;
protected $webContentUserService;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->toolsMediaService = $this->container->get('app.tools.media');
$this->webContentUserService = $this->container->get('app.web_content.user');
}
public static function getSubscribedEvents()
{
return [
BeforeEntityPersistedEvent::class => ['setNewMediaObject'],
BeforeEntityUpdatedEvent::class => ['setEditMediaObject']
];
}
public function setNewMediaObject(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof MediaObject)) {
return;
}
$this->toolsMediaService->defineEntityMediaFromFile($entity);
}
public function setEditMediaObject(BeforeEntityUpdatedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof MediaObject)) {
return;
}
$this->toolsMediaService->defineEntityMediaFromFile($entity);
}
// public function preRemove(LifecycleEventArgs $args)
// {
// $entity = $args->getEntity();
// if (!$entity instanceof MediaObject) {
// return;
// }
// $this->toolsMediaService->remove($entity);
// }
// private function generatesMultipleFormats($entity)
// {
// if (!in_array($entity->getEncodingFormat(), $this->toolsMediaService->getMimetypesRules())) {
// throw new \RuntimeException('Encoding format is not accepted');
// }
// $this->toolsMediaService->generatesMultipleFormats($entity);
// }
// public function setSlide(BeforeEntityPersistedEvent $event)
// {
// $entity = $event->getEntityInstance();
// if (!($entity instanceof Slide)) {
// return;
// }
// dump('this is Slide eventSubscriber');
// die;
// }
}