<?php
declare(strict_types=1);
namespace App\Infrastructure\Security\Voter;
use App\Domain\Cart\DTO\Factory\CartDTOFactory;
use App\Domain\Common\Entity\IzOrderClient;
use App\Domain\Common\Repository\CartRepository;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
final class OrderVoter extends Voter
{
public const VALIDATION = 'ORDER_VALIDATION';
private CartRepository $cartRepository;
private CartDTOFactory $cartFactory;
public function __construct(
CartRepository $cartRepository,
CartDTOFactory $cartFactory
) {
$this->cartRepository = $cartRepository;
$this->cartFactory = $cartFactory;
}
protected function supports(string $attribute, $subject): bool
{
return self::VALIDATION === $attribute;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$client = $token->getUser();
if (!$client instanceof IzOrderClient) {
return false;
}
$cartProducts = $this->cartRepository->findForSelf();
if (empty($cartProducts)) {
return false;
}
return $this->cartFactory->build()->getTotalAmount() >= $client->getFranco();
}
}