256 lines
5.3 KiB
PHP
256 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace LDC\LDCCRM\Transport;
|
|
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Psr7\Request as Psr7Request;
|
|
use GuzzleHttp\RequestOptions;
|
|
use LDC\LDCCRM\Enums\HttpMethods;
|
|
use LDC\LDCCRM\Utils;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
/**
|
|
* The LDC CRM request
|
|
* Contains the http method, the requested url and data to pass
|
|
*
|
|
* @property \LDC\LDCCRM\Enums\HttpMethods $method readonly method
|
|
* @property string $uri readonly uri
|
|
* @property array $headers headers
|
|
* @property array $data readonly data
|
|
*
|
|
* @package LDCCrmClient
|
|
* @subpackage transport
|
|
*/
|
|
class Request
|
|
{
|
|
|
|
/**
|
|
* GuzzleHttp client
|
|
*
|
|
* @var \GuzzleHttp\Client
|
|
*/
|
|
private $client;
|
|
|
|
/**
|
|
* The http method
|
|
*
|
|
* @var \LDC\LDCCRM\Enums\HttpMethods
|
|
*/
|
|
protected $method;
|
|
|
|
/**
|
|
* The http url
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $uri;
|
|
|
|
/**
|
|
* Data to pass to the http headers
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $headers = [];
|
|
|
|
/**
|
|
* Data to pass to the http body
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $data = [];
|
|
|
|
/**
|
|
* Guzzle request
|
|
*
|
|
* @var callable the returns \GuzzleHttp\Psr7\Request
|
|
*/
|
|
private $request;
|
|
|
|
/**
|
|
* The Api endpoint
|
|
*
|
|
* @var string
|
|
*/
|
|
private $endpoint;
|
|
|
|
/**
|
|
* Hidden props
|
|
*
|
|
* @var callable
|
|
*/
|
|
private $hidden;
|
|
|
|
/**
|
|
* Max attemps to login
|
|
*
|
|
* @var integer
|
|
*/
|
|
protected $maxAttemps = 3;
|
|
|
|
/**
|
|
* Current attempt to login
|
|
*
|
|
* @var integer
|
|
*/
|
|
protected $attemps = 0;
|
|
|
|
public function __construct(
|
|
HttpMethods $method,
|
|
string $uri,
|
|
array $headers = [],
|
|
array $data = []
|
|
) {
|
|
$this->hidden = function () {
|
|
return [];
|
|
};
|
|
$this->method = $method;
|
|
$this->uri = $uri;
|
|
$this->data = $data;
|
|
$this->headers = $headers;
|
|
$this->client = new Client([
|
|
RequestOptions::HEADERS => [
|
|
'User-Agent' => sprintf(
|
|
'LDC CRM Client v%s',
|
|
Utils::getPackageInfo()['version']
|
|
)
|
|
]
|
|
]);
|
|
}
|
|
|
|
public function __get($field)
|
|
{
|
|
if (\in_array($field, ['method', 'uri', 'data'])) {
|
|
return $this->{$field};
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* make the guzzle request
|
|
*
|
|
* @return void
|
|
*/
|
|
private function makeRequest(): void
|
|
{
|
|
$this->request = function () {
|
|
$uri = $this->uri;
|
|
if ($this->method->equals(HttpMethods::get())) {
|
|
$uri = sprintf("%s?%s", $uri, http_build_query($this->data));
|
|
}
|
|
$headers = array_merge($this->headers, ($this->hidden)('headers'));
|
|
$data = array_merge($this->data, ($this->hidden)('data'));
|
|
return new Psr7Request(
|
|
\strtoupper($this->method),
|
|
sprintf('%s/%s', $this->endpoint, $uri),
|
|
$headers,
|
|
$this->method->equals(HttpMethods::get()) ? null : \json_encode($data)
|
|
);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Set the api endpoint to the request
|
|
*
|
|
* @param string $endpoint
|
|
* @return void
|
|
*/
|
|
public function setEndpoint(string $endpoint): void
|
|
{
|
|
$this->endpoint = $endpoint;
|
|
}
|
|
|
|
/**
|
|
* Make the Http request
|
|
*
|
|
* @return ResponseInterface
|
|
*/
|
|
public function call(): ResponseInterface
|
|
{
|
|
$this->makeRequest();
|
|
/** @var \GuzzleHttp\Psr7\Request */
|
|
$request = ($this->request)();
|
|
return $this->client->send($request, [
|
|
'http_errors' => false // do not throw http errors
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Add headers to the request
|
|
*
|
|
* @param array $headers
|
|
* @return void
|
|
*/
|
|
public function addHeaders(array $headers)
|
|
{
|
|
$this->headers = \array_merge($this->headers, $headers);
|
|
}
|
|
|
|
/**
|
|
* Add data to the request
|
|
*
|
|
* @param array $data
|
|
* @return void
|
|
*/
|
|
public function addData(array $data)
|
|
{
|
|
$this->data = \array_merge($this->data, $data);
|
|
}
|
|
|
|
/**
|
|
* Set hidden header and data
|
|
*
|
|
* @param array $array
|
|
* @return void
|
|
*/
|
|
public function setHidden(array $array)
|
|
{
|
|
$this->hidden = function ($headersOrArray) use ($array) {
|
|
if (
|
|
$headersOrArray == 'headers' and
|
|
isset($array['headers']) and
|
|
is_array($array['headers'])
|
|
) {
|
|
return $array['headers'];
|
|
}
|
|
if (
|
|
$headersOrArray == 'data' and
|
|
isset($array['data']) and
|
|
is_array($array['data'])
|
|
) {
|
|
return $array['data'];
|
|
}
|
|
return [];
|
|
};
|
|
}
|
|
|
|
|
|
/**
|
|
* Get attempted calls
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function getAttempts(): int
|
|
{
|
|
return $this->attemps;
|
|
}
|
|
|
|
/**
|
|
* Does max attempts have been reached
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function maxAttempted(): bool
|
|
{
|
|
return $this->attemps >= $this->maxAttemps;
|
|
}
|
|
|
|
/**
|
|
* Increment the attempt to call
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function incrementAttempt(): int
|
|
{
|
|
return $this->attemps = $this->attemps + 1;
|
|
}
|
|
}
|