src/Voter/CartItemVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Voter;
  3. use App\Entity\CartItem;
  4. use App\Model\Cart\CartItemFactory;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class CartItemVoter extends Voter
  8. {
  9.     private $attributes;
  10.     private $cartItemFactory;
  11.     public function __construct(CartItemFactory $cartItemFactory)
  12.     {
  13.         $this->cartItemFactory $cartItemFactory;
  14.         $this->attributes = [
  15.             'delete'
  16.         ];
  17.     }
  18.     protected function supports($attribute$entity): bool
  19.     {
  20.         // if the attribute isn't one we support, return false
  21.         if (! in_array($attribute$this->attributes)) {
  22.             return false;
  23.         }
  24.         if (! $entity instanceof CartItem) {
  25.             return false;
  26.         }
  27.         return true;
  28.     }
  29.     protected function voteOnAttribute($attribute$entityTokenInterface $token): bool
  30.     {
  31.         $userEntity $token->getUser();
  32.         $cartItem $this->cartItemFactory->createModel();
  33.         $cartItem->setEntity($entity);
  34.         return $cartItem->canBeDeletedBy($userEntity);
  35.     }
  36. }