src/Security/Voters/StripeVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\CustomerQuotations;
  4. use App\Entity\PriceInquiry;
  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 StripeVoter extends Voter
  10. {
  11.     private Security $security;
  12.     public const INQUIRY_CAN_CHANGE_TO_ACCEPTED   'inquiry.can_be_accepted';
  13.     public const QUOTATION_CAN_CHANGE_TO_ACCEPTED 'quotation.can_be_accepted';
  14.     private const ATTRIBUTES = [
  15.         self::INQUIRY_CAN_CHANGE_TO_ACCEPTED,
  16.         self::QUOTATION_CAN_CHANGE_TO_ACCEPTED,
  17.     ];
  18.     private WorkflowHelper $workflowHelper;
  19.     public function __construct(
  20.         Security $security,
  21.         WorkflowHelper $workflowHelper
  22.     )
  23.     {
  24.         $this->security $security;
  25.         $this->workflowHelper $workflowHelper;
  26.     }
  27.     protected function supports($attribute$subject): bool
  28.     {
  29.         return in_array($attributeself::ATTRIBUTES);
  30.     }
  31.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  32.     {
  33.         switch ($attribute) {
  34.             case self::INQUIRY_CAN_CHANGE_TO_ACCEPTED:
  35.                 return $this->inquiryCanBeAccepted($subject);
  36.             case self::QUOTATION_CAN_CHANGE_TO_ACCEPTED:
  37.                 return $this->quotationCanBeAccepted($subject);
  38.         }
  39.         throw new \LogicException('Invalid attribute: ' $attribute);
  40.     }
  41.     private function inquiryCanBeAccepted(PriceInquiry $inquiry): bool
  42.     {
  43.         if (!$inquiry->getQuotation()->getIsWebShopRelated()) {
  44.             return false;
  45.         }
  46.         if ( !in_array($inquiry->getStatus(), [PriceInquiry::STATUS_OFFER]) ) {
  47.             return false;
  48.         }
  49.         return true;
  50.     }
  51.     private function quotationCanBeAccepted(CustomerQuotations $quotation): bool
  52.     {
  53.         if ($this->workflowHelper->isSubjectStatusGreaterOrEqual($quotationCustomerQuotations::STATUS_ACCEPTED) ) {
  54.             return false;
  55.         }
  56.         return true;
  57.     }
  58. }