src/App/Security/Voter/Message/MessageAccess.php line 12

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Security\Voter\Message;
  3. use App\Entity\Message\MessageReception;
  4. use App\Entity\User;
  5. use App\Security\Voter\AbstractUserAwareVoter;
  6. use App\Security\Voter\Bid\HasPurchasedOnceVoter;
  7. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  8. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  9. class MessageAccess extends AbstractUserAwareVoter
  10. {
  11.     public const ATTRIBUTE 'MESSAGE_ACCESS';
  12.     private AuthorizationCheckerInterface $authorizationChecker;
  13.     public function __construct(AuthorizationCheckerInterface $authorizationChecker)
  14.     {
  15.         $this->authorizationChecker $authorizationChecker;
  16.     }
  17.     /**
  18.      * @return array<int, string>
  19.      */
  20.     protected function getSupportedAttributes(): array
  21.     {
  22.         return [self::ATTRIBUTE];
  23.     }
  24.     protected function supportsObject(?object $object null): bool
  25.     {
  26.         return $object instanceof MessageReception;
  27.     }
  28.     /**
  29.      * @param MessageReception|object $object
  30.      */
  31.     protected function isGrantedForUser(object $objectUser $user): bool
  32.     {
  33.         if (!$object instanceof MessageReception) {
  34.             throw new UnexpectedTypeException($objectMessageReception::class);
  35.         }
  36.         return $object->getMessage()->isNotification()
  37.             || $this->authorizationChecker->isGranted(
  38.                 HasPurchasedOnceVoter::ATTRIBUTE,
  39.                 $object->getBid() ?? $object->createBid()
  40.             );
  41.     }
  42. }