<?php declare(strict_types=1);
namespace App\Security\Voter\Bid;
use App\Entity\DealBid;
use App\Entity\User;
use App\Security\Voter\AbstractProposalBidVoter;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class HasNoContributionVoter extends AbstractProposalBidVoter
{
public const ATTRIBUTE = 'HAS_NO_CONTRIBUTION';
/**
* @return array<int, string>
*/
protected function getSupportedAttributes(): array
{
return [self::ATTRIBUTE];
}
/**
* @param DealBid|object $object
*/
protected function isGrantedForUser($object, User $user): bool
{
if (!$object instanceof DealBid) {
throw new UnexpectedTypeException($object, DealBid::class);
}
return false === $object->getDeal()->getUserBids($user)->exists(static function (int $index, DealBid $bid) use ($object): bool {
return null !== $bid->getId() && $bid->isStillAccessible() && $object->getDealPerk() === $bid->getDealPerk();
});
}
protected function generateCacheKey(object $object): string
{
if (!$object instanceof DealBid) {
throw new UnexpectedTypeException($object, DealBid::class);
}
$key = parent::generateCacheKey($object);
if (null !== $object->getDealPerk() && null !== $object->getDealPerk()->getId()) {
$key .= '-perk' . $object->getDealPerk()->getId();
}
return $key;
}
}