<?php
namespace App\V4\DataProvider\Quote;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\DataProvider\AbstractCollectionDataProvider;
use App\Model\CustomerFile\CustomerFile;
use App\Service\ApiWebService;
use App\Service\CustomerFile\CustomerFileManager;
use App\Service\Provider\ApiBusinessProvider;
use App\V4\Model\Quote\Quote;
use ReflectionException;
use Symfony\Component\HttpFoundation\Request;
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 QuoteItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
{
/**
* @var ApiWebService
*/
private $apiWebService;
/**
* @var ApiBusinessProvider
*/
private $apiBusinessProvider;
/**
* @var CollectionDataProviderInterface
*/
private $collectionDataProvider;
/**
* @var CustomerFileManager
*/
private $customerFileManager;
/**
* @param ApiWebService $apiWebService
* @param ApiBusinessProvider $apiBusinessProvider
* @param CollectionDataProviderInterface $collectionDataProvider
* @param CustomerFileManager $customerFileManager
*/
public function __construct(
ApiWebService $apiWebService,
ApiBusinessProvider $apiBusinessProvider,
CollectionDataProviderInterface $collectionDataProvider,
CustomerFileManager $customerFileManager
) {
$this->apiWebService = $apiWebService;
$this->apiBusinessProvider = $apiBusinessProvider;
$this->collectionDataProvider = $collectionDataProvider;
$this->customerFileManager = $customerFileManager;
}
/**
* @param string $resourceClass
* @param string|null $operationName
* @param array $context
*
* @return bool
*/
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
return Quote::class === $resourceClass;
}
/**
* @param string $resourceClass
* @param $id
* @param string|null $operationName
* @param array $context
*
* @return Quote|null
*
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ReflectionException
* @throws ResourceClassNotSupportedException
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?Quote
{
$response = $this->getRawItem($id, $context);
return (new Quote())
->importFromData($response)
;
}
/**
* @param string $id
* @param array $context
* @return array
*
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ResourceClassNotSupportedException
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public function getRawItem(string $id, array $context = []): array
{
$this->apiWebService->addFilterCustomer();
$quote = $this
->apiWebService
->send($this->apiBusinessProvider, Request::METHOD_GET, '/api/quotes/'.$id)
->toArray()
;
if (!isset($quote['prospectId']) && isset($quote['prospect.id'])) {
$quote['prospectId'] = $quote['prospect.id'];
}
if (isset($context[AbstractCollectionDataProvider::FETCH_CUSTOMER_FILES]) && !$context[AbstractCollectionDataProvider::FETCH_CUSTOMER_FILES]) {
return $quote;
}
/** @var CustomerFile[] $files */
$files = $this
->collectionDataProvider
->getCollection(CustomerFile::class, null, [
'filters' => [
'quoteId' => $id,
],
])
;
$customerFiles = [];
foreach ($files as $customerResource) {
$customerResource->setDownloadLink($this->customerFileManager->generateDownloadLink($customerResource));
$customerResource->setDeleteLink($this->customerFileManager->generateDeleteLink($customerResource));
$customerFiles[] = $customerResource;
}
return array_merge($quote, [
'customerFiles' => array_values($customerFiles),
]);
}
}