<?php declare(strict_types=1);
namespace App\Security\Voter\Bid;
use App\Contract\Payments\PurchaseInterface;
use App\Contract\Payments\PurchaseMultiInterface;
use App\Entity\User;
use App\Security\Voter\AbstractUserAwareVoter;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class HasPurchasedVoter extends AbstractUserAwareVoter
{
public const ATTRIBUTE = 'HAS_PURCHASED';
/**
* @return array<int, string>
*/
protected function getSupportedAttributes(): array
{
return [self::ATTRIBUTE];
}
protected function supportsObject(?object $object = null): bool
{
return $object instanceof PurchaseMultiInterface;
}
/**
* @param PurchaseInterface|object $object
*/
protected function isGrantedForUser($object, User $user): bool
{
if (!$object instanceof PurchaseMultiInterface) {
throw new UnexpectedTypeException($object, PurchaseMultiInterface::class);
}
return $object->getPurchased()->getUserBids($user)->exists(static function (int $index, PurchaseMultiInterface $bid): bool {
return null !== $bid->getId() && $bid->isStillAccessible();
});
}
}