<?php
declare(strict_types=1);
namespace App\Point\Security\Voter;
use App\Point\Doctrine\Entity\Purchase;
use App\Program\Doctrine\Entity\Profile;
use App\Program\Doctrine\Entity\Program;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
final class PurchaseVoter extends Voter
{
protected function supports(string $attribute, mixed $subject): bool
{
return $subject instanceof Purchase && 'item' === $attribute;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
/** @var Purchase $purchase */
$purchase = $subject;
/** @var Program $program */
$program = $token->getUser();
/** @var Profile $profile */
$profile = $purchase->getAccount()->getProfile();
return $profile->getProgram() === $program;
}
}