<?php
namespace App\Voter;
use App\Entity\CartItem;
use App\Model\Cart\CartItemFactory;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class CartItemVoter extends Voter
{
private $attributes;
private $cartItemFactory;
public function __construct(CartItemFactory $cartItemFactory)
{
$this->cartItemFactory = $cartItemFactory;
$this->attributes = [
'delete'
];
}
protected function supports($attribute, $entity): bool
{
// if the attribute isn't one we support, return false
if (! in_array($attribute, $this->attributes)) {
return false;
}
if (! $entity instanceof CartItem) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $entity, TokenInterface $token): bool
{
$userEntity = $token->getUser();
$cartItem = $this->cartItemFactory->createModel();
$cartItem->setEntity($entity);
return $cartItem->canBeDeletedBy($userEntity);
}
}