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
<?php
namespace App\Service ;
use GuzzleHttp\Client ;
use GuzzleHttp\Exception\GuzzleException ;
use GuzzleHttp\RequestOptions ;
use Psr\Http\Message\ResponseInterface ;
class ErpService
{
private const BASE_URL = 'https://erp.adnencheres.com' ;
private array $endpoints = array(
'getSale' => '/api/sale/{id}' ,
'getSales' => '/api/sale' ,
'getSaleRequest' => '/api/sale_request' ,
'getPendingSaleRequest' => '/api/export/{sale}/sale_request/pending' ,
'getProof' => '/api/sale_request/{request}/proof' ,
'getSellers' => '/api/seller' ,
'getDocuments' => '/api/sale_request/{request}/documents' ,
'postSellers' => '/api/upload/sellers' ,
'postSales' => '/api/upload/sales' ,
'postBatches' => '/api/upload/batches' ,
'regenerateRequest' => '/api/sale_request/{request}/generate'
);
/**
* @param string $name
* @param array $params
* @return string
*/
public function getEndpoint ( string $name , array $params = array()): string
{
$endpoint = $this -> endpoints [ $name ];
foreach ($params as $key => $value ) {
$endpoint = str_replace ( '{' . $key . '}' , $value , $endpoint );
}
return $endpoint ;
}
public function getSale ( int $saleId )
{
$endpoint = $this -> getEndpoint ( __FUNCTION__ , array(
'id' => $saleId
));
$response = $this -> call ( 'GET' , $endpoint );
return json_decode ( $response -> getBody ()-> getContents ());
}
public function getSales ()
{
$endpoint = $this -> getEndpoint ( __FUNCTION__ );
$response = $this -> call ( 'GET' , $endpoint );
return json_decode ( $response -> getBody ()-> getContents ());
}
public function getSellers ( $queryParams )
{
$endpoint = $this -> getEndpoint ( __FUNCTION__ );
$response = $this -> call ( 'GET' , $endpoint , queryParams : $queryParams );
return json_decode ( $response -> getBody ()-> getContents ());
}
public function getSaleRequest ()
{
$endpoint = $this -> getEndpoint ( __FUNCTION__ );
$response = $this -> call ( 'GET' , $endpoint );
return json_decode ( $response -> getBody ()-> getContents ());
}
public function getPendingSaleRequest ( int $saleId )
{
$endpoint = $this -> getEndpoint ( __FUNCTION__ , array(
'sale' => $saleId
));
$response = $this -> call ( 'GET' , $endpoint );
return json_decode ( $response -> getBody ()-> getContents ());
}
public function getProof ( int $requestId )
{
$endpoint = $this -> getEndpoint ( __FUNCTION__ , array(
'request' => $requestId
));
$response = $this -> call ( 'GET' , $endpoint );
return json_decode ( $response -> getBody ()-> getContents ());
}
public function getDocuments ( int $requestId )
{
$endpoint = $this -> getEndpoint ( __FUNCTION__ , array(
'request' => $requestId
));
$response = $this -> call ( 'GET' , $endpoint );
return json_decode ( $response -> getBody ()-> getContents ());
}
public function postSales ( string $fileContent )
{
$endpoint = $this -> getEndpoint ( __FUNCTION__ );
$response = $this -> call ( 'POST' , $endpoint , array(
'content' => $fileContent
));
return json_decode ( $response -> getBody ()-> getContents ());
}
public function postSellers ( string $fileContent )
{
$endpoint = $this -> getEndpoint ( __FUNCTION__ );
$response = $this -> call ( 'POST' , $endpoint , array(
'content' => $fileContent
));
return json_decode ( $response -> getBody ()-> getContents ());
}
public function postBatches ( string $fileContent )
{
$endpoint = $this -> getEndpoint ( __FUNCTION__ );
$response = $this -> call ( 'POST' , $endpoint , array(
'content' => $fileContent
));
return json_decode ( $response -> getBody ()-> getContents ());
}
public function regenerateRequest ( int $requestId )
{
$endpoint = $this -> getEndpoint ( __FUNCTION__ , array(
'request' => $requestId
));
$response = $this -> call ( 'GET' , $endpoint );
return json_decode ( $response -> getBody ()-> getContents ());
}
/**
* @param string $method
* @param string $endpoint
* @param array|null $bodyParams
* @param array $queryParams
* @return ResponseInterface
* @throws GuzzleException
*/
private function call ( string $method , string $endpoint , array $bodyParams = null , array $queryParams = array()): ResponseInterface
{
$httpClient = new Client ();
return $httpClient -> request ( $method , self :: BASE_URL . $endpoint . '?' . http_build_query ( $queryParams ), array( RequestOptions :: JSON => $bodyParams ));
}
}