src/Security/Voters/ShopVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\CustomerQuotations;
  4. use App\Service\Constants\InvoiceStatus;
  5. use App\Service\WorkflowHelper;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. class ShopVoter extends Voter
  10. {
  11.     public const ORDER_IS_PAID   'order.is_paid';
  12.     public const BEFORE_PAYMENT  'order.before_payment';
  13.     public const AFTER_PAYMENT   'order.after_payment';
  14.     public const ORDER_CAN_CUSTOMER_CONNECT 'order.can_customer_connect';
  15.     public const ORDER_CAN_SAVE_FOR_LATER_SECURED 'order.can_save_for_later_secured';
  16.     public const ORDER_NEED_INVOICE_PAYMENT_STAGE 'order.need_invoice_payment_stage';
  17.     private const ATTRIBUTES = [
  18.         self::ORDER_IS_PAID,
  19.         self::BEFORE_PAYMENT,
  20.         self::AFTER_PAYMENT,
  21.         self::ORDER_CAN_CUSTOMER_CONNECT,
  22.         self::ORDER_CAN_SAVE_FOR_LATER_SECURED,
  23.         self::ORDER_NEED_INVOICE_PAYMENT_STAGE,
  24.     ];
  25.     private Security $security;
  26.     private WorkflowHelper $workflowHelper;
  27.     public function __construct(
  28.         Security $security,
  29.         WorkflowHelper $workflowHelper
  30.     )
  31.     {
  32.         $this->security $security;
  33.         $this->workflowHelper $workflowHelper;
  34.     }
  35.     protected function supports($attribute$subject): bool
  36.     {
  37.         return in_array($attributeself::ATTRIBUTES);
  38.     }
  39.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  40.     {
  41.         switch ($attribute) {
  42.             case self::ORDER_IS_PAID:
  43.             case self::AFTER_PAYMENT:
  44.                 return $this->orderIsPaid($subject);
  45.             case self::BEFORE_PAYMENT:
  46.                 return !$this->orderIsPaid($subject);
  47.             case self::ORDER_CAN_CUSTOMER_CONNECT:
  48.                 return $this->orderCanCustomerConnect($subject);
  49.             case self::ORDER_CAN_SAVE_FOR_LATER_SECURED:
  50.                 return $this->orderCanSaveForLaterSecured($subject);
  51.             case self::ORDER_NEED_INVOICE_PAYMENT_STAGE:
  52.                 return $this->needInvoicePaymentStage($subject);
  53.         }
  54.         throw new \LogicException('Invalid attribute: ' $attribute);
  55.     }
  56.     private function orderIsPaid(CustomerQuotations $order): bool
  57.     {
  58.         if (!$this->workflowHelper->isSubjectStatusGreaterOrEqual($orderCustomerQuotations::STATUS_ACCEPTED)) {
  59.             return false;
  60.         }
  61.         if (!$order->getIsWebShopRelated()) {
  62.             return false;
  63.         }
  64.         foreach ($order->getInvoices() as $regInvoice) {
  65.             if ($regInvoice->getIsWebShopRelated()) {
  66.                 if ($regInvoice->getStatus() === InvoiceStatus::INVOICE_STATUS_PAID_FULLY) {
  67.                     return true;
  68.                 }
  69.             }
  70.         }
  71.         return false;
  72.     }
  73.     private function orderCanCustomerConnect(CustomerQuotations $order): bool
  74.     {
  75.         if ($order->getCustomer()) {
  76.             return false;
  77.         }
  78.         if (!in_array($order->getStatus(), [
  79.             CustomerQuotations::STATUS_ACCEPTED,
  80.             CustomerQuotations::STATUS_WAITING_PAYMENT,
  81.             CustomerQuotations::STATUS_DRAFT
  82.         ])) {
  83.             return false;
  84.         }
  85.         if (!$order->getIsWebShopRelated()) {
  86.             return false;
  87.         }
  88.         return true;
  89.     }
  90.     private function orderCanSaveForLaterSecured(CustomerQuotations $order): bool
  91.     {
  92.         if (!$order->getIsWebShopRelated()) {
  93.             return false;
  94.         }
  95.         if (!in_array($order->getStatus(), [
  96.             CustomerQuotations::STATUS_DRAFT
  97.         ])) {
  98.             return false;
  99.         }
  100.         if ($order->getCustomer()) {
  101.             return $order->getCustomer() === $this->security->getUser()->getCustomer();
  102.         }
  103.         return true;
  104.     }
  105.     private function needInvoicePaymentStage(CustomerQuotations $order): bool
  106.     {
  107.         foreach ($order->getInvoices() as $regInvoice) {
  108.             if ($regInvoice->getIsWebShopRelated()) {
  109.                 return false;
  110.             }
  111.         }
  112.         return true;
  113.     }
  114. }