src/V4/DataProvider/Quote/QuoteItemDataProvider.php line 119

Open in your IDE?
  1. <?php
  2. namespace App\V4\DataProvider\Quote;
  3. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  4. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  5. use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
  6. use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
  7. use App\DataProvider\AbstractCollectionDataProvider;
  8. use App\Model\CustomerFile\CustomerFile;
  9. use App\Service\ApiWebService;
  10. use App\Service\CustomerFile\CustomerFileManager;
  11. use App\Service\Provider\ApiBusinessProvider;
  12. use App\V4\Model\Quote\Quote;
  13. use ReflectionException;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  16. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  17. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  18. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  19. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  20. class QuoteItemDataProvider implements ItemDataProviderInterfaceRestrictedDataProviderInterface
  21. {
  22.     /**
  23.      * @var ApiWebService
  24.      */
  25.     private $apiWebService;
  26.     /**
  27.      * @var ApiBusinessProvider
  28.      */
  29.     private $apiBusinessProvider;
  30.     /**
  31.      * @var CollectionDataProviderInterface
  32.      */
  33.     private $collectionDataProvider;
  34.     /**
  35.      * @var CustomerFileManager
  36.      */
  37.     private $customerFileManager;
  38.     /**
  39.      * @param ApiWebService                   $apiWebService
  40.      * @param ApiBusinessProvider             $apiBusinessProvider
  41.      * @param CollectionDataProviderInterface $collectionDataProvider
  42.      * @param CustomerFileManager             $customerFileManager
  43.      */
  44.     public function __construct(
  45.         ApiWebService $apiWebService,
  46.         ApiBusinessProvider $apiBusinessProvider,
  47.         CollectionDataProviderInterface $collectionDataProvider,
  48.         CustomerFileManager $customerFileManager
  49.     ) {
  50.         $this->apiWebService $apiWebService;
  51.         $this->apiBusinessProvider $apiBusinessProvider;
  52.         $this->collectionDataProvider $collectionDataProvider;
  53.         $this->customerFileManager $customerFileManager;
  54.     }
  55.     /**
  56.      * @param string      $resourceClass
  57.      * @param string|null $operationName
  58.      * @param array       $context
  59.      *
  60.      * @return bool
  61.      */
  62.     public function supports(string $resourceClassstring $operationName null, array $context = []): bool
  63.     {
  64.         return Quote::class === $resourceClass;
  65.     }
  66.     /**
  67.      * @param string $resourceClass
  68.      * @param $id
  69.      * @param string|null $operationName
  70.      * @param array       $context
  71.      *
  72.      * @return Quote|null
  73.      *
  74.      * @throws ClientExceptionInterface
  75.      * @throws DecodingExceptionInterface
  76.      * @throws RedirectionExceptionInterface
  77.      * @throws ReflectionException
  78.      * @throws ResourceClassNotSupportedException
  79.      * @throws ServerExceptionInterface
  80.      * @throws TransportExceptionInterface
  81.      */
  82.     public function getItem(string $resourceClass$idstring $operationName null, array $context = []): ?Quote
  83.     {
  84.         $response $this->getRawItem($id$context);
  85.         return (new Quote())
  86.             ->importFromData($response)
  87.         ;
  88.     }
  89.     /**
  90.      * @param string $id
  91.      * @param array $context
  92.      * @return array
  93.      *
  94.      * @throws ClientExceptionInterface
  95.      * @throws DecodingExceptionInterface
  96.      * @throws RedirectionExceptionInterface
  97.      * @throws ResourceClassNotSupportedException
  98.      * @throws ServerExceptionInterface
  99.      * @throws TransportExceptionInterface
  100.      */
  101.     public function getRawItem(string $id, array $context = []): array
  102.     {
  103.         $this->apiWebService->addFilterCustomer();
  104.         $quote $this
  105.             ->apiWebService
  106.             ->send($this->apiBusinessProviderRequest::METHOD_GET'/api/quotes/'.$id)
  107.             ->toArray()
  108.         ;
  109.         if (!isset($quote['prospectId']) && isset($quote['prospect.id'])) {
  110.             $quote['prospectId'] = $quote['prospect.id'];
  111.         }
  112.         if (isset($context[AbstractCollectionDataProvider::FETCH_CUSTOMER_FILES]) && !$context[AbstractCollectionDataProvider::FETCH_CUSTOMER_FILES]) {
  113.             return $quote;
  114.         }
  115.         /** @var CustomerFile[] $files */
  116.         $files $this
  117.             ->collectionDataProvider
  118.             ->getCollection(CustomerFile::class, null, [
  119.                 'filters' => [
  120.                     'quoteId' => $id,
  121.                 ],
  122.             ])
  123.         ;
  124.         $customerFiles = [];
  125.         foreach ($files as $customerResource) {
  126.             $customerResource->setDownloadLink($this->customerFileManager->generateDownloadLink($customerResource));
  127.             $customerResource->setDeleteLink($this->customerFileManager->generateDeleteLink($customerResource));
  128.             $customerFiles[] = $customerResource;
  129.         }
  130.         return array_merge($quote, [
  131.             'customerFiles' => array_values($customerFiles),
  132.         ]);
  133.     }
  134. }