<?php
namespace AppBundle\Security;
use AppBundle\Entity\Field\Field;
use AppBundle\Manager\FieldManager;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
* Class FieldVoter.
*/
class FieldVoter extends Voter
{
const EDIT = 'EDIT';
const DELETE = 'DELETE';
/**
* @var FieldManager
*/
private $fieldManager;
/**
* FieldVoter constructor.
* @param FieldManager $fieldManager
*/
public function __construct(FieldManager $fieldManager)
{
$this->fieldManager = $fieldManager;
}
/**
* {@inheritdoc}
*/
protected function supports($attribute, $subject)
{
return (self::EDIT === $attribute || self::DELETE === $attribute)
&& $subject instanceof Field;
}
/**
* {@inheritdoc}
* @param Field $subject
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
switch ($attribute) {
case self::EDIT:
return true;
case self::DELETE:
if ($subject->isLocked()) {
return false;
}
return !$this->fieldManager->isFieldUsed($subject);
default:
throw new \Exception(sprintf('Attribute "%s" not supported.', $attribute));
}
}
}