src/TeclaConnector/EventSubscriber/SynchronizeCreatedContactSubscriber.php line 50

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2020 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2020 TECLA Consulting Group oü
  16.  */
  17. namespace App\TeclaConnector\EventSubscriber;
  18. use App\Contact\Event\ContactCreatedEvent;
  19. use App\TeclaConnector\Exception\Synchronizer\LegacySynchronizerFailedExceptionInterface;
  20. use App\TeclaConnector\Service\FailedLegacySynchronizerServiceInterface;
  21. use App\TeclaConnector\Synchronizer\Contact\LegacyCreateSynchronizerInterface;
  22. use JetBrains\PhpStorm\ArrayShape;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. final class SynchronizeCreatedContactSubscriber implements EventSubscriberInterface
  25. {
  26.     private LegacyCreateSynchronizerInterface $createdContactSynchronizer;
  27.     private FailedLegacySynchronizerServiceInterface $failedSynchronizerService;
  28.     public function __construct(
  29.         LegacyCreateSynchronizerInterface $createdContactSynchronizer,
  30.         FailedLegacySynchronizerServiceInterface $failedSynchronizerService
  31.     ) {
  32.         $this->createdContactSynchronizer $createdContactSynchronizer;
  33.         $this->failedSynchronizerService $failedSynchronizerService;
  34.     }
  35.     #[ArrayShape([ContactCreatedEvent::class => 'array'])]
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             ContactCreatedEvent::class => ['onContactCreated'10],
  40.         ];
  41.     }
  42.     public function onContactCreated(ContactCreatedEvent $event): void
  43.     {
  44.         if (false === $event->getSynchronize()) {
  45.             return;
  46.         }
  47.         $contact $event->getContact();
  48.         try {
  49.             $this->createdContactSynchronizer->synchronize($contact);
  50.         } catch (LegacySynchronizerFailedExceptionInterface $e) {
  51.             $this->failedSynchronizerService->create($e->getSynchronizerRequest());
  52.         }
  53.     }
  54. }