src/EventSubscriber/CreateWherebyMeetingLinkSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\EventSubscriber;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Harmonizely\CalendarEvents;
  6. use Harmonizely\EventTypes\LocationTypeProvider;
  7. use Harmonizely\Integration\Whereby\ClientFactory;
  8. use Harmonizely\Integration\Whereby\WherebyApi;
  9. use Harmonizely\Model\EventInterface;
  10. use Harmonizely\Model\EventTypeInterface;
  11. use Harmonizely\Model\Integration;
  12. use Harmonizely\Model\IntegrationInterface;
  13. use Harmonizely\Model\UserInterface;
  14. use Psr\Log\LoggerInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\EventDispatcher\GenericEvent;
  17. use Webmozart\Assert\Assert;
  18. use Harmonizely\Types\IntegrationType;
  19. final class CreateWherebyMeetingLinkSubscriber implements EventSubscriberInterface
  20. {
  21.     private $entityManager;
  22.     private $logger;
  23.     private LocationTypeProvider $locationTypeProvider;
  24.     public function __construct(
  25.         EntityManagerInterface $entityManager,
  26.         LoggerInterface $logger,
  27.         LocationTypeProvider $locationTypeProvider
  28.     ) {
  29.         $this->entityManager $entityManager;
  30.         $this->logger $logger;
  31.         $this->locationTypeProvider $locationTypeProvider;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             CalendarEvents::EVENT_PRE_CREATE => 'createMeeting',
  37.         ];
  38.     }
  39.     public function createMeeting(GenericEvent $event): void
  40.     {
  41.         /** @var EventInterface $event */
  42.         $event $event->getSubject();
  43.         Assert::isInstanceOf($eventEventInterface::class);
  44.         $eventType $event->getEventType();
  45.         $user $event->getUser();
  46.         $locationType $this->locationTypeProvider->provideFor($eventType$user$event);
  47.         if (EventTypeInterface::LOCATION_TYPE_WHEREBY !== $locationType) {
  48.             return;
  49.         }
  50.         $integration $this->findIntegration($user);
  51.         if (null === $integration || (null !== $integration && !$integration->isEnabled())) {
  52.             return;
  53.         }
  54.         if ($eventType->isGroupMeetingType() && $event->getLocation()) {
  55.             return;
  56.         }
  57.         $wherebyClientFactory = new ClientFactory($integration->getConfig()['key']);
  58.         $wherebyApi = new WherebyApi($wherebyClientFactory->create());
  59.         $invitee $event->getInvitee();
  60.         try {
  61.             $meeting $wherebyApi->createMeeting($event->getScheduledAt(), $event->getEndDate());
  62.         } catch (\Exception $e) {
  63.             $this->logger->critical('Whereby create meeting failed.', [
  64.                 'message' => $e->getMessage(),
  65.                 'user_email' => $user->getEmail(),
  66.                 'invitee_email' => $invitee->getEmail(),
  67.             ]);
  68.             return;
  69.         }
  70.         $event->setLocation($meeting->getRoomUrl());
  71.     }
  72.     private function findIntegration(UserInterface $user): ?IntegrationInterface
  73.     {
  74.         $integrationRepository $this->entityManager->getRepository(Integration::class);
  75.         return $integrationRepository->findOneBy([
  76.             'type' => IntegrationType::TYPE_WHEREBY,
  77.             'user' => $user,
  78.         ]);
  79.     }
  80. }