src/Shop/Security/Voter/ProductVoter.php line 12

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Shop\Security\Voter;
  4. use App\Shop\Doctrine\Entity\Product;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Workflow\WorkflowInterface;
  8. final class ProductVoter extends Voter
  9. {
  10.     public function __construct(private WorkflowInterface $productStateMachine)
  11.     {
  12.     }
  13.     protected function supports(string $attributemixed $subject): bool
  14.     {
  15.         return $subject instanceof Product && in_array($attribute, ['edit'], true);
  16.     }
  17.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  18.     {
  19.         /** @var Product $product */
  20.         $product $subject;
  21.         return $this->productStateMachine->getMarking($product)->has('on_hold')
  22.             || $this->productStateMachine->getMarking($product)->has('published')
  23.             || $this->productStateMachine->getMarking($product)->has('draft')
  24.             || $this->productStateMachine->can($product'publish');
  25.     }
  26. }