src/EventSubscriber/Song/SongSubscriber.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Song;
  3. use App\Entity\Logs\EventLog;
  4. use App\Event\LogEvent;
  5. use App\Event\Song\SongCreatedEvent;
  6. use App\Event\Song\SongRemovedEvent;
  7. use App\Service\AuthenticationService;
  8. use App\Service\CookieService;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class SongSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var AuthenticationService
  15.      */
  16.     private $authenticationService;
  17.     /**
  18.      * @var EventDispatcherInterface
  19.      */
  20.     private $eventDispatcher;
  21.     /**
  22.      * @var CookieService
  23.      */
  24.     private $cookieService;
  25.     public function __construct(
  26.         AuthenticationService $authenticationService,
  27.         EventDispatcherInterface $eventDispatcher,
  28.         CookieService $cookieService
  29.     )
  30.     {
  31.         $this->eventDispatcher $eventDispatcher;
  32.         $this->authenticationService $authenticationService;
  33.         $this->cookieService $cookieService;
  34.     }
  35.     /**
  36.      * @inheritDoc
  37.      */
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             SongCreatedEvent::class => 'onSongCreatedEvent',
  42.             SongRemovedEvent::class => 'onSongRemovedEvent'
  43.         ];
  44.     }
  45.     /**
  46.      * @param SongCreatedEvent $event
  47.      */
  48.     public function onSongCreatedEvent(SongCreatedEvent $event): void
  49.     {
  50.         $this->eventDispatcher->dispatch(new LogEvent($event->getSong(), $this->authenticationService->getUser(), EventLog::TYPE_SONG_CREATED));
  51.     }
  52.     /**
  53.      * @param SongRemovedEvent $event
  54.      */
  55.     public function onSongRemovedEvent(SongRemovedEvent $event): void
  56.     {
  57.         if ($this->cookieService->getJwtCookie()) {
  58.             $this->eventDispatcher->dispatch(new LogEvent($event->getSong(), $this->authenticationService->getUser(), EventLog::TYPE_SONG_REMOVED));
  59.         }
  60.     }
  61. }