<?php
declare(strict_types=1);
namespace App\Shop\Security\Voter;
use App\Shop\Doctrine\Entity\Product;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Workflow\WorkflowInterface;
final class ProductVoter extends Voter
{
public function __construct(private WorkflowInterface $productStateMachine)
{
}
protected function supports(string $attribute, mixed $subject): bool
{
return $subject instanceof Product && in_array($attribute, ['edit'], true);
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
/** @var Product $product */
$product = $subject;
return $this->productStateMachine->getMarking($product)->has('on_hold')
|| $this->productStateMachine->getMarking($product)->has('published')
|| $this->productStateMachine->getMarking($product)->has('draft')
|| $this->productStateMachine->can($product, 'publish');
}
}