<?php
namespace App\Security\Voter;
use App\Entity\Form;
use App\Service\ProcessFormService;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class ProcessFormVoter extends Voter
{
public const PROCESS_CREATE = 'PROCESS_CREATE';
public const PROCESS_EDIT = 'PROCESS_EDIT';
public const PROCESS_SUCCESS = 'PROCESS_SUCCESS';
/** @var ProcessFormService $service */
private $service;
/**
* @param ProcessFormService $service
*/
public function __construct(ProcessFormService $service)
{
$this->service = $service;
}
/**
* @param string $attribute
* @param $subject
* @return bool
*/
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, [self::PROCESS_CREATE, self::PROCESS_EDIT, self::PROCESS_SUCCESS]) && $subject instanceof Form;
}
/**
* @param string $attribute
* @param $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
switch ($attribute) {
case self::PROCESS_CREATE:
return $this->canCreate($subject);
case self::PROCESS_EDIT:
return $this->canEdit($subject);
case self::PROCESS_SUCCESS:
return $this->canSuccess($subject);
}
return false;
}
/**
* @param Form $form
* @return bool
*/
private function canCreate(Form $form): bool
{
return $this->service->checkActiveStatus($form);
}
/**
* @param Form $form
* @return bool
*/
private function canEdit(Form $form): bool
{
return $this->service->checkActiveStatus($form);
}
/**
* @param Form $form
* @return bool
*/
private function canSuccess(Form $form): bool
{
return $this->service->checkActiveStatus($form);
}
}