src/App/Security/Voter/Bid/HasPurchasedVoter.php line 11

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Security\Voter\Bid;
  3. use App\Contract\Payments\PurchaseInterface;
  4. use App\Contract\Payments\PurchaseMultiInterface;
  5. use App\Entity\User;
  6. use App\Security\Voter\AbstractUserAwareVoter;
  7. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  8. class HasPurchasedVoter extends AbstractUserAwareVoter
  9. {
  10.     public const ATTRIBUTE 'HAS_PURCHASED';
  11.     /**
  12.      * @return array<int, string>
  13.      */
  14.     protected function getSupportedAttributes(): array
  15.     {
  16.         return [self::ATTRIBUTE];
  17.     }
  18.     protected function supportsObject(?object $object null): bool
  19.     {
  20.         return $object instanceof PurchaseMultiInterface;
  21.     }
  22.     /**
  23.      * @param PurchaseInterface|object $object
  24.      */
  25.     protected function isGrantedForUser($objectUser $user): bool
  26.     {
  27.         if (!$object instanceof PurchaseMultiInterface) {
  28.             throw new UnexpectedTypeException($objectPurchaseMultiInterface::class);
  29.         }
  30.         return $object->getPurchased()->getUserBids($user)->exists(static function (int $indexPurchaseMultiInterface $bid): bool {
  31.             return null !== $bid->getId() && $bid->isStillAccessible();
  32.         });
  33.     }
  34. }