2022-06-20 14:47:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This Software is the property of Data Development and is protected
|
|
|
|
* by copyright law - it is NOT Freeware.
|
|
|
|
* Any unauthorized use of this software without a valid license
|
|
|
|
* is a violation of the license agreement and will be prosecuted by
|
|
|
|
* civil and criminal law.
|
|
|
|
* http://www.shopmodule.com
|
|
|
|
*
|
|
|
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
|
|
|
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
|
|
|
* @link http://www.oxidmodule.com
|
|
|
|
*/
|
|
|
|
|
2022-06-24 16:00:56 +02:00
|
|
|
declare( strict_types = 1 );
|
|
|
|
|
2022-06-20 14:47:33 +02:00
|
|
|
namespace D3\LinkmobilityClient;
|
|
|
|
|
2022-06-24 14:35:17 +02:00
|
|
|
use D3\LinkmobilityClient\Exceptions\ApiException;
|
2022-06-24 16:00:56 +02:00
|
|
|
use D3\LinkmobilityClient\Exceptions\ExceptionMessages;
|
2022-06-20 14:47:33 +02:00
|
|
|
use D3\LinkmobilityClient\Request\RequestInterface;
|
2022-07-10 21:52:37 +02:00
|
|
|
use GuzzleHttp\ClientInterface;
|
2022-06-24 16:00:56 +02:00
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
2022-07-01 09:24:16 +02:00
|
|
|
use InvalidArgumentException;
|
2022-06-24 14:35:17 +02:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2022-07-01 09:24:16 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2022-06-20 14:47:33 +02:00
|
|
|
|
|
|
|
class Client
|
|
|
|
{
|
|
|
|
private $accessToken;
|
|
|
|
public $apiUrl;
|
|
|
|
public $requestClient;
|
|
|
|
|
2022-07-01 09:24:16 +02:00
|
|
|
private $logger;
|
|
|
|
|
2022-07-10 21:52:37 +02:00
|
|
|
public function __construct(string $accessToken, UrlInterface $apiUrl = null, ClientInterface $client = null)
|
2022-06-20 14:47:33 +02:00
|
|
|
{
|
|
|
|
$this->accessToken = $accessToken;
|
|
|
|
$this->apiUrl = $apiUrl ?: new Url();
|
|
|
|
$this->requestClient = $client ?: new \GuzzleHttp\Client( [ 'base_uri' => $this->apiUrl->getBaseUri() ] );
|
|
|
|
}
|
|
|
|
|
2022-06-24 16:00:56 +02:00
|
|
|
/**
|
|
|
|
* @param RequestInterface $request
|
|
|
|
*
|
|
|
|
* @return Response\ResponseInterface
|
|
|
|
* @throws ApiException
|
|
|
|
* @throws GuzzleException
|
2022-07-01 09:24:16 +02:00
|
|
|
* @throws InvalidArgumentException
|
2022-06-24 16:00:56 +02:00
|
|
|
*/
|
|
|
|
public function request(RequestInterface $request) : Response\ResponseInterface
|
2022-06-20 14:47:33 +02:00
|
|
|
{
|
|
|
|
$request->validate();
|
2022-06-24 14:35:17 +02:00
|
|
|
|
2022-06-20 14:47:33 +02:00
|
|
|
return $request->getResponseInstance(
|
|
|
|
$this->rawRequest($request->getUri(), $request->getMethod(), $request->getOptions())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $url
|
|
|
|
* @param string $method
|
2022-06-24 16:00:56 +02:00
|
|
|
* @param array $options
|
2022-06-20 14:47:33 +02:00
|
|
|
*
|
2022-06-24 14:35:17 +02:00
|
|
|
* @return ResponseInterface
|
2022-06-20 14:47:33 +02:00
|
|
|
* @throws ApiException
|
|
|
|
* @throws GuzzleException
|
|
|
|
*/
|
2022-06-24 14:35:17 +02:00
|
|
|
protected function rawRequest( $url, string $method = RequestInterface::METHOD_GET, array $options = []): ResponseInterface
|
2022-06-20 14:47:33 +02:00
|
|
|
{
|
2022-06-24 14:35:17 +02:00
|
|
|
$options['headers']['Authorization'] = 'Bearer '.$this->accessToken;
|
|
|
|
|
2022-07-01 09:24:16 +02:00
|
|
|
if ($this->hasLogger()) $this->getLogger()->debug('request '.$url, $options);
|
|
|
|
|
2022-06-20 14:47:33 +02:00
|
|
|
$response = $this->requestClient->request(
|
|
|
|
$method,
|
|
|
|
$url,
|
|
|
|
$options
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($response->getStatusCode() != 200) {
|
2022-07-10 21:52:37 +02:00
|
|
|
$message = sprintf(ExceptionMessages::NOK_REQUEST_RETURN, $url, $response->getStatusCode());
|
2022-07-01 09:24:16 +02:00
|
|
|
if ($this->hasLogger()) $this->getLogger()->error($message);
|
|
|
|
throw new ApiException($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->hasLogger()) {
|
|
|
|
$response->getBody()->rewind();
|
|
|
|
$this->getLogger()->debug('response', [$response->getBody()->getContents()]);
|
2022-06-20 14:47:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2022-07-01 09:24:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $logger
|
|
|
|
*
|
|
|
|
* @return Client
|
|
|
|
*/
|
|
|
|
public function setLogger(LoggerInterface $logger ) : Client
|
|
|
|
{
|
|
|
|
$this->logger = $logger;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasLogger() : bool
|
|
|
|
{
|
|
|
|
return $this->logger instanceof LoggerInterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return LoggerInterface
|
|
|
|
*/
|
2022-07-10 21:52:37 +02:00
|
|
|
public function getLogger(): ?LoggerInterface
|
2022-07-01 09:24:16 +02:00
|
|
|
{
|
2022-07-10 21:52:37 +02:00
|
|
|
return $this->hasLogger() ? $this->logger : null;
|
2022-07-04 13:31:12 +02:00
|
|
|
}
|
2022-06-20 14:47:33 +02:00
|
|
|
}
|