vendor/roothirsch/dam-bundle/EventSubscriber/AssetToggleFavoriteSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\DamBundle\EventSubscriber;
  3. // api/src/EventSubscriber/BookMailSubscriber.php
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Roothirsch\DamBundle\Entity\Asset;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. final class AssetToggleFavoriteSubscriber implements EventSubscriberInterface
  11. {
  12. /**
  13. * @var TokenStorageInterface
  14. */
  15. private $tokenStorage;
  16. /**
  17. * @var EntityManagerInterface
  18. */
  19. private $entityManager;
  20. public function __construct(
  21. TokenStorageInterface $tokenStorage,
  22. EntityManagerInterface $entityManager
  23. ) {
  24. $this->tokenStorage = $tokenStorage;
  25. $this->entityManager = $entityManager;
  26. }
  27. public static function getSubscribedEvents()
  28. {
  29. return [
  30. KernelEvents::VIEW => ['validate', EventPriorities::POST_VALIDATE],
  31. ];
  32. }
  33. public function validate(\Symfony\Component\HttpKernel\Event\ViewEvent $event)
  34. {
  35. if ($event->getRequest()->attributes->get('_route') === 'api_dam/assets_toggle_favorite_item') {
  36. /** @var Asset $asset */
  37. $asset = $event->getControllerResult();
  38. $asset->toggleFavorite($this->tokenStorage->getToken()->getUser());
  39. }
  40. }
  41. }