src/Webhooks/EventSubscriber/SendWebhookPayloadSubscriber.php line 61

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\Webhooks\EventSubscriber;
  4. use FOS\RestBundle\Context\Context;
  5. use FOS\RestBundle\Serializer\JMSSerializerAdapter;
  6. use Harmonizely\CalendarEvents;
  7. use Harmonizely\Model\EventInterface;
  8. use Harmonizely\Webhooks\Client\WebhookClient;
  9. use Harmonizely\Webhooks\Client\WebhookPayload;
  10. use Harmonizely\Webhooks\Query\FindWebhookForEventQuery;
  11. use Harmonizely\Webhooks\Webhooks;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\EventDispatcher\GenericEvent;
  15. use Webmozart\Assert\Assert;
  16. final class SendWebhookPayloadSubscriber implements EventSubscriberInterface
  17. {
  18.     private JMSSerializerAdapter $serializer;
  19.     private LoggerInterface $logger;
  20.     private FindWebhookForEventQuery $findWebhookForEventQuery;
  21.     private WebhookClient $webhookClient;
  22.     public function __construct(
  23.         JMSSerializerAdapter $serializer,
  24.         FindWebhookForEventQuery $findWebhookForEventQuery,
  25.         LoggerInterface $logger,
  26.         WebhookClient $webhookClient
  27.     ) {
  28.         $this->serializer $serializer;
  29.         $this->findWebhookForEventQuery $findWebhookForEventQuery;
  30.         $this->logger $logger;
  31.         $this->webhookClient $webhookClient;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             CalendarEvents::EVENT_POST_CREATE => 'sendScheduled',
  37.             CalendarEvents::EVENT_POST_CANCEL => 'sendCancelled',
  38.             CalendarEvents::EVENT_POST_RESCHEDULE => 'sendRescheduled',
  39.         ];
  40.     }
  41.     public function sendScheduled(GenericEvent $event): void
  42.     {
  43.         /** @var EventInterface $event */
  44.         $event $event->getSubject();
  45.         Assert::isInstanceOf($eventEventInterface::class);
  46.         $this->sendPayload($eventWebhooks::MEETING_SCHEDULED);
  47.     }
  48.     public function sendRescheduled(GenericEvent $event): void
  49.     {
  50.         /** @var EventInterface $event */
  51.         $event $event->getSubject();
  52.         Assert::isInstanceOf($eventEventInterface::class);
  53.         $this->sendPayload($eventWebhooks::MEETING_RESCHEDULED);
  54.     }
  55.     public function sendCancelled(GenericEvent $event): void
  56.     {
  57.         /** @var EventInterface $event */
  58.         $event $event->getSubject();
  59.         Assert::isInstanceOf($eventEventInterface::class);
  60.         $this->sendPayload($eventWebhooks::MEETING_CANCELLED);
  61.     }
  62.     private function sendPayload(EventInterface $eventstring $webhookEventName): void
  63.     {
  64.         $user $event->getUser();
  65.         $webhook = ($this->findWebhookForEventQuery)((int) $user->getId(), $webhookEventName);
  66.         if (null === $webhook) {
  67.             return;
  68.         }
  69.         $webhookPayload = new WebhookPayload($event$webhookEventName$webhook->getUuid());
  70.         $context = new Context();
  71.         $context->setGroups(['integration']);
  72.         $context->setSerializeNull(true);
  73.         $payload $this->serializer->serialize($webhookPayload'json'$context);
  74.         try {
  75.             $this->webhookClient->post($webhook->getUrl(), $payload$webhook->getSecret());
  76.         } catch (\Throwable $e) {
  77.             $this->logger->error($e->getMessage());
  78.         }
  79.     }
  80. }