src/V4/Webhook/EventSubscriber/PostPersistEvent/SendAsyncWebhookHandleMessageEventSubscriber.php line 56

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\V4\Webhook\EventSubscriber\PostPersistEvent;
  4. use App\Model\Product\Product;
  5. use App\Transformer\FQCNToShortNameTransformer;
  6. use App\V4\Event\PostPersistEvent;
  7. use App\V4\Model\Prospect\Prospect;
  8. use App\V4\Model\Quote\Quote;
  9. use App\V4\Model\Task\Task;
  10. use App\V4\Security\AuthenticationManager;
  11. use App\V4\Webhook\Enum\WebhookTriggeredOnEnum;
  12. use App\V4\Webhook\Message\WebhookTriggeredMessage;
  13. use App\V4\Webhook\Repository\WebhookRepository;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Messenger\MessageBusInterface;
  16. class SendAsyncWebhookHandleMessageEventSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var AuthenticationManager
  20.      */
  21.     private $authenticationManager;
  22.     /**
  23.      * @var MessageBusInterface
  24.      */
  25.     private $messageBus;
  26.     /**
  27.      * @var WebhookRepository
  28.      */
  29.     private $webhookRepository;
  30.     public function __construct(
  31.         AuthenticationManager $authenticationManager,
  32.         MessageBusInterface $messageBus,
  33.         WebhookRepository $webhookRepository
  34.     ) {
  35.         $this->authenticationManager $authenticationManager;
  36.         $this->messageBus $messageBus;
  37.         $this->webhookRepository $webhookRepository;
  38.     }
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             PostPersistEvent::NAME => 'onPostPersist',
  43.         ];
  44.     }
  45.     /**
  46.      * @SuppressWarnings(PHPMD.StaticAccess)
  47.      */
  48.     public function onPostPersist(PostPersistEvent $event): void
  49.     {
  50.         $trigger null === $event->getBefore()
  51.             ? WebhookTriggeredOnEnum::CREATE
  52.             WebhookTriggeredOnEnum::UPDATE;
  53.         $className FQCNToShortNameTransformer::transform(get_class($event->getAfter()));
  54.         if (!$className) {
  55.             return;
  56.         }
  57.         $webhooks $this->webhookRepository->getEnableWebhooksFor($trigger$className);
  58.         if (=== count($webhooks)) {
  59.             return;
  60.         }
  61.         //hotfix/2.44.4 : nécessaire pour éviter de sérialiser les fichiers lors du dispatch du message
  62.         $after $event->getAfter();
  63.         if (in_array(get_class($after), [Prospect::class, Task::class, Quote::class, Product::class], true) && is_array($after->getCustomerFiles())) {
  64.             foreach ($after->getCustomerFiles() as $file) {
  65.                 $file->setCustomerFile(null);
  66.             }
  67.         }
  68.         foreach ($webhooks as $webhook) {
  69.             $message = new WebhookTriggeredMessage(
  70.                 $event,
  71.                 $webhook->getId(),
  72.                 $this->authenticationManager->getLoggedInUser()->getCustomerId()
  73.             );
  74.             $this->messageBus->dispatch($message);
  75.         }
  76.     }
  77. }