<?php
declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\Cart;
use App\Entity\Quotation;
use App\Enum\Order\Status;
use App\Repository\QuotationRepository;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class QuotationVoter extends Voter
{
private QuotationRepository $quotationRepository;
public function __construct(QuotationRepository $quotationRepository)
{
$this->quotationRepository = $quotationRepository;
}
protected function supports(string $attribute, $subject): bool
{
return ($attribute === 'QUOTATION_NOT_CONFIRM' && ($subject instanceof Quotation || $subject instanceof Cart))
|| ($attribute === 'QUOTATION_EXTRA_VIEW' && ($subject instanceof Quotation));
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
if ($attribute === 'QUOTATION_NOT_CONFIRM') {
if ($subject instanceof Cart) {
if (!$subject->isReservation()) {
return true;
}
/** @var Quotation $subject */
$subject = $this->quotationRepository->findOneBy(['cart' => $subject]);
if ($subject === null) {
return false;
}
}
return $subject->getCart() !== null;
}
if ($subject instanceof Quotation) {
return $subject->getCode() !== null && (
$subject->getCart() !== null
|| ($subject->getOrder() !== null
&& $subject->getOrder()->getStatus()->getValue() === Status::PENDING)
);
}
return false;
}
}