src/Invoice/Security/InvoiceRepeatRule/InvoiceRepeatRuleVoter.php line 27

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2020 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2020 TECLA Consulting Group oü
  16.  */
  17. namespace App\Invoice\Security\InvoiceRepeatRule;
  18. use App\Invoice\Model\InvoiceRepeatRuleInterface;
  19. use LogicException;
  20. use Symfony\Component\DependencyInjection\ServiceLocator;
  21. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  22. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  23. final class InvoiceRepeatRuleVoter extends Voter
  24. {
  25.     private ServiceLocator $criteria;
  26.     public function __construct(ServiceLocator $criteria)
  27.     {
  28.         $this->criteria $criteria;
  29.     }
  30.     protected function supports(string $attribute$subject): bool
  31.     {
  32.         return $this->criteria->has($attribute) && (null === $subject || true === $subject instanceof InvoiceRepeatRuleInterface);
  33.     }
  34.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  35.     {
  36.         /** @var ?InvoiceRepeatRuleInterface $invoiceRepeatRule */
  37.         $invoiceRepeatRule $subject;
  38.         /** @var CriterionInterface $criterion */
  39.         $criterion = ($this->criteria)($attribute);
  40.         if ($criterion) {
  41.             return $criterion->handle($invoiceRepeatRule);
  42.         }
  43.         throw new LogicException('This code should not be reached!');
  44.     }
  45. }