<?php
namespace App\Security\Voter;
use App\Entity\Purchase;
use App\Entity\User;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class PurchaseVoter extends Voter
{
protected function supports($attribute, $subject): bool
{
$supportsAttribute = $attribute == 'delete';
$supportsSubject = $subject instanceof Purchase;
return $supportsAttribute && $supportsSubject;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$authenticatedUser = $token->getUser();
if (!$authenticatedUser instanceof User) return false;
/** @var Purchase $purchase */
$purchase = $subject;
return $purchase->getTransactions()->isEmpty();
}
}