<?php
namespace App\Security\Voter;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class UserSetupVoter extends Voter {
const SETUP_COMPLETE = 'setup-complete';
private $validator;
public function __construct(ValidatorInterface $validator)
{
$this->validator = $validator;
}
protected function supports($attribute, $subject)
{
if(!in_array($attribute, [self::SETUP_COMPLETE])){
return false;
}
if(!$subject instanceof User){
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if(!$user instanceof User){
return false;
}
switch ($attribute) {
case self::SETUP_COMPLETE:
return $this->isSetupComplete($user);
}
throw new \LogicException('This code should not be reached!');
}
private function isSetupComplete(User $user){
$errors = $this->validator->validate($user);
if(empty($user->getCompany()) || empty($user->getAddress()) || empty($user->getWebsite())){
return false;
}
return true;
}
}