src/DataProvider/Field/FieldCollectionDataProvider.php line 74

Open in your IDE?
  1. <?php
  2. namespace App\DataProvider\Field;
  3. use App\DataProvider\AbstractCollectionDataProvider;
  4. use App\DataProvider\RequestDataProviderTrait;
  5. use App\Listing\Transformer\ListingResponseTransformerInterface;
  6. use App\Model\Field\Field;
  7. use App\Service\ApiWebService;
  8. use App\Service\ApiWebServiceFilterBuilder\FilterBuildersChain;
  9. use App\Service\Cache\CacheManager;
  10. use App\Service\Cache\Exception\UnableToSaveKeyException;
  11. use App\Service\Provider\ApiParamProvider;
  12. use App\Voters\ProductVoter;
  13. use Psr\Cache\CacheException;
  14. use Psr\Cache\InvalidArgumentException;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  17. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  18. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  19. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  20. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  21. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  22. class FieldCollectionDataProvider extends AbstractCollectionDataProvider
  23. {
  24.     protected const ENTITY Field::class;
  25.     protected const ENDPOINT '/api/fields';
  26.     /**
  27.      * @var Security
  28.      */
  29.     private $security;
  30.     use RequestDataProviderTrait;
  31.     /**
  32.      * @param FilterBuildersChain                 $filterBuildersChain
  33.      * @param ApiWebService                       $apiWebService
  34.      * @param ApiParamProvider                    $apiParamProvider
  35.      * @param CacheManager                        $cacheManager
  36.      * @param ListingResponseTransformerInterface $listingResponseTransformer
  37.      * @param Security                            $security
  38.      */
  39.     public function __construct(
  40.         FilterBuildersChain $filterBuildersChain,
  41.         ApiWebService $apiWebService,
  42.         ApiParamProvider $apiParamProvider,
  43.         CacheManager $cacheManager,
  44.         ListingResponseTransformerInterface $listingResponseTransformer,
  45.         Security $security
  46.     ) {
  47.         parent::__construct($filterBuildersChain$apiWebService$apiParamProvider$cacheManager$listingResponseTransformer);
  48.         $this->security $security;
  49.     }
  50.     /**
  51.      * @param string      $resourceClass
  52.      * @param string|null $operationName
  53.      * @param array       $context
  54.      *
  55.      * @return iterable
  56.      *
  57.      * @throws UnableToSaveKeyException
  58.      * @throws CacheException
  59.      * @throws InvalidArgumentException
  60.      * @throws ExceptionInterface
  61.      * @throws ClientExceptionInterface
  62.      * @throws DecodingExceptionInterface
  63.      * @throws RedirectionExceptionInterface
  64.      * @throws ServerExceptionInterface
  65.      * @throws TransportExceptionInterface
  66.      */
  67.     public function getCollection(string $resourceClassstring $operationName null, array $context = []): iterable
  68.     {
  69.         //@todo refacto: check this override
  70.         return parent::getCollection($resourceClass$operationName$context);
  71.         $rawCollection $this->getRawCollection($context);
  72.         $entities = [];
  73.         foreach ($rawCollection['hydra:member'] as $responseContent) {
  74.             if ('Product' === $responseContent['entity'] && !$this->security->isGranted(ProductVoter::PRODUCT_MANAGE_FIELDS)) {
  75.                 continue;
  76.             }
  77.             $class $this::ENTITY;
  78.             $entity = new $class();
  79.             $entity->importFromData($responseContent);
  80.             $entities[] = $entity;
  81.         }
  82.         return $entities;
  83.     }
  84. }