<?php declare(strict_types=1);
namespace App\Security\Voter;
use App\Constant\EntityStatus;
use App\Entity\Experience;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class ExperienceVoter extends Voter
{
const CAN_VIEW = 'CAN_VIEW';
protected function supports($attribute, $subject): bool
{
return $attribute === self::CAN_VIEW;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
switch ($attribute) {
case self::CAN_VIEW:
return $this->canView($subject);
}
throw new \LogicException(sprintf('Attribute "%s" not handled', $attribute));
}
private function canView($subject): bool
{
if (!$subject instanceof Experience) {
return false;
}
return !in_array($subject->getStatus(), [EntityStatus::DRAFT]);
}
}