ldc-bogt/app/Lib/CafelApi/Transport/Request.php

203 lines
4.6 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Lib\CafelApi\Transport;
use App\Enums\HttpMethods;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request as Psr7Request;
use GuzzleHttp\Psr7\Utils as Psr7Utils;
use GuzzleHttp\RequestOptions;
use GuzzleHttp\Utils as GuzzleHttpUtils;
use Psr\Http\Message\ResponseInterface;
/**
* The LDC CRM request
* Contains the http method, the requested url and data to pass
*
* @property \App\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 \App\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;
/**
* Request
*
* @param \App\Enums\HttpMethods $method
* @param string $uri
* @param array $headers
* @param array $data
*/
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(
'BOGT CAFEL API client version %s',
config('cafel.client_version')
)
]
]);
}
/**
* Generic accessor
*
* @param string $field
* @return mixed|null
*/
public function __get(string $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 query merge query and data, set data to empty.
if ($this->method->equals(HttpMethods::get())) {
// * Merge query in url with Data
parse_str(\parse_url($uri, \PHP_URL_QUERY) ?? '', $query);
// * Remove query part.
$uri = strtok($uri, '?');
// * Append build query
$uri = sprintf("%s?%s", $uri, http_build_query(array_merge($query, $this->data)));
$this->data = [];
}
$request = new Psr7Request(
\strtoupper($this->method),
sprintf('%s/%s', $this->endpoint, $uri),
$this->headers
);
$request = Psr7Utils::modifyRequest($request, [
'body' => GuzzleHttpUtils::jsonEncode($this->data)
]);
return $request;
};
}
/**
* 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, [
// ! Do not throw HTTP Exceptions.
'http_errors' => false,
'allow_redirects' => false
]);
}
/**
* 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);
}
}