src/Security/Voter/QuotationVoter.php line 14

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\Cart;
  5. use App\Entity\Quotation;
  6. use App\Enum\Order\Status;
  7. use App\Repository\QuotationRepository;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. class QuotationVoter extends Voter
  11. {
  12.     private QuotationRepository  $quotationRepository;
  13.     public function __construct(QuotationRepository $quotationRepository)
  14.     {
  15.         $this->quotationRepository $quotationRepository;
  16.     }
  17.     protected function supports(string $attribute$subject): bool
  18.     {
  19.         return ($attribute === 'QUOTATION_NOT_CONFIRM' && ($subject instanceof Quotation || $subject instanceof Cart))
  20.             || ($attribute === 'QUOTATION_EXTRA_VIEW' && ($subject instanceof Quotation));
  21.     }
  22.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  23.     {
  24.         if ($attribute === 'QUOTATION_NOT_CONFIRM') {
  25.             if ($subject instanceof Cart) {
  26.                 if (!$subject->isReservation()) {
  27.                     return true;
  28.                 }
  29.                 /** @var Quotation $subject */
  30.                 $subject $this->quotationRepository->findOneBy(['cart' => $subject]);
  31.                 if ($subject === null) {
  32.                     return false;
  33.                 }
  34.             }
  35.             return $subject->getCart() !== null;
  36.         }
  37.         if ($subject instanceof Quotation) {
  38.             return $subject->getCode() !== null && (
  39.                 $subject->getCart() !== null
  40.                     || ($subject->getOrder() !== null
  41.                         && $subject->getOrder()->getStatus()->getValue() === Status::PENDING)
  42.             );
  43.         }
  44.         return false;
  45.     }
  46. }