src/EventSubscriber/QRCodeGenerator.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\WithQRCodeInterface;
  4. use App\Service\QRCodeService;
  5. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  8. use Exception;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class QRCodeGenerator implements EventSubscriberInterface
  11. {
  12.     public function __construct(
  13.         private QRCodeService $qrCodeService,
  14.     )
  15.     {
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             AfterEntityPersistedEvent::class => [
  21.                 ['onAfterEntityPersisted', -1]
  22.             ],
  23.             AfterEntityUpdatedEvent::class => [
  24.                 ['onAfterEntityUpdated', -1]
  25.             ],
  26.             AfterEntityDeletedEvent::class => [
  27.                 ['onAfterEntityDeleted', -1]
  28.             ],
  29.         ];
  30.     }
  31.     /**
  32.      * @throws Exception
  33.      */
  34.     public function onAfterEntityPersisted(AfterEntityPersistedEvent $event)
  35.     {
  36.         $entity $event->getEntityInstance();
  37.         if ($entity instanceof WithQRCodeInterface) {
  38.             $this->qrCodeService->generateForEntity($entity);
  39.         }
  40.     }
  41.     /**
  42.      * @throws Exception
  43.      */
  44.     public function onAfterEntityUpdated(AfterEntityUpdatedEvent $event)
  45.     {
  46.         $entity $event->getEntityInstance();
  47.         if ($entity instanceof WithQRCodeInterface) {
  48.             $this->qrCodeService->generateForEntity($entity);
  49.         }
  50.     }
  51.     public function onAfterEntityDeleted(AfterEntityDeletedEvent $event)
  52.     {
  53.         $entity $event->getEntityInstance();
  54.         if ($entity instanceof WithQRCodeInterface) {
  55.             // unlinking the qr code file
  56.             $this->qrCodeService->unlinkQRCode($entity);
  57.         }
  58.     }
  59. }