<?php
// api/src/EventSubscriber/BookMailSubscriber.php
namespace Roothirsch\ShopBundle\EventSubscriber\Api;
use ApiPlatform\Core\EventListener\EventPriorities;
use Roothirsch\ShopBundle\Entity\Estimate;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
final class EstimateOwnerSubscriber implements EventSubscriberInterface
{
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['validate', EventPriorities::PRE_VALIDATE],
];
}
public function validate(\Symfony\Component\HttpKernel\Event\ViewEvent $event)
{
if (!$event->getControllerResult() instanceof Estimate) {
return;
}
if ($event->getControllerResult()->getOwner() !== null) {
return;
}
$event->getControllerResult()->setOwner($this->tokenStorage->getToken()->getUser());
}
}