<?php
namespace Roothirsch\DamBundle\EventSubscriber;
// api/src/EventSubscriber/BookMailSubscriber.php
use ApiPlatform\Core\EventListener\EventPriorities;
use Doctrine\ORM\EntityManagerInterface;
use Roothirsch\DamBundle\Entity\Asset;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
final class AssetToggleFavoriteSubscriber implements EventSubscriberInterface
{
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
* @var EntityManagerInterface
*/
private $entityManager;
public function __construct(
TokenStorageInterface $tokenStorage,
EntityManagerInterface $entityManager
) {
$this->tokenStorage = $tokenStorage;
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['validate', EventPriorities::POST_VALIDATE],
];
}
public function validate(\Symfony\Component\HttpKernel\Event\ViewEvent $event)
{
if ($event->getRequest()->attributes->get('_route') === 'api_dam/assets_toggle_favorite_item') {
/** @var Asset $asset */
$asset = $event->getControllerResult();
$asset->toggleFavorite($this->tokenStorage->getToken()->getUser());
}
}
}