<?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 BackendCustomerVoter extends Voter 
{ 
    const BACKEND_CUSTOMER_VIEW = 'BACKEND_CUSTOMER_VIEW'; 
    const BACKEND_CUSTOMER_SHOW = 'BACKEND_CUSTOMER_SHOW'; 
    const BACKEND_CUSTOMER_EDIT = 'BACKEND_CUSTOMER_EDIT'; 
    const BACKEND_CUSTOMER_INACTIVE = 'BACKEND_CUSTOMER_INACTIVE'; 
    const BACKEND_CUSTOMER_DELETE = 'BACKEND_CUSTOMER_DELETE'; 
    const BACKEND_CUSTOMER_BILLING_STATUS = 'BACKEND_CUSTOMER_BILLING_STATUS'; 
 
    const BACKEND_CUSTOMER_USERS_VIEW = 'BACKEND_CUSTOMER_USERS_VIEW'; 
    const BACKEND_CUSTOMER_TEAM_CHANGE = 'BACKEND_CUSTOMER_TEAM_CHANGE'; 
    const BACKEND_CUSTOMER_INVOICES_VIEW = 'BACKEND_CUSTOMER_INVOICES_VIEW'; 
    const BACKEND_CUSTOMER_TRANSACTIONS_VIEW = 'BACKEND_CUSTOMER_TRANSACTIONS_VIEW'; 
    const BACKEND_CUSTOMER_PLAN_CHANGES_VIEW = 'BACKEND_CUSTOMER_PLAN_CHANGES_VIEW'; 
    const BACKEND_CUSTOMER_KEYS_VIEW = 'BACKEND_CUSTOMER_KEYS_VIEW'; 
 
    const BACKEND_CUSTOMER_TRANSACTION_CHARGE = 'BACKEND_CUSTOMER_TRANSACTION_CHARGE'; 
    const BACKEND_CUSTOMER_TRANSACTION_CANCEL = 'BACKEND_CUSTOMER_TRANSACTION_CANCEL'; 
    const BACKEND_CUSTOMER_TRANSACTION_REMOVE_AUTH = 'BACKEND_CUSTOMER_TRANSACTION_REMOVE_AUTH'; 
 
    const BACKEND_CUSTOMER_PLAN_CHANGE_EDIT = 'BACKEND_CUSTOMER_PLAN_CHANGE_EDIT'; 
    /** 
     * @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::BACKEND_CUSTOMER_VIEW, self::BACKEND_CUSTOMER_SHOW, self::BACKEND_CUSTOMER_EDIT, 
                self::BACKEND_CUSTOMER_INACTIVE, self::BACKEND_CUSTOMER_DELETE, self::BACKEND_CUSTOMER_BILLING_STATUS, 
                self::BACKEND_CUSTOMER_USERS_VIEW, self::BACKEND_CUSTOMER_TRANSACTIONS_VIEW, self::BACKEND_CUSTOMER_INVOICES_VIEW, 
                self::BACKEND_CUSTOMER_KEYS_VIEW, self::BACKEND_CUSTOMER_PLAN_CHANGES_VIEW, self::BACKEND_CUSTOMER_TEAM_CHANGE, 
                self::BACKEND_CUSTOMER_TRANSACTION_CANCEL, self::BACKEND_CUSTOMER_TRANSACTION_CHARGE, self::BACKEND_CUSTOMER_TRANSACTION_REMOVE_AUTH, 
                self::BACKEND_CUSTOMER_PLAN_CHANGE_EDIT 
            ] 
        )) { 
            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::BACKEND_CUSTOMER_VIEW: 
                return $this->canView(); 
            case self::BACKEND_CUSTOMER_SHOW: 
                return $this->canShow(); 
            case self::BACKEND_CUSTOMER_INACTIVE: 
                return $this->canInactive(); 
            case self::BACKEND_CUSTOMER_EDIT: 
                return $this->canEdit(); 
            case self::BACKEND_CUSTOMER_DELETE: 
                return $this->canDelete(); 
            case self::BACKEND_CUSTOMER_TEAM_CHANGE: 
                return $this->canTeamChange(); 
            case self::BACKEND_CUSTOMER_BILLING_STATUS: 
                return $this->canBillingAllow(); 
            case self::BACKEND_CUSTOMER_USERS_VIEW: 
                return $this->canViewUsers(); 
            case self::BACKEND_CUSTOMER_TRANSACTIONS_VIEW: 
                return $this->canViewTransactions(); 
            case self::BACKEND_CUSTOMER_INVOICES_VIEW: 
                return $this->canViewInvoices(); 
            case self::BACKEND_CUSTOMER_KEYS_VIEW: 
                return $this->canViewKeys(); 
            case self::BACKEND_CUSTOMER_PLAN_CHANGES_VIEW: 
                return $this->canViewPlanChanges(); 
            case self::BACKEND_CUSTOMER_PLAN_CHANGE_EDIT: 
                return $this->canEditPlanChange(); 
            case self::BACKEND_CUSTOMER_TRANSACTION_CHARGE: 
                return $this->canTransactionCharge(); 
            case self::BACKEND_CUSTOMER_TRANSACTION_CANCEL: 
                return $this->canTransactionCancel(); 
            case self::BACKEND_CUSTOMER_TRANSACTION_REMOVE_AUTH: 
                return $this->canRemoveAuth(); 
        } 
 
        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('customer', $this->permissions)) { 
            if (in_array('View', $this->permissions['customer'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have Show Permission else return false 
     * @return bool 
     */ 
    private function canShow(): bool 
    { 
        if (array_key_exists('customer', $this->permissions)) { 
            if (in_array('Show', $this->permissions['customer'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have Edit Permission else return false 
     * @return bool 
     */ 
    private function canEdit(): bool 
    { 
        if (array_key_exists('customer', $this->permissions)) { 
            if (in_array('Edit', $this->permissions['customer'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have Change Status Permission else return false 
     * @return bool 
     */ 
    private function canInactive(): bool 
    { 
        if (array_key_exists('customer', $this->permissions)) { 
            if (in_array('Inactive', $this->permissions['customer'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have Delete Permission else return false 
     * @return bool 
     */ 
    private function canDelete(): bool 
    { 
        if (array_key_exists('customer', $this->permissions)) { 
            if (in_array('Delete', $this->permissions['customer'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
 
    /** 
     * Return True if have TeamChange Permission else return false 
     * @return bool 
     */ 
    private function canTeamChange(): bool 
    { 
        if (array_key_exists('customer', $this->permissions)) { 
            if (in_array('TeamChange', $this->permissions['customer'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have ViewUsers Permission else return false 
     * @return bool 
     */ 
    private function canViewUsers(): bool 
    { 
        if (array_key_exists('customerAbo', $this->permissions)) { 
            if (in_array('ViewUsers', $this->permissions['customerAbo'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have ViewTransactions Permission else return false 
     * @return bool 
     */ 
    private function canViewTransactions(): bool 
    { 
        if (array_key_exists('customerAbo', $this->permissions)) { 
            if (in_array('ViewTransactions', $this->permissions['customerAbo'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have ViewInvoices Permission else return false 
     * @return bool 
     */ 
    private function canViewInvoices(): bool 
    { 
        if (array_key_exists('customerAbo', $this->permissions)) { 
            if (in_array('ViewInvoices', $this->permissions['customerAbo'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have ViewKeys Permission else return false 
     * @return bool 
     */ 
    private function canViewKeys(): bool 
    { 
        if (array_key_exists('customerAbo', $this->permissions)) { 
            if (in_array('ViewKeys', $this->permissions['customerAbo'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have ViewPlanChanges Permission else return false 
     * @return bool 
     */ 
    private function canViewPlanChanges(): bool 
    { 
        if (array_key_exists('customerAbo', $this->permissions)) { 
            if (in_array('ViewPlanChanges', $this->permissions['customerAbo'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have BillingAllow Permission else return false 
     * @return bool 
     */ 
    private function canBillingAllow(): bool 
    { 
        if (array_key_exists('customerInvoices', $this->permissions)) { 
            if (in_array('BillingAllow', $this->permissions['customerInvoices'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have Charge Permission else return false 
     * @return bool 
     */ 
    private function canTransactionCharge(): bool 
    { 
        if (array_key_exists('customerTransactions', $this->permissions)) { 
            if (in_array('Charge', $this->permissions['customerTransactions'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have Cancellation Permission else return false 
     * @return bool 
     */ 
    private function canTransactionCancel(): bool 
    { 
        if (array_key_exists('customerTransactions', $this->permissions)) { 
            if (in_array('Cancellation', $this->permissions['customerTransactions'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have RemoveAuth Permission else return false 
     * @return bool 
     */ 
    private function canRemoveAuth(): bool 
    { 
        if (array_key_exists('customerTransactions', $this->permissions)) { 
            if (in_array('RemoveAuth', $this->permissions['customerTransactions'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have Edit Permission else return false 
     * @return bool 
     */ 
    private function canEditPlanChange(): bool 
    { 
        if (array_key_exists('customerPlanChanges', $this->permissions)) { 
            if (in_array('Edit', $this->permissions['customerPlanChanges'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
 
}