<?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 WishlistVoter extends Voter 
{ 
    const WISHLIST_VIEW = 'WISHLIST_VIEW'; 
    const WISHLIST_CREATE = 'WISHLIST_CREATE'; 
    const WISHLIST_LIKE = 'WISHLIST_LIKE'; 
    const WISHLIST_DISLIKE = 'WISHLIST_DISLIKE'; 
 
    /** 
     * @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 the attribute isn't one we support, return false 
        if (!in_array($attribute, 
            [self::WISHLIST_VIEW, self::WISHLIST_LIKE, self::WISHLIST_CREATE, self::WISHLIST_DISLIKE] 
        )) { 
            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->getUserPermissions($user); 
 
        switch ($attribute) { 
            case self::WISHLIST_VIEW: 
                return $this->canView(); 
            case self::WISHLIST_CREATE: 
                return $this->canCreate(); 
            case self::WISHLIST_LIKE: 
                return $this->canLike(); 
            case self::WISHLIST_DISLIKE: 
                return $this->canDislike(); 
        } 
 
        throw new LogicException('Invalid attribute: ' . $attribute); 
    } 
 
    /** 
     * Return True if have View Permission else return false 
     * @return bool 
     */ 
    private function canView(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
 
        if (array_key_exists('wishList', $this->permissions)) { 
            if (in_array('View', $this->permissions['wishList'])) { 
                return true; 
            } 
        } 
 
        return false; 
 
    } 
 
    /** 
     * Return True if have Like Permission else return false 
     * @return bool 
     */ 
    private function canLike(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
        if (array_key_exists('wishList', $this->permissions)) { 
            if (in_array('Like', $this->permissions['wishList'])) { 
                return true; 
            } 
        } 
 
        return false; 
    } 
 
    /** 
     * Return True if have Create Permission else return false 
     * @return bool 
     */ 
    private function canCreate(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
        if (array_key_exists('wishList', $this->permissions)) { 
            if (in_array('Create', $this->permissions['wishList'])) { 
                return true; 
            } 
        } 
 
        return false; 
    } 
 
    /** 
     * Return True if have DisLike Permission else return false 
     * @return bool 
     */ 
    private function canDislike(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
        if (array_key_exists('wishList', $this->permissions)) { 
            if (in_array('Dislike', $this->permissions['wishList'])) { 
                return true; 
            } 
        } 
 
        return false; 
    } 
}