src/Security/OrderVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\System\Employee;
  4. use App\Entity\System\Order;
  5. use App\Entity\System\OrderStatus;
  6. use App\Entity\System\PaymentMethod;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class OrderVoter extends Voter
  10. {
  11.     public const PUBLISH 'publish';
  12.     public const CHECK_PAYPAL 'publish';
  13.     /**
  14.      * @param Order $subject
  15.      */
  16.     protected function supports(string $attribute$subject): bool
  17.     {
  18.         if (!(get_class($subject) === Order::class)) {
  19.             return false;
  20.         }
  21.         return $this->supportsAttribute($attribute);
  22.     }
  23.     public function supportsAttribute(string $attribute): bool
  24.     {
  25.         $voter = new \ReflectionClass(__CLASS__);
  26.         $allowedValues $voter->getConstants();
  27.         if (!\array_key_exists($attribute, \array_flip($allowedValues))) {
  28.             return false;
  29.         }
  30.         return true;
  31.     }
  32.     /**
  33.      * @param Order $order
  34.      */
  35.     protected function voteOnAttribute(string $attribute$orderTokenInterface $token): bool
  36.     {
  37.         $user $token->getUser();
  38.         if (!$user instanceof Employee) {
  39.             return false;
  40.         }
  41.         switch ($attribute) {
  42.             case self::PUBLISH:
  43.                 return $this->publishAllowed($order);
  44.             case self::CHECK_PAYPAL:
  45.                 return $this->checkPayPal($order);
  46.             default:
  47.                 throw new \LogicException('This code should not be reached!');
  48.         }
  49.     }
  50.     private function publishAllowed(Order $order): bool
  51.     {
  52.         return $order->getOrderStatus() !== OrderStatus::PROCESSING;
  53.     }
  54.     private function checkPayPal(Order $order): bool
  55.     {
  56.         if ($order->getPaypalPayerEmail() !== null || $order->getTransactionId() === null) {
  57.             return false;
  58.         }
  59.         return $order->getPayment() === PaymentMethod::PAYMENT_METHOD_PAYPAL;
  60.     }
  61. }