src/Service/EventSubscriber/DirectorySubscriber.php line 58

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Service\EventSubscriber;
  3. use Harmonizely\Background\Data\DirectoryDeleteCompanyEventData;
  4. use Harmonizely\Background\Data\DirectorySaveCompanyEventData;
  5. use Harmonizely\Core\BackgroundWork\Contract\IClient;
  6. use Harmonizely\Core\Security\Contract\ISecurityHelper;
  7. use Harmonizely\Model\EventTypeInterface;
  8. use Harmonizely\Service\Event\CreateEventTypeEvent;
  9. use Harmonizely\Service\Event\DeleteEventTypeEvent;
  10. use Harmonizely\Service\Event\UpdateEventTypeEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class DirectorySubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var IClient
  16.      */
  17.     private IClient $backgroundClient;
  18.     /**
  19.      * @var ISecurityHelper
  20.      */
  21.     private ISecurityHelper $securityHelper;
  22.     /**
  23.      * @param IClient $backgroundClient
  24.      * @param ISecurityHelper $securityHelper
  25.      */
  26.     public function __construct(
  27.         IClient $backgroundClient,
  28.         ISecurityHelper $securityHelper
  29.     )
  30.     {
  31.         $this->backgroundClient $backgroundClient;
  32.         $this->securityHelper $securityHelper;
  33.     }
  34.     /**
  35.      * @return string[]
  36.      */
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             CreateEventTypeEvent::NAME => 'createEventType',
  41.             UpdateEventTypeEvent::NAME => 'updateEventType',
  42.             DeleteEventTypeEvent::NAME => 'deleteEventType',
  43.         ];
  44.     }
  45.     /**
  46.      * @param CreateEventTypeEvent $event
  47.      * @return void
  48.      */
  49.     public function createEventType(CreateEventTypeEvent $event): void
  50.     {
  51.         $this->saveOrDeleteEventType($event->getEventType());
  52.     }
  53.     /**
  54.      * @param UpdateEventTypeEvent $event
  55.      * @return void
  56.      */
  57.     public function updateEventType(UpdateEventTypeEvent $event): void
  58.     {
  59.         $this->saveOrDeleteEventType($event->getEventType());
  60.     }
  61.     /**
  62.      * @param DeleteEventTypeEvent $event
  63.      * @return void
  64.      */
  65.     public function deleteEventType(DeleteEventTypeEvent $event): void
  66.     {
  67.         $user $this->securityHelper->getUser();
  68.         $this->backgroundClient->add(new DirectoryDeleteCompanyEventData($user->getId(), $event->getEventTypeId()));
  69.         $this->backgroundClient->runJobs();
  70.     }
  71.     /**
  72.      * @param EventTypeInterface $eventType
  73.      * @return void
  74.      */
  75.     private function saveOrDeleteEventType(EventTypeInterface $eventType): void
  76.     {
  77.         $user $this->securityHelper->getUser();
  78.         if ($eventType->isEnabled() && !$eventType->isSecret()) {
  79.             $this->backgroundClient->add(new DirectorySaveCompanyEventData($user->getId(), $eventType->getId()));
  80.         } else {
  81.             $this->backgroundClient->add(new DirectoryDeleteCompanyEventData($user->getId(), $eventType->getId()));
  82.         }
  83.         $this->backgroundClient->runJobs();
  84.     }
  85. }