<?php declare(strict_types=1);
namespace App\Security\Voter\Message;
use App\Entity\Message\MessageBid;
use App\Entity\Message\MessageReception;
use App\Entity\User;
use App\Security\Voter\AbstractUserAwareVoter;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class HasUnlockedVoter extends AbstractUserAwareVoter
{
public const ATTRIBUTE = 'HAS_UNLOCKED_MESSAGE';
/**
* @return array<int, string>
*/
protected function getSupportedAttributes(): array
{
return [self::ATTRIBUTE];
}
protected function supportsObject(?object $object = null): bool
{
return $object instanceof MessageReception;
}
/**
* @param MessageReception|object $object
*/
protected function isGrantedForUser($object, User $user): bool
{
if (!$object instanceof MessageReception) {
throw new UnexpectedTypeException($object, MessageReception::class);
}
return $user->getMessageBids()->exists(static function (int $index, MessageBid $bid) use ($object): bool {
return $bid->getMessageReception() === $object;
});
}
}