<?php
declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\Project\DeveloperRate;
use App\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class CanSetRate extends Voter
{
/**
* {@inheritDoc}
*/
protected function supports(string $attribute, $subject): bool
{
return $attribute === 'CAN_SET_RATE' && $subject instanceof DeveloperRate;
}
/**
* @param string $attribute CAN_SET_RATE
* @param DeveloperRate|mixed $subject
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
$manager = $user->getManager();
if ($manager === null) {
return false;
}
$managers = ($project = $subject->getProject()) !== null ? $project->getManagers() : new ArrayCollection();
// TRUE if this collection contained the specified element, FALSE otherwise
/** @psalm-suppress InvalidArgument */
return $managers->removeElement($manager);
}
}