<?php
namespace App\Security\Voters;
use App\Entity\SustainabilityOption;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class SustainabilityOptionVoter extends Voter
{
public const CAN_BE_DELETED = 'option.can_be_deleted';
private const ATTRIBUTES = [
self::CAN_BE_DELETED,
];
protected function supports($attribute, $subject): bool
{
return in_array($attribute, self::ATTRIBUTES);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
switch ($attribute) {
case self::CAN_BE_DELETED:
return $this->canBeDeleted($subject);
}
throw new \LogicException('Invalid attribute: '.$attribute);
}
private function canBeDeleted(SustainabilityOption $option): bool
{
if (count($option->getPriceMaps())) return false;
return true;
}
}