<?php 
 
namespace App\Security\Voter; 
 
use App\Entity\User; 
use App\Utils\Commons; 
use LogicException; 
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; 
use Symfony\Component\Security\Core\Authorization\Voter\Voter; 
use Symfony\Component\Security\Core\Security; 
use Symfony\Component\Security\Core\User\UserInterface; 
 
class BackendNewRegistrationVoter extends Voter 
{ 
    const NEW_REGISTRATION_VIEW = 'NEW_REGISTRATION_VIEW'; 
    const NEW_REGISTRATION_EDIT = 'NEW_REGISTRATION_EDIT'; 
    const NEW_REGISTRATION_ASSIGN = 'NEW_REGISTRATION_ASSIGN'; 
    const NEW_REGISTRATION_MESSAGE = 'NEW_REGISTRATION_MESSAGE'; 
    const NEW_REGISTRATION_DELETE = 'NEW_REGISTRATION_DELETE'; 
    /** 
     * @var Security 
     */ 
    private $security; 
    /** 
     * @var Commons 
     */ 
    private $commons; 
    /** 
     * @var array 
     */ 
    private $permissions; 
 
    /** 
     * SupportVoter constructor. 
     * @param Security $security 
     * @param Commons $commons 
     */ 
    public function __construct(Security $security, Commons $commons) 
    { 
        $this->security = $security; 
        $this->commons = $commons; 
        $this->permissions = []; 
    } 
 
    /** 
     * @param string $attribute 
     * @param mixed $subject 
     * @return bool 
     */ 
    protected function supports($attribute, $subject): bool 
    { 
        if (!in_array($attribute, 
            [self::NEW_REGISTRATION_VIEW, self::NEW_REGISTRATION_VIEW,  self::NEW_REGISTRATION_ASSIGN, self::NEW_REGISTRATION_MESSAGE, self::NEW_REGISTRATION_DELETE] 
        )) { 
            return false; 
        } 
        return true; 
    } 
 
    /** 
     * @param string $attribute 
     * @param mixed $subject 
     * @param TokenInterface $token 
     * @return bool 
     */ 
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool 
    { 
        /** @var User $user */ 
        $user = $token->getUser(); 
        // if the user is anonymous, do not grant access 
        if (!$user instanceof UserInterface) { 
            return false; 
        } 
        $this->permissions = $this->commons->getBackendPermissions($user); 
 
        if ($this->hasSuperUser() === true) { 
            return true; 
        } 
 
        switch ($attribute) { 
            case self::NEW_REGISTRATION_VIEW: 
                return $this->canView(); 
            case self::NEW_REGISTRATION_EDIT: 
                return $this->canEdit(); 
            case self::NEW_REGISTRATION_ASSIGN: 
                return $this->canAssign(); 
            case self::NEW_REGISTRATION_MESSAGE: 
                return $this->canMessage(); 
            case self::NEW_REGISTRATION_DELETE: 
                return $this->canDelete(); 
        } 
 
        throw new LogicException('Invalid attribute: ' . $attribute); 
    } 
 
    /** 
     * Return True if have Super View Permission else return false 
     * @return bool 
     */ 
    private function hasSuperUser(): bool 
    { 
        if (array_key_exists('superUser', $this->permissions)) { 
            if (in_array('Yes', $this->permissions['superUser'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
 
    /** 
     * Return True if have View Permission else return false 
     * @return bool 
     */ 
    private function canView(): bool 
    { 
        if (array_key_exists('newRegistrations', $this->permissions)) { 
            if (in_array('View', $this->permissions['newRegistrations'])) { 
                return true; 
            } 
        } 
 
        return false; 
 
    } 
 
    /** 
     * Return True if have Edit Permission else return false 
     * @return bool 
     */ 
    private function canEdit(): bool 
    { 
        if (array_key_exists('newRegistrations', $this->permissions)) { 
            if (in_array('Edit', $this->permissions['newRegistrations'])) { 
                return true; 
            } 
        } 
 
        return false; 
    } 
 
    /** 
     * Return True if have Assign Team Permission else return false 
     * @return bool 
     */ 
    private function canAssign(): bool 
    { 
        if (array_key_exists('newRegistrations', $this->permissions)) { 
            if (in_array('Assign', $this->permissions['newRegistrations'])) { 
                return true; 
            } 
        } 
 
        return false; 
    } 
 
    /** 
     * Return True if have Message Permission else return false 
     * @return bool 
     */ 
    private function canMessage(): bool 
    { 
        if (array_key_exists('newRegistrations', $this->permissions)) { 
            if (in_array('Message', $this->permissions['newRegistrations'])) { 
                return true; 
            } 
        } 
 
        return false; 
    } 
 
    /** 
     * Return True if have Delete Permission else return false 
     * @return bool 
     */ 
    private function canDelete(): bool 
    { 
        if (array_key_exists('newRegistrations', $this->permissions)) { 
            if (in_array('Delete', $this->permissions['newRegistrations'])) { 
                return true; 
            } 
        } 
 
        return false; 
    } 
}