<?php
namespace App\EventSubscriber;
use App\Entity\Asset;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class VichUploaderSubscriber implements EventSubscriberInterface
{
private EntityManagerInterface $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function onVichUploaderPostRemove($event)
{
$object = $event->getObject();
if ($object instanceof Asset && null === $object->getAssetFile()) {
$this->entityManager->remove($object);
$this->entityManager->flush();
}
}
public static function getSubscribedEvents(): array
{
return [
'vich_uploader.post_remove' => 'onVichUploaderPostRemove',
];
}
}