Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

src/Service/ErpService.php line 167

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\Exception\GuzzleException;
  5. use GuzzleHttp\RequestOptions;
  6. use Psr\Http\Message\ResponseInterface;
  7. class ErpService
  8. {
  9.     private const BASE_URL 'https://erp.adnencheres.com';
  10.     private array $endpoints = array(
  11.         'getSale' => '/api/sale/{id}',
  12.         'getSales' => '/api/sale',
  13.         'getSaleRequest' => '/api/sale_request',
  14.         'getPendingSaleRequest' => '/api/export/{sale}/sale_request/pending',
  15.         'getProof' => '/api/sale_request/{request}/proof',
  16.         'getSellers' => '/api/seller',
  17.         'getDocuments' => '/api/sale_request/{request}/documents',
  18.         'postSellers' => '/api/upload/sellers',
  19.         'postSales' => '/api/upload/sales',
  20.         'postBatches' => '/api/upload/batches',
  21.         'regenerateRequest' => '/api/sale_request/{request}/generate'
  22.     );
  23.     /**
  24.      * @param string $name
  25.      * @param array $params
  26.      * @return string
  27.      */
  28.     public function getEndpoint(string $name, array $params = array()): string
  29.     {
  30.         $endpoint $this->endpoints[$name];
  31.         foreach ($params as $key => $value) {
  32.             $endpoint str_replace('{' $key '}'$value$endpoint);
  33.         }
  34.         return $endpoint;
  35.     }
  36.     public function getSale(int $saleId)
  37.     {
  38.         $endpoint $this->getEndpoint(__FUNCTION__, array(
  39.             'id' => $saleId
  40.         ));
  41.         $response $this->call('GET'$endpoint);
  42.         return json_decode($response->getBody()->getContents());
  43.     }
  44.     public function getSales()
  45.     {
  46.         $endpoint $this->getEndpoint(__FUNCTION__);
  47.         $response $this->call('GET'$endpoint);
  48.         return json_decode($response->getBody()->getContents());
  49.     }
  50.     public function getSellers($queryParams)
  51.     {
  52.         $endpoint $this->getEndpoint(__FUNCTION__);
  53.         $response $this->call('GET'$endpointqueryParams$queryParams);
  54.         return json_decode($response->getBody()->getContents());
  55.     }
  56.     public function getSaleRequest()
  57.     {
  58.         $endpoint $this->getEndpoint(__FUNCTION__);
  59.         $response $this->call('GET'$endpoint);
  60.         return json_decode($response->getBody()->getContents());
  61.     }
  62.     public function getPendingSaleRequest(int $saleId)
  63.     {
  64.         $endpoint $this->getEndpoint(__FUNCTION__, array(
  65.             'sale' => $saleId
  66.         ));
  67.         $response $this->call('GET'$endpoint);
  68.         return json_decode($response->getBody()->getContents());
  69.     }
  70.     public function getProof(int $requestId)
  71.     {
  72.         $endpoint $this->getEndpoint(__FUNCTION__, array(
  73.             'request' => $requestId
  74.         ));
  75.         $response $this->call('GET'$endpoint);
  76.         return json_decode($response->getBody()->getContents());
  77.     }
  78.     public function getDocuments(int $requestId)
  79.     {
  80.         $endpoint $this->getEndpoint(__FUNCTION__, array(
  81.             'request' => $requestId
  82.         ));
  83.         $response $this->call('GET'$endpoint);
  84.         return json_decode($response->getBody()->getContents());
  85.     }
  86.     public function postSales(string $fileContent)
  87.     {
  88.         $endpoint $this->getEndpoint(__FUNCTION__);
  89.         $response $this->call('POST'$endpoint, array(
  90.             'content' => $fileContent
  91.         ));
  92.         return json_decode($response->getBody()->getContents());
  93.     }
  94.     public function postSellers(string $fileContent)
  95.     {
  96.         $endpoint $this->getEndpoint(__FUNCTION__);
  97.         $response $this->call('POST'$endpoint, array(
  98.             'content' => $fileContent
  99.         ));
  100.         return json_decode($response->getBody()->getContents());
  101.     }
  102.     public function postBatches(string $fileContent)
  103.     {
  104.         $endpoint $this->getEndpoint(__FUNCTION__);
  105.         $response $this->call('POST'$endpoint, array(
  106.             'content' => $fileContent
  107.         ));
  108.         return json_decode($response->getBody()->getContents());
  109.     }
  110.     public function regenerateRequest(int $requestId)
  111.     {
  112.         $endpoint $this->getEndpoint(__FUNCTION__, array(
  113.             'request' => $requestId
  114.         ));
  115.         $response $this->call('GET'$endpoint);
  116.         return json_decode($response->getBody()->getContents());
  117.     }
  118.     /**
  119.      * @param string $method
  120.      * @param string $endpoint
  121.      * @param array|null $bodyParams
  122.      * @param array $queryParams
  123.      * @return ResponseInterface
  124.      * @throws GuzzleException
  125.      */
  126.     private function call(string $methodstring $endpoint, array $bodyParams null, array $queryParams = array()): ResponseInterface
  127.     {
  128.         $httpClient = new Client();
  129.         return $httpClient->request($methodself::BASE_URL $endpoint '?' http_build_query($queryParams), array(RequestOptions::JSON => $bodyParams));
  130.     }
  131. }