src/EventSubscriber/SetEventSchedulerUserSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\EventSubscriber;
  4. use Harmonizely\CalendarEvents;
  5. use Harmonizely\Core\Security\Contract\ISecurityHelper;
  6. use Harmonizely\Model\EventInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\EventDispatcher\GenericEvent;
  9. use Webmozart\Assert\Assert;
  10. class SetEventSchedulerUserSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var ISecurityHelper
  14.      */
  15.     private ISecurityHelper $securityHelper;
  16.     /**
  17.      * @param ISecurityHelper $securityHelper
  18.      */
  19.     public function __construct(ISecurityHelper $securityHelper)
  20.     {
  21.         $this->securityHelper $securityHelper;
  22.     }
  23.     /**
  24.      * @return string[]
  25.      */
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             CalendarEvents::EVENT_PRE_CREATE => 'setEventSchedulerUser',
  30.         ];
  31.     }
  32.     /**
  33.      * @param GenericEvent $genericEvent
  34.      * @return void
  35.      */
  36.     public function setEventSchedulerUser(GenericEvent $genericEvent): void
  37.     {
  38.         /** @var EventInterface $event */
  39.         $event $genericEvent->getSubject();
  40.         Assert::isInstanceOf($eventEventInterface::class);
  41.         $currentUser $this->securityHelper->getUser();
  42.         if ($currentUser) {
  43.             $event->setSchedulerUser($currentUser);
  44.         }
  45.     }
  46. }