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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Security\Voter\Bid;
  3. use App\Entity\DealPerk;
  4. use App\Entity\ProjectPerk;
  5. use App\Entity\User;
  6. use App\Security\Voter\AbstractUserAwareVoter;
  7. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  8. class HasCreditsPerkVoter extends AbstractUserAwareVoter
  9. {
  10.     public const ATTRIBUTE 'HAS_CREDITS_PERK';
  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 ProjectPerk || $object instanceof DealPerk;
  21.     }
  22.     /**
  23.      * @param ProjectPerk|DealPerk|object $object
  24.      */
  25.     protected function isGrantedForUser($objectUser $user): bool
  26.     {
  27.         if (!$object instanceof ProjectPerk && !$object instanceof DealPerk) {
  28.             throw new UnexpectedTypeException($objectProjectPerk::class . '|' DealPerk::class);
  29.         }
  30.         return $object->getDynamicPrice() <= $user->getCredits();
  31.     }
  32. }