src/EventListener/SendZapierPayloadListener.php line 86

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\EventListener;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use FOS\RestBundle\Context\Context;
  6. use FOS\RestBundle\Serializer\JMSSerializerAdapter;
  7. use FOS\RestBundle\Serializer\Serializer;
  8. use GuzzleHttp\ClientInterface;
  9. use GuzzleHttp\Exception\RequestException;
  10. use Harmonizely\Model\Event;
  11. use Harmonizely\Model\EventInterface;
  12. use Harmonizely\Model\EventType;
  13. use Harmonizely\Model\Integration;
  14. use Harmonizely\Model\IntegrationInterface;
  15. use Harmonizely\Model\Invitee;
  16. use Harmonizely\Model\UserInterface;
  17. use Harmonizely\Types\EventTypeDurationType;
  18. use Psr\Http\Message\ResponseInterface;
  19. use Psr\Log\LoggerInterface;
  20. use Symfony\Component\EventDispatcher\GenericEvent;
  21. use Webmozart\Assert\Assert;
  22. use Harmonizely\Types\IntegrationType;
  23. final class SendZapierPayloadListener
  24. {
  25.     /**
  26.      * @var ClientInterface
  27.      */
  28.     private $client;
  29.     /**
  30.      * @var EntityManagerInterface
  31.      */
  32.     private $entityManager;
  33.     /**
  34.      * @var Serializer
  35.      */
  36.     private $serializer;
  37.     /**
  38.      * @var LoggerInterface
  39.      */
  40.     private $logger;
  41.     public function __construct(
  42.         ClientInterface $client,
  43.         EntityManagerInterface $entityManager,
  44.         JMSSerializerAdapter $serializer,
  45.         LoggerInterface $logger
  46.     ) {
  47.         $this->client $client;
  48.         $this->entityManager $entityManager;
  49.         $this->serializer $serializer;
  50.         $this->logger $logger;
  51.     }
  52.     public function sendCreatedPayload(GenericEvent $event): void
  53.     {
  54.         $this->logger->notice('Start test ZAPIER log (create)');
  55.         $event $event->getSubject();
  56.         Assert::isInstanceOf($eventEventInterface::class);
  57.         $user $event->getUser();
  58.         $integration $this->findIntegration($user);
  59.         if ($integration && $integration->isEnabled()) {
  60.             $config $integration->getConfig();
  61.             if (!isset($config['createdWebhookUrl'])) {
  62.                 return;
  63.             }
  64.             $this->logger->notice('Send scheduled meeting payload to Zapier', [
  65.                 'user_email' => $user->getEmail(),
  66.                 'integration_config' => $integration->getConfig(),
  67.             ]);
  68.             $this->send($event$config['createdWebhookUrl']);
  69.         }
  70.     }
  71.     public function sendCancelledPayload(GenericEvent $event): void
  72.     {
  73.         $event $event->getSubject();
  74.         Assert::isInstanceOf($eventEventInterface::class);
  75.         $user $event->getUser();
  76.         $integration $this->findIntegration($user);
  77.         if ($integration && $integration->isEnabled()) {
  78.             $config $integration->getConfig();
  79.             if (!isset($config['canceledWebhookUrl'])) {
  80.                 return;
  81.             }
  82.             $this->logger->notice('Send cancelled meeting payload to Zapier', [
  83.                 'user_email' => $user->getEmail(),
  84.                 'integration_config' => $integration->getConfig(),
  85.             ]);
  86.             $this->send($event$config['canceledWebhookUrl']);
  87.         }
  88.     }
  89.     public function sendRescheduledPayload(GenericEvent $event): void
  90.     {
  91.         $event $event->getSubject();
  92.         Assert::isInstanceOf($eventEventInterface::class);
  93.         $user $event->getUser();
  94.         $integration $this->findIntegration($user);
  95.         if ($integration && $integration->isEnabled()) {
  96.             $config $integration->getConfig();
  97.             if (!isset($config['rescheduledWebhookUrl'])) {
  98.                 return;
  99.             }
  100.             $this->logger->notice('Send rescheduled meeting payload to Zapier', [
  101.                 'user_email' => $user->getEmail(),
  102.                 'integration_config' => $integration->getConfig(),
  103.             ]);
  104.             $this->send($event$config['rescheduledWebhookUrl']);
  105.         }
  106.     }
  107.     private function findIntegration(UserInterface $user): ?IntegrationInterface
  108.     {
  109.         $integrationRepository $this->entityManager->getRepository(Integration::class);
  110.         return $integrationRepository->findOneBy([
  111.             'type' => IntegrationType::TYPE_ZAPIER,
  112.             'user' => $user,
  113.         ]);
  114.     }
  115.     public function sendCreatedTestPayload(GenericEvent $event): void
  116.     {
  117.         /** @var IntegrationInterface $integration */
  118.         $integration $event->getSubject();
  119.         Assert::isInstanceOf($integrationIntegrationInterface::class);
  120.         $config $integration->getConfig();
  121.         if (!isset($config['createdWebhookUrl'])) {
  122.             return;
  123.         }
  124.         $event $this->createTestEvent();
  125.         $this->send($event$config['createdWebhookUrl']);
  126.     }
  127.     public function sendCancelledTestPayload(GenericEvent $event): void
  128.     {
  129.         /** @var IntegrationInterface $integration */
  130.         $integration $event->getSubject();
  131.         Assert::isInstanceOf($integrationIntegrationInterface::class);
  132.         $config $integration->getConfig();
  133.         if (!isset($config['canceledWebhookUrl'])) {
  134.             return;
  135.         }
  136.         $event $this->createTestEvent();
  137.         $event->cancel();
  138.         $this->send($event$config['canceledWebhookUrl']);
  139.     }
  140.     public function sendRescheduledTestPayload(GenericEvent $event): void
  141.     {
  142.         /** @var IntegrationInterface $integration */
  143.         $integration $event->getSubject();
  144.         Assert::isInstanceOf($integrationIntegrationInterface::class);
  145.         $config $integration->getConfig();
  146.         if (!isset($config['rescheduledWebhookUrl'])) {
  147.             return;
  148.         }
  149.         $event $this->createTestEvent();
  150.         $this->send($event$config['rescheduledWebhookUrl']);
  151.     }
  152.     private function send(EventInterface $eventstring $webhookUrl): void
  153.     {
  154.         $context = new Context();
  155.         $context->setGroups(['integration']);
  156.         $context->setSerializeNull(true);
  157.         $payload $this->serializer->serialize($event'json'$context);
  158.         try {
  159.             $promise $this->client->postAsync($webhookUrl, [
  160.                 'body' => $payload,
  161.                 'headers' => [
  162.                     'Content-Type' => 'application/json',
  163.                     'User-Agent' => 'SimplyMeet.me',
  164.                 ],
  165.             ]);
  166.             $data json_decode($payloadtrue);
  167.             $inviteeData $data['invitee'];
  168.             $eventTypeData $data['event_type'];
  169.             unset($data['invitee'], $data['event_type']);
  170.             $this->logger->notice('Zapier data', [
  171.                 'content' => $data,
  172.                 'invitee' => $inviteeData,
  173.                 'event_type' => $eventTypeData,
  174.             ]);
  175.             $promise->then(
  176.                 function (ResponseInterface $res) {
  177.                     $this->logger->notice('Zapier response', [
  178.                         'status_code' => $res->getStatusCode(),
  179.                         'content' => $res->getBody()->getContents(),
  180.                     ]);
  181.                 },
  182.                 function (RequestException $e) {
  183.                     $this->logger->critical($e->getMessage());
  184.                 }
  185.             );
  186.             $promise->wait();
  187.         } catch (\Exception $e) {
  188.             $this->logger->critical($e->getMessage());
  189.         }
  190.     }
  191.     private function createTestEvent(): EventInterface
  192.     {
  193.         $event = new Event();
  194.         $eventType = new EventType();
  195.         $eventType->setDurationType(EventTypeDurationType::FIXED_TYPE);
  196.         $eventType->setName('15 min meeting');
  197.         $eventType->setLocation('Google Meet');
  198.         $eventType->setDescription('15 minutes meeting with me.');
  199.         $eventType->setDuration(15);
  200.         $eventType->setSlug('15min');
  201.         $eventType->setTimezone('Europe/Berlin');
  202.         $event->setEventType($eventType);
  203.         $event->setScheduledAt(new \DateTime('now'));
  204.         $event->setEndDate(new \DateTime('+1 hour'));
  205.         $event->setDuration(15);
  206.         $invitee = new Invitee();
  207.         $invitee->setTimezone('Europe/Berlin');
  208.         $invitee->setEmail('doe@example.com');
  209.         $invitee->setFullName('John Doe');
  210.         $invitee->setPhoneNumber(null);
  211.         $event->setInvitee($invitee);
  212.         return $event;
  213.     }
  214. }