<?php
namespace App\Security\SuperheroOffice;
use App\Entity\SuperheroOffice\Communication;
use App\Handler\SuperheroOffice\UserOfficeHandler;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
* Class CommunicationVoter
*
* @package App\Security\SuperheroOffice
*/
class CommunicationVoter extends Voter
{
const VIEW = 'view';
const COMPLETE = 'complete';
/**
* base voter actions
*/
private const ATTRIBUTES = [
self::VIEW,
self::COMPLETE
];
/**
* @var UserOfficeHandler
*/
private $userOfficeHandler;
/**
* ShareholderVoter constructor.
*
* @param UserOfficeHandler $userOfficeHandler
*/
public function __construct(UserOfficeHandler $userOfficeHandler)
{
$this->userOfficeHandler = $userOfficeHandler;
}
/**
* @param string $attribute
* @param mixed $subject
*
* @return bool
*/
protected function supports($attribute, $subject)
{
return $subject instanceof Communication
&& in_array($attribute, self::ATTRIBUTES);
}
/**
* @param $attribute
* @param Communication $communication
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute(
$attribute,
$communication,
TokenInterface $token
): bool {
switch ($attribute) {
case self::COMPLETE:
case self::VIEW:
return $this
->userOfficeHandler
->getUserOffice()
->getCommunications()
->contains($communication);
break;
}
throw new \LogicException('Invalid attribute: ' . $attribute);
}
}