src/Security/Voters/OrderVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\CustomerQuotations;
  4. use App\Entity\OrderClaim;
  5. use App\Repository\PackagingTypeRepository;
  6. use App\Service\WorkflowHelper;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. class OrderVoter extends Voter
  11. {
  12.     private Security $security;
  13.     public const CAN_RATE 'order.can_rate';
  14.     public const CAN_DO_CLAIM 'order.can_claim';
  15.     public const CAN_RESOLVE_CLAIM 'order.can_resolve_claim';
  16.     public const CAN_VIEW 'order.can_view_claim';
  17.     public const CAN_REORDER 'order.can_reorder';
  18.     public const CAN_ADD_BATCH 'order.can_add_batch';
  19.     public const CAN_GENERATE_PREPAYMENT_INVOICE 'order.can_generate_prepayment_invoice';
  20.     public const CAN_GENERATE_BATCHES_INVOICE 'order.can_generate_batches_invoice';
  21.     public const IS_STOCK_PRODUCT 'order.is_stock_product';
  22.     private const ATTRIBUTES = [
  23.         self::CAN_RATE,
  24.         self::CAN_DO_CLAIM,
  25.         self::CAN_RESOLVE_CLAIM,
  26.         self::CAN_VIEW,
  27.         self::CAN_REORDER,
  28.         self::CAN_ADD_BATCH,
  29.         self::CAN_GENERATE_PREPAYMENT_INVOICE,
  30.         self::CAN_GENERATE_BATCHES_INVOICE,
  31.         self::IS_STOCK_PRODUCT,
  32.     ];
  33.     private PackagingTypeRepository $packagingTypeRepo;
  34.     private WorkflowHelper $workflowHelper;
  35.     public function __construct(
  36.         Security                $security,
  37.         WorkflowHelper          $workflowHelper,
  38.         PackagingTypeRepository $packagingTypeRepo
  39.     )
  40.     {
  41.         $this->packagingTypeRepo $packagingTypeRepo;
  42.         $this->security $security;
  43.         $this->workflowHelper $workflowHelper;
  44.     }
  45.     protected function supports($attribute$subject): bool
  46.     {
  47.         return in_array($attributeself::ATTRIBUTES);
  48.     }
  49.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  50.     {
  51.         switch ($attribute) {
  52.             case self::CAN_RATE:
  53.                 return $this->catRate($subject);
  54.             case self::CAN_DO_CLAIM:
  55.                 return $this->canClaim($subject);
  56.             case self::CAN_RESOLVE_CLAIM:
  57.                 return $this->canResolveClaim($subject);
  58.             case self::CAN_VIEW:
  59.                 return $this->canViewClaim($subject);
  60.             case self::CAN_REORDER:
  61.                 return $this->canReorder($subject);
  62.             case self::CAN_ADD_BATCH:
  63.                 return $this->canAddBatch($subject);
  64.             case self::CAN_GENERATE_PREPAYMENT_INVOICE:
  65.                 return $this->canGeneratePrePaymentInvoice($subject);
  66.             case self::CAN_GENERATE_BATCHES_INVOICE:
  67.                 return $this->canGenerateBatchesInvoice($subject);
  68.             case self::IS_STOCK_PRODUCT:
  69.                 return $this->isStockProduct($subject);
  70.         }
  71.         throw new \LogicException('Invalid attribute: ' $attribute);
  72.     }
  73.     private function catRate(CustomerQuotations $order): bool
  74.     {
  75.         if ($this->security->isGranted('user.is_manufacturer')) {
  76.             return false;
  77.         }
  78.         if ($order->getOrderRating()) {
  79.             return false;
  80.         }
  81.         if ($order->getStatus() == CustomerQuotations::STATUS_FINISHED) {
  82.             return true;
  83.         }
  84.         return false;
  85.     }
  86.     private function canClaim(CustomerQuotations $order): bool
  87.     {
  88.         if ($this->security->isGranted('user.is_manufacturer')) {
  89.             return false;
  90.         }
  91.         if ($order->getClaim()) {
  92.             return false;
  93.         }
  94.         if ($order->getStatus() == CustomerQuotations::STATUS_FINISHED) {
  95.             return true;
  96.         }
  97.         return false;
  98.     }
  99.     private function canResolveClaim(CustomerQuotations $order): bool
  100.     {
  101.         if ($order->getClaim() && $order->getClaim()->getStatus() != OrderClaim::STATUS_RESOLVED) {
  102.             return true;
  103.         }
  104.         return false;
  105.     }
  106.     private function canViewClaim(CustomerQuotations $order): bool
  107.     {
  108.         return true;
  109.     }
  110.     private function canReorder(CustomerQuotations $order): bool
  111.     {
  112.         if ($this->security->isGranted('user.is_manufacturer')) {
  113.             return false;
  114.         }
  115.         if ($order->getStatus() !== CustomerQuotations::STATUS_FINISHED) {
  116.             return false;
  117.         }
  118.         if (!$this->security->isGranted('ROLE_SALES_MANAGER')) {
  119.             return false;
  120.         }
  121.         return true;
  122.     }
  123.     private function canAddBatch(CustomerQuotations $quotation): bool
  124.     {
  125.         return in_array($quotation->getStatus(), [
  126.             CustomerQuotations::STATUS_PRODUCTION,
  127.             CustomerQuotations::STATUS_PRODUCED,
  128.             CustomerQuotations::STATUS_PRODUCTION_FINISHED,
  129.             CustomerQuotations::STATUS_TRANSPORT
  130.         ]);
  131.     }
  132.     private function canGeneratePrePaymentInvoice(CustomerQuotations $quotation): bool
  133.     {
  134.         if (!$this->workflowHelper->isSubjectStatusGreaterOrEqual($quotationCustomerQuotations::STATUS_MF_CONTRACT_APPROVED)) {
  135.             return false;
  136.         }
  137.         return $this->security->isGranted('ROLE_SALES_MANAGER');
  138.     }
  139.     private function canGenerateBatchesInvoice(CustomerQuotations $quotation): bool
  140.     {
  141.         if (!$this->security->isGranted('ROLE_SALES_MANAGER')) {
  142.             return false;
  143.         }
  144.         if (!count($quotation->getProductionBatches())) {
  145.             return false;
  146.         }
  147.         foreach ($quotation->getProductionBatches() as $batch) {
  148.             if (!count($batch->getInvoices())) {
  149.                 return true;
  150.             }
  151.         }
  152.         return false;
  153.     }
  154.     private function isStockProduct(CustomerQuotations $quotation): bool
  155.     {
  156.         return in_array($quotation->getPackagingType(), $this->packagingTypeRepo->getStockCodes());
  157.     }
  158. }