<?php declare(strict_types=1);
namespace App\Security\Voter\Bid;
use App\Entity\DealPerk;
use App\Entity\ProjectPerk;
use App\Entity\User;
use App\Security\Voter\AbstractUserAwareVoter;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class HasCreditsPerkVoter extends AbstractUserAwareVoter
{
public const ATTRIBUTE = 'HAS_CREDITS_PERK';
/**
* @return array<int, string>
*/
protected function getSupportedAttributes(): array
{
return [self::ATTRIBUTE];
}
protected function supportsObject(?object $object = null): bool
{
return $object instanceof ProjectPerk || $object instanceof DealPerk;
}
/**
* @param ProjectPerk|DealPerk|object $object
*/
protected function isGrantedForUser($object, User $user): bool
{
if (!$object instanceof ProjectPerk && !$object instanceof DealPerk) {
throw new UnexpectedTypeException($object, ProjectPerk::class . '|' . DealPerk::class);
}
return $object->getDynamicPrice() <= $user->getCredits();
}
}