src/App/Security/Voter/Favorite/AddFavoriteVoter.php line 12

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Security\Voter\Favorite;
  3. use App\Doctrine\Cache\CacheDecider;
  4. use App\Entity\Tag;
  5. use App\Entity\User;
  6. use App\Repository\FavoriteRepository;
  7. use App\Security\Voter\AbstractUserAwareVoter;
  8. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  9. class AddFavoriteVoter extends AbstractUserAwareVoter
  10. {
  11.     public const ATTRIBUTE 'ADD_FAVORITE';
  12.     private FavoriteRepository $favoriteRepository;
  13.     private CacheDecider $cacheDecider;
  14.     public function __construct(FavoriteRepository $favoriteRepositoryCacheDecider $cacheDecider)
  15.     {
  16.         $this->favoriteRepository $favoriteRepository;
  17.         $this->cacheDecider $cacheDecider;
  18.     }
  19.     /**
  20.      * @return array<int, string>
  21.      */
  22.     protected function getSupportedAttributes(): array
  23.     {
  24.         return [self::ATTRIBUTE];
  25.     }
  26.     protected function supportsObject(?object $object null): bool
  27.     {
  28.         return $object instanceof Tag;
  29.     }
  30.     protected function isGrantedForUser(object $objectUser $user): bool
  31.     {
  32.         if (!$object instanceof Tag) {
  33.             throw new UnexpectedTypeException($objectTag::class);
  34.         }
  35.         return null === $this->favoriteRepository->filterOneResult([
  36.             'user' => $user->getId(),
  37.             'tag' => $object->getId(),
  38.         ], $this->cacheDecider);
  39.     }
  40. }