src/Component/Core/Vendor/Security/Voter/OrderOperationVoter.php line 20

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file was created by developers working at BitBag
  4.  * Do you need more information about us and what we do? Visit our https://bitbag.io website!
  5.  * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
  6. */
  7. declare(strict_types=1);
  8. namespace BitBag\OpenMarketplace\Component\Core\Vendor\Security\Voter;
  9. use BitBag\OpenMarketplace\Component\Order\Entity\OrderInterface;
  10. use SM\Factory\FactoryInterface;
  11. use Sylius\Component\Core\OrderPaymentStates;
  12. use Sylius\Component\Order\OrderTransitions;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  15. final class OrderOperationVoter extends Voter
  16. {
  17.     public const PREFIX 'VENDOR_ORDER_';
  18.     public const CANCEL self::PREFIX 'CANCEL';
  19.     public function __construct(
  20.         private FactoryInterface $stateMachineFactory
  21.     ) {
  22.     }
  23.     public function supportsAttribute(string $attribute): bool
  24.     {
  25.         return str_starts_with($attributeself::PREFIX);
  26.     }
  27.     public function supportsType(string $subjectType): bool
  28.     {
  29.         return is_a($subjectTypeOrderInterface::class, true);
  30.     }
  31.     protected function supports(string $attribute$subject): bool
  32.     {
  33.         if (self::CANCEL === $attribute && $subject instanceof OrderInterface) {
  34.             return true;
  35.         }
  36.         return false;
  37.     }
  38.     protected function voteOnAttribute(
  39.         string $attribute,
  40.         $subject,
  41.         TokenInterface $token
  42.     ): bool {
  43.         return match ($attribute) {
  44.             self::CANCEL => $this->canCancel($subject),
  45.             default => throw new \LogicException(sprintf('Unsupported attribute: "%s"'$attribute))
  46.         };
  47.     }
  48.     private function canCancel(OrderInterface $order): bool
  49.     {
  50.         $stateMachine $this->stateMachineFactory->get($orderOrderTransitions::GRAPH);
  51.         return $stateMachine->can(OrderTransitions::TRANSITION_CANCEL)
  52.             && OrderPaymentStates::STATE_PAID === $order->getPaymentState();
  53.     }
  54. }