src/App/Security/Voter/Message/HasUnlockedVoter.php line 11

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Security\Voter\Message;
  3. use App\Entity\Message\MessageBid;
  4. use App\Entity\Message\MessageReception;
  5. use App\Entity\User;
  6. use App\Security\Voter\AbstractUserAwareVoter;
  7. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  8. class HasUnlockedVoter extends AbstractUserAwareVoter
  9. {
  10.     public const ATTRIBUTE 'HAS_UNLOCKED_MESSAGE';
  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 MessageReception;
  21.     }
  22.     /**
  23.      * @param MessageReception|object $object
  24.      */
  25.     protected function isGrantedForUser($objectUser $user): bool
  26.     {
  27.         if (!$object instanceof MessageReception) {
  28.             throw new UnexpectedTypeException($objectMessageReception::class);
  29.         }
  30.         return $user->getMessageBids()->exists(static function (int $indexMessageBid $bid) use ($object): bool {
  31.             return $bid->getMessageReception() === $object;
  32.         });
  33.     }
  34. }