<?php
namespace App\DataProvider\Field;
use App\DataProvider\AbstractCollectionDataProvider;
use App\DataProvider\RequestDataProviderTrait;
use App\Listing\Transformer\ListingResponseTransformerInterface;
use App\Model\Field\Field;
use App\Service\ApiWebService;
use App\Service\ApiWebServiceFilterBuilder\FilterBuildersChain;
use App\Service\Cache\CacheManager;
use App\Service\Cache\Exception\UnableToSaveKeyException;
use App\Service\Provider\ApiParamProvider;
use App\Voters\ProductVoter;
use Psr\Cache\CacheException;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class FieldCollectionDataProvider extends AbstractCollectionDataProvider
{
protected const ENTITY = Field::class;
protected const ENDPOINT = '/api/fields';
/**
* @var Security
*/
private $security;
use RequestDataProviderTrait;
/**
* @param FilterBuildersChain $filterBuildersChain
* @param ApiWebService $apiWebService
* @param ApiParamProvider $apiParamProvider
* @param CacheManager $cacheManager
* @param ListingResponseTransformerInterface $listingResponseTransformer
* @param Security $security
*/
public function __construct(
FilterBuildersChain $filterBuildersChain,
ApiWebService $apiWebService,
ApiParamProvider $apiParamProvider,
CacheManager $cacheManager,
ListingResponseTransformerInterface $listingResponseTransformer,
Security $security
) {
parent::__construct($filterBuildersChain, $apiWebService, $apiParamProvider, $cacheManager, $listingResponseTransformer);
$this->security = $security;
}
/**
* @param string $resourceClass
* @param string|null $operationName
* @param array $context
*
* @return iterable
*
* @throws UnableToSaveKeyException
* @throws CacheException
* @throws InvalidArgumentException
* @throws ExceptionInterface
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public function getCollection(string $resourceClass, string $operationName = null, array $context = []): iterable
{
//@todo refacto: check this override
return parent::getCollection($resourceClass, $operationName, $context);
$rawCollection = $this->getRawCollection($context);
$entities = [];
foreach ($rawCollection['hydra:member'] as $responseContent) {
if ('Product' === $responseContent['entity'] && !$this->security->isGranted(ProductVoter::PRODUCT_MANAGE_FIELDS)) {
continue;
}
$class = $this::ENTITY;
$entity = new $class();
$entity->importFromData($responseContent);
$entities[] = $entity;
}
return $entities;
}
}