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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Infrastructure\Security\Voter;
  4. use App\Domain\Common\Entity\IzOrderProduct;
  5. use App\Infrastructure\Elasticsearch\Factory\Product\ProductSearchFactory;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. final class ProductVoter extends Voter
  9. {
  10.     public const VIEW 'PRODUCT_VIEW';
  11.     private ProductSearchFactory $searchFactory;
  12.     public function __construct(ProductSearchFactory $searchFactory)
  13.     {
  14.         $this->searchFactory $searchFactory;
  15.     }
  16.     protected function supports(string $attribute$subject): bool
  17.     {
  18.         return self::VIEW === $attribute && $subject instanceof IzOrderProduct;
  19.     }
  20.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  21.     {
  22.         if (self::VIEW === $attribute) {
  23.             $relatedProducts $this->searchFactory->buildFromFilters([
  24.                 /** @var IzOrderProduct $subject */
  25.                 'recherche' => $subject->getCodeArticle1(),
  26.             ], 11)->search()->getResults();
  27.             return !empty($relatedProducts) && $relatedProducts[0]->getData()['id'] === $subject->getCodeArticle1();
  28.         }
  29.         return false;
  30.     }
  31. }