src/EventSubscriber/VoucherSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Voucher;
  4. use App\Entity\JobQueue;
  5. use App\Service\EsJobQueue;
  6. use App\Event\VoucherPostPersistEvent;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\Security\Core\Security;
  9. use App\Service\JobQueueAction\GenerateVoucherCodeAction;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class VoucherSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(
  14.         private EntityManagerInterface $_em,
  15.         private Security $security,
  16.         private EsJobQueue $esJobQueue
  17.     ) {
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             VoucherPostPersistEvent::NAME => 'onPostCreate'
  23.         ];
  24.     }
  25.     public function onPostCreate(VoucherPostPersistEvent $event)
  26.     {
  27.         $object $event->getObject();
  28.         if ($object->getVoucherType() == Voucher::VOUCHERTYPE_ONE) {
  29.             $payload['voucherId'] = $object->getId();
  30.             $this->esJobQueue->create(
  31.                 GenerateVoucherCodeAction::NAME,
  32.                 json_encode($payload),
  33.                 JobQueue::PRIORITY_LOW,
  34.                 null,
  35.                 1,
  36.                 $this->security->getUser()
  37.             );
  38.         }
  39.     }
  40. }