<?php
namespace App\Security\Voter;
use App\Entity\TarifOffreCac;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use App\Constants\TarifOffreCac as ConstantTarif;
class SouscriptionVoter extends Voter {
const CAN_SUBMIT_SOUSCRPTION = "can_submit_souscription";
const IS_PERMANENT = 'is_permanent';
const IS_PONCTUAL = 'is_ponctual';
const ACTIONS = [
self::IS_PERMANENT,
self::IS_PONCTUAL
];
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, self::ACTIONS);
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
{
$oUser = $token->getUser();
$oTarif = $subject;
switch ($attribute) {
case self::IS_PERMANENT:
return $this->isPermanant($oTarif);
default:
throw new \LogicException('This code should not be reached!');
}
}
private function isPermanant(TarifOffreCac $oTarif){
return $oTarif->getBesoin() === ConstantTarif::PERMANENT_NEED;
}
}