<?php
namespace App\Security\Voters;
use App\Entity\CustomerQuotations;
use App\Entity\PriceInquiry;
use App\Service\WorkflowHelper;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class StripeVoter extends Voter
{
private Security $security;
public const INQUIRY_CAN_CHANGE_TO_ACCEPTED = 'inquiry.can_be_accepted';
public const QUOTATION_CAN_CHANGE_TO_ACCEPTED = 'quotation.can_be_accepted';
private const ATTRIBUTES = [
self::INQUIRY_CAN_CHANGE_TO_ACCEPTED,
self::QUOTATION_CAN_CHANGE_TO_ACCEPTED,
];
private WorkflowHelper $workflowHelper;
public function __construct(
Security $security,
WorkflowHelper $workflowHelper
)
{
$this->security = $security;
$this->workflowHelper = $workflowHelper;
}
protected function supports($attribute, $subject): bool
{
return in_array($attribute, self::ATTRIBUTES);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
switch ($attribute) {
case self::INQUIRY_CAN_CHANGE_TO_ACCEPTED:
return $this->inquiryCanBeAccepted($subject);
case self::QUOTATION_CAN_CHANGE_TO_ACCEPTED:
return $this->quotationCanBeAccepted($subject);
}
throw new \LogicException('Invalid attribute: ' . $attribute);
}
private function inquiryCanBeAccepted(PriceInquiry $inquiry): bool
{
if (!$inquiry->getQuotation()->getIsWebShopRelated()) {
return false;
}
if ( !in_array($inquiry->getStatus(), [PriceInquiry::STATUS_OFFER]) ) {
return false;
}
return true;
}
private function quotationCanBeAccepted(CustomerQuotations $quotation): bool
{
if ($this->workflowHelper->isSubjectStatusGreaterOrEqual($quotation, CustomerQuotations::STATUS_ACCEPTED) ) {
return false;
}
return true;
}
}