src/TeclaConnector/EventSubscriber/SynchronizeCreatedUserSubscriber.php line 54

  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\Core\Event\UserRegistrationConfirmedEvent;
  19. use App\Core\Event\UserRegistrationSuccessEvent;
  20. use App\Core\Model\UserInterface;
  21. use App\TeclaConnector\Exception\Synchronizer\LegacySynchronizerFailedExceptionInterface;
  22. use App\TeclaConnector\Service\FailedLegacySynchronizerServiceInterface;
  23. use App\TeclaConnector\Synchronizer\User\LegacyCreateSynchronizerInterface;
  24. use JetBrains\PhpStorm\ArrayShape;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. final class SynchronizeCreatedUserSubscriber implements EventSubscriberInterface
  27. {
  28.     private LegacyCreateSynchronizerInterface $synchronizer;
  29.     private FailedLegacySynchronizerServiceInterface $failedSynchronizerService;
  30.     public function __construct(LegacyCreateSynchronizerInterface $synchronizerFailedLegacySynchronizerServiceInterface $failedSynchronizerService)
  31.     {
  32.         $this->synchronizer $synchronizer;
  33.         $this->failedSynchronizerService $failedSynchronizerService;
  34.     }
  35.     #[ArrayShape([
  36.         UserRegistrationSuccessEvent::class => 'string',
  37.         UserRegistrationConfirmedEvent::class => 'string',
  38.     ])]
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             UserRegistrationSuccessEvent::class => 'onUserRegistrationSuccess',
  43.             UserRegistrationConfirmedEvent::class => 'onUserRegistrationConfirmedEvent',
  44.         ];
  45.     }
  46.     public function onUserRegistrationSuccess(UserRegistrationSuccessEvent $event): void
  47.     {
  48.         $this->synchronizeCreatedUser($event->getUser());
  49.     }
  50.     public function onUserRegistrationConfirmedEvent(UserRegistrationConfirmedEvent $event): void
  51.     {
  52.         $this->synchronizeCreatedUser($event->getUser());
  53.     }
  54.     private function synchronizeCreatedUser(UserInterface $user): void
  55.     {
  56.         try {
  57.             $this->synchronizer->synchronize($user);
  58.         } catch (LegacySynchronizerFailedExceptionInterface $e) {
  59.             $this->failedSynchronizerService->create($e->getSynchronizerRequest());
  60.         }
  61.     }
  62. }