<?php
namespace App\EventSubscriber\Song;
use App\Entity\Logs\EventLog;
use App\Event\LogEvent;
use App\Event\Song\SongCreatedEvent;
use App\Event\Song\SongRemovedEvent;
use App\Service\AuthenticationService;
use App\Service\CookieService;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SongSubscriber implements EventSubscriberInterface
{
/**
* @var AuthenticationService
*/
private $authenticationService;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* @var CookieService
*/
private $cookieService;
public function __construct(
AuthenticationService $authenticationService,
EventDispatcherInterface $eventDispatcher,
CookieService $cookieService
)
{
$this->eventDispatcher = $eventDispatcher;
$this->authenticationService = $authenticationService;
$this->cookieService = $cookieService;
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [
SongCreatedEvent::class => 'onSongCreatedEvent',
SongRemovedEvent::class => 'onSongRemovedEvent'
];
}
/**
* @param SongCreatedEvent $event
*/
public function onSongCreatedEvent(SongCreatedEvent $event): void
{
$this->eventDispatcher->dispatch(new LogEvent($event->getSong(), $this->authenticationService->getUser(), EventLog::TYPE_SONG_CREATED));
}
/**
* @param SongRemovedEvent $event
*/
public function onSongRemovedEvent(SongRemovedEvent $event): void
{
if ($this->cookieService->getJwtCookie()) {
$this->eventDispatcher->dispatch(new LogEvent($event->getSong(), $this->authenticationService->getUser(), EventLog::TYPE_SONG_REMOVED));
}
}
}