src/EventSubscriber/ActionSubscriber.php line 90

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Event\ActionErrorEvent;
  4. use App\Event\ActionFormEvent;
  5. use App\Message\ProcessFormActionsMessage;
  6. use App\Service\FormActionSetManager;
  7. use Exception;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Messenger\MessageBusInterface;
  11. class ActionSubscriber implements EventSubscriberInterface
  12. {
  13.     /** @var FormActionSetManager $formActionSetManager */
  14.     private $formActionSetManager;
  15.     /** @var MessageBusInterface $messageBus */
  16.     private $messageBus;
  17.     /** @var LoggerInterface $logger */
  18.     private $logger;
  19.     /** @var bool $asyncFormActions */
  20.     private $asyncFormActions;
  21.     /**
  22.      * @param FormActionSetManager $formActionSetManager
  23.      * @param MessageBusInterface $messageBus
  24.      * @param LoggerInterface $logger
  25.      * @param bool $asyncFormActions
  26.      */
  27.     public function __construct(
  28.         FormActionSetManager $formActionSetManager,
  29.         MessageBusInterface $messageBus,
  30.         LoggerInterface $logger,
  31.         bool $asyncFormActions
  32.     ) {
  33.         $this->formActionSetManager = $formActionSetManager;
  34.         $this->messageBus = $messageBus;
  35.         $this->logger = $logger;
  36.         $this->asyncFormActions = $asyncFormActions;
  37.     }
  38.     /**
  39.      * @return string[]
  40.      */
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             ActionErrorEvent::KEY => 'onActionError',
  45.             ActionFormEvent::KEY => 'onAppEventsForm'
  46.         ];
  47.     }
  48.     /**
  49.      * Una acción que no ha podido completarse (hoy: un email sin destinatarios resolubles).
  50.      *
  51.      * Se registra con nivel error para que llegue a Sentry. Sin esto, un formulario cuyo
  52.      * email de confirmación no encuentra destinatario falla de forma completamente silenciosa.
  53.      *
  54.      * @param ActionErrorEvent $event
  55.      * @return void
  56.      */
  57.     public function onActionError(ActionErrorEvent $event)
  58.     {
  59.         $action = $event->getAction();
  60.         $this->logger->error('La acción del formulario no ha podido completarse', [
  61.             'action' => $action->getId(),
  62.             'action_name' => $action->getName(),
  63.             'command' => $action->getCommand(),
  64.             'form' => $event->getForm()->getId(),
  65.             'form_lead' => $event->getFormLead()->getId(),
  66.         ]);
  67.     }
  68.     /**
  69.      * Las acciones post-envío generan PDFs con Dompdf, calculan los promedios del anexo de
  70.      * indicadores sobre toda la campaña y envían por SMTP. Con ASYNC_FORM_ACTIONS=1 todo eso
  71.      * sale del request y lo procesa un worker de Messenger.
  72.      *
  73.      * El interruptor viene por variable de entorno y por defecto está apagado: así el código
  74.      * se puede desplegar sin activar el asíncrono, validar el worker en un entorno y activarlo
  75.      * después sin necesidad de otra release. Si no hay worker consumiendo la cola, los emails
  76.      * no llegan a salir, y eso tiene que ser una decisión explícita.
  77.      *
  78.      * @param ActionFormEvent $event
  79.      * @return void
  80.      * @throws Exception
  81.      */
  82.     public function onAppEventsForm(ActionFormEvent $event)
  83.     {
  84.         if ($this->asyncFormActions) {
  85.             $this->messageBus->dispatch(new ProcessFormActionsMessage(
  86.                 $event->getForm()->getId(),
  87.                 $event->getFormLead()->getId(),
  88.                 $event->getName()
  89.             ));
  90.             return;
  91.         }
  92.         $this->formActionSetManager
  93.             ->setForm($event->getForm())
  94.             ->setLead($event->getFormLead())
  95.             ->executeActionSets($event->getName())
  96.         ;
  97.     }
  98. }