src/EventSubscriber/Vendor/VendorPreWriteSubscriber.php line 56

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 15/03/2019
  6.  * Time: 04:13 PM
  7.  */
  8. namespace App\EventSubscriber\Vendor;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\ViewEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. use ApiPlatform\Core\EventListener\EventPriorities;
  18. use App\Exception\AccessDeniedException;
  19. use App\Services\VendorService;
  20. use App\Services\UtilsService;
  21. use App\Entity\App\User;
  22. use App\Entity\App\Role;
  23. use App\Entity\App\Vendor;
  24. class VendorPreWriteSubscriber implements EventSubscriberInterface
  25. {
  26.     private $tokenStorage;
  27.     private $authorizationChecker;
  28.     private $vendorService;
  29.     private $utilsService;
  30.     private $translator;
  31.     private $entityManager;
  32.     public function __construct(
  33.         TokenStorageInterface $tokenStorage,
  34.         AuthorizationCheckerInterface $checker,
  35.         VendorService $vendorService,
  36.         UtilsService $utilsService,
  37.         TranslatorInterface $translator,
  38.         EntityManagerInterface $entityManager
  39.     ){
  40.         $this->tokenStorage $tokenStorage;
  41.         $this->authorizationChecker $checker;
  42.         $this->vendorService $vendorService;
  43.         $this->utilsService $utilsService;
  44.         $this->translator $translator;
  45.         $this->entityManager $entityManager;
  46.     }
  47.     /**
  48.      * @param ViewEvent $event
  49.      * @throws AccessDeniedException
  50.      */
  51.     public function onKernelView(ViewEvent $event)
  52.     {
  53.         if ($this->utilsService->isAPublicRequest($event)) {
  54.             return;
  55.         }
  56.         $vendor $event->getControllerResult();
  57.         $request $event->getRequest();
  58.         $method $request->getMethod();
  59.         $userCurrent $this->tokenStorage->getToken()->getUser();
  60.         if (!($userCurrent instanceof User) || !($vendor instanceof Vendor)
  61.         ) {
  62.             return;
  63.         }
  64.         if(Request::METHOD_POST === $method) {
  65.             $baseCode strtoupper(iconv('UTF-8','ASCII//TRANSLIT'preg_replace('/\s/'''$vendor->getName())));
  66.             $baseCode str_replace("'"'',$baseCode);
  67.             $code $baseCode;
  68.             do{
  69.                 $vendorWithCode $this->entityManager->getRepository(Vendor::class)->findOneBy(['signUpCode' => $code]);
  70.                 if(!$vendorWithCode instanceof  Vendor)
  71.                     break;
  72.                 $code $baseCode rand(1,999);
  73.             }while(true);
  74.             $vendor->setSignUpCode($code);
  75.             if(empty($vendor->getTimezone())){
  76.                 $vendor->setTimezone('UTC');
  77.             }
  78.             if(empty($vendor->getCountry())){
  79.                 $vendor->setCountry('ES');
  80.             }
  81.             if(empty($vendor->getDateFormat())){
  82.                 $vendor->setDateFormat(1);
  83.             }
  84.             if(empty($vendor->getCurrency())){
  85.                 $vendor->setCurrency('EUR');
  86.             }
  87.         }
  88.         if (Request::METHOD_PUT !== $method && Request::METHOD_DELETE !== $method) {
  89.             return;
  90.         }
  91.         if ($this->authorizationChecker->isGranted('ROLE_SUPERADMIN'))
  92.         {
  93.             return;
  94.         }
  95.         $controlAccess = [Role::ROLE_ADMIN];
  96.         if (!$this->vendorService->isUserRoleInToVendor($vendor$userCurrent$controlAccess)) {
  97.             $controlAccessTranslator = [];
  98.             foreach ($controlAccess as $roleName) {
  99.                 $controlAccessTranslator[] = $this->translator->trans($roleName);
  100.             }
  101.             throw new AccessDeniedException(
  102.                 $this->translator->trans('access_allowed_only_for') . (implode(', '$controlAccessTranslator))
  103.             );
  104.         }
  105.     }
  106.     public static function getSubscribedEvents()
  107.     {
  108.         return [
  109.             KernelEvents::VIEW => ['onKernelView'EventPriorities::PRE_WRITE]
  110.         ];
  111.     }
  112. }