src/Security/OrderVoter.php line 13

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 App\Service\EnvironmentService;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. class OrderVoter extends Voter
  11. {
  12. public const PUBLISH = 'publish';
  13. public const CHECK_PAYPAL = 'publish';
  14. public const VIRTUAL_MARK_TRANSFER_PROCESSED = 'virtual_mark_transfer_processed';
  15. private EnvironmentService $environmentService;
  16. public function __construct(EnvironmentService $environmentService)
  17. {
  18. $this->environmentService = $environmentService;
  19. }
  20. /**
  21. * @param Order $subject
  22. */
  23. protected function supports(string $attribute, $subject): bool
  24. {
  25. if (!(get_class($subject) === Order::class)) {
  26. return false;
  27. }
  28. return $this->supportsAttribute($attribute);
  29. }
  30. public function supportsAttribute(string $attribute): bool
  31. {
  32. $voter = new \ReflectionClass(__CLASS__);
  33. $allowedValues = $voter->getConstants();
  34. if (!\array_key_exists($attribute, \array_flip($allowedValues))) {
  35. return false;
  36. }
  37. return true;
  38. }
  39. /**
  40. * @param Order $order
  41. */
  42. protected function voteOnAttribute(string $attribute, $order, TokenInterface $token): bool
  43. {
  44. $user = $token->getUser();
  45. if (!$user instanceof Employee) {
  46. return false;
  47. }
  48. switch ($attribute) {
  49. case self::PUBLISH:
  50. return $this->publishAllowed($order);
  51. case self::CHECK_PAYPAL:
  52. return $this->checkPayPal($order);
  53. case self::VIRTUAL_MARK_TRANSFER_PROCESSED:
  54. return $this->virtualMarkProcessed($order);
  55. default:
  56. throw new \LogicException('This code should not be reached!');
  57. }
  58. }
  59. private function publishAllowed(Order $order): bool
  60. {
  61. return $order->getOrderStatusId() !== OrderStatus::PROCESSING;
  62. }
  63. private function checkPayPal(Order $order): bool
  64. {
  65. return $order->getOrderStatusId() === OrderStatus::PAYPAL_VALIDATION_PENDING
  66. && $order->getTransactionId() !== null
  67. && $order->getPaymentMethod()->getId() === PaymentMethod::PAYPAL_PAYMENT_METHOD_ID;
  68. }
  69. private function virtualMarkProcessed(Order $order): bool
  70. {
  71. return $this->environmentService->isDev() && $order->getCart()->isVirtual()
  72. && $order->getOrderStatusId() === OrderStatus::PENDING_PAYMENT
  73. && $order->getPaymentMethod()->getId() === PaymentMethod::TRANSFER_PAYMENT_METHOD_ID;
  74. }
  75. }