<?php
namespace AppBundle\Security;
use AppBundle\Entity\LawFirm;
use AppBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
* Class FreemiumVoter
*/
class FreemiumVoter extends Voter
{
public const LAW_FIRM_FROM_FREEMIUM = 'law_firm.from_freemium';
public function isFromFreemium(LawFirm $lawFirm): bool
{
return $lawFirm->getZohoId() !== null;
}
/**
* {@inheritdoc}
*/
protected function supports($attribute, $subject): bool
{
if (self::LAW_FIRM_FROM_FREEMIUM === $attribute && (null === $subject || $subject instanceof LawFirm)) {
return true;
}
return false;
}
/**
* {@inheritdoc}
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
switch ($attribute) {
case self::LAW_FIRM_FROM_FREEMIUM:
return $this->isFromFreemium($subject ?? $user->getLawFirm());
default:
throw new \LogicException('This code should not be reached');
}
}
}