Skip to content

Instantly share code, notes, and snippets.

@cirykpopeye
Created January 30, 2019 09:43
Show Gist options
  • Save cirykpopeye/64f2e8b8718213723ac72dbc8abddedc to your computer and use it in GitHub Desktop.
Save cirykpopeye/64f2e8b8718213723ac72dbc8abddedc to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: cirykpopeye
* Date: 2019-01-09
* Time: 09:59
*/
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\Taxation;
use App\Exception\LockedException;
use App\Exception\TaxationLockedException;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
class LockTaxationSubscriber implements EventSubscriberInterface
{
private $em;
private $security;
private $translator;
/**
* LockTaxationSubscriber constructor.
* @param Security $security
*/
public function __construct(Security $security, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->security = $security;
$this->em = $em;
$this->translator = $translator;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['handleStatus', EventPriorities::PRE_SERIALIZE]
];
}
public function handleStatus(GetResponseForControllerResultEvent $event)
{
$taxation = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$taxation instanceof Taxation || Request::METHOD_GET !== $method) {
return;
}
//-- Check status
if (
$taxation->getLocked() &&
$taxation->getLockedBy() !== $this->security->getUser()
) {
throw new LockedException($this->translator->trans('label.taxation_locked'));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment