1
0
mirror of https://github.com/ovh/php-ovh.git synced 2023-11-05 03:20:26 +01:00

refactor: hint and simplify if/else

This commit is contained in:
Stéphane Bour 2020-12-08 13:22:03 +01:00
parent 6bfd3cac33
commit c042884a32
No known key found for this signature in database
GPG Key ID: 2CEC3F826D073CA7

View File

@ -31,8 +31,11 @@
namespace Ovh; namespace Ovh;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
/** /**
* Wrapper to manage login and exchanges with simpliest Ovh API * Wrapper to manage login and exchanges with simpliest Ovh API
@ -68,42 +71,42 @@ class Api
* *
* @var string * @var string
*/ */
private $endpoint = null; private ?string $endpoint;
/** /**
* Contain key of the current application * Contain key of the current application
* *
* @var string * @var string
*/ */
private $application_key = null; private ?string $application_key;
/** /**
* Contain secret of the current application * Contain secret of the current application
* *
* @var string * @var string
*/ */
private $application_secret = null; private ?string $application_secret;
/** /**
* Contain consumer key of the current application * Contain consumer key of the current application
* *
* @var string * @var string
*/ */
private $consumer_key = null; private ?string $consumer_key;
/** /**
* Contain delta between local timestamp and api server timestamp * Contain delta between local timestamp and api server timestamp
* *
* @var string * @var string
*/ */
private $time_delta = null; private ?string $time_delta;
/** /**
* Contain http client connection * Contain http client connection
* *
* @var Client * @var Client
*/ */
private $http_client = null; private ?Client $http_client;
/** /**
* Construct a new wrapper instance * Construct a new wrapper instance
@ -135,9 +138,9 @@ class Api
} else { } else {
if (!array_key_exists($api_endpoint, $this->endpoints)) { if (!array_key_exists($api_endpoint, $this->endpoints)) {
throw new Exceptions\InvalidParameterException("Unknown provided endpoint"); throw new Exceptions\InvalidParameterException("Unknown provided endpoint");
} else {
$this->endpoint = $this->endpoints[$api_endpoint];
} }
$this->endpoint = $this->endpoints[$api_endpoint];
} }
if (!isset($http_client)) { if (!isset($http_client)) {
@ -151,13 +154,12 @@ class Api
$this->application_secret = $application_secret; $this->application_secret = $application_secret;
$this->http_client = $http_client; $this->http_client = $http_client;
$this->consumer_key = $consumer_key; $this->consumer_key = $consumer_key;
$this->time_delta = null;
} }
/** /**
* Calculate time delta between local machine and API's server * Calculate time delta between local machine and API's server
* *
* @throws \GuzzleHttp\Exception\ClientException if http request is an error * @throws ClientException if http request is an error
* @return int * @return int
*/ */
private function calculateTimeDelta() private function calculateTimeDelta()
@ -184,7 +186,7 @@ class Api
* @param string $redirection url to redirect on your website after authentication * @param string $redirection url to redirect on your website after authentication
* *
* @return mixed * @return mixed
* @throws \GuzzleHttp\Exception\ClientException if http request is an error * @throws ClientException if http request is an error
*/ */
public function requestCredentials( public function requestCredentials(
array $accessRules, array $accessRules,
@ -213,15 +215,18 @@ class Api
* This is the main method of this wrapper. It will * This is the main method of this wrapper. It will
* sign a given query and return its result. * sign a given query and return its result.
* *
* @param string $method HTTP method of request (GET,POST,PUT,DELETE) * @param string $method HTTP method of request (GET,POST,PUT,DELETE)
* @param string $path relative url of API request * @param string $path relative url of API request
* @param \stdClass|array|null $content body of the request * @param \stdClass|array|null $content body of the request
* @param bool $is_authenticated if the request use authentication * @param bool $is_authenticated if the request use authentication
* *
* @return array * @param null $headers
* @throws \GuzzleHttp\Exception\ClientException if http request is an error * @return ResponseInterface
* @throws Exceptions\InvalidParameterException
* @throws GuzzleException
* @throws \JsonException
*/ */
protected function rawCall($method, $path, $content = null, $is_authenticated = true, $headers = null) protected function rawCall($method, $path, $content = null, $is_authenticated = true, $headers = null): ResponseInterface
{ {
if ($is_authenticated) { if ($is_authenticated) {
if (!isset($this->application_key)) { if (!isset($this->application_key)) {
@ -235,7 +240,7 @@ class Api
$url = $this->endpoint . $path; $url = $this->endpoint . $path;
$request = new Request($method, $url); $request = new Request($method, $url);
if (isset($content) && $method == 'GET') { if (isset($content) && $method === 'GET') {
$query_string = $request->getUri()->getQuery(); $query_string = $request->getUri()->getQuery();
$query = array(); $query = array();
@ -264,7 +269,7 @@ class Api
$request = $request->withUri($url); $request = $request->withUri($url);
$body = ""; $body = "";
} elseif (isset($content)) { } elseif (isset($content)) {
$body = json_encode($content, JSON_UNESCAPED_SLASHES); $body = json_encode($content, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);
$request->getBody()->write($body); $request->getBody()->write($body);
} else { } else {
@ -301,38 +306,40 @@ class Api
/** /**
* Decode a Response object body to an Array * Decode a Response object body to an Array
* *
* @param Response $response * @param Response $response
* *
* @return array * @return array
* @throws \JsonException
*/ */
private function decodeResponse(Response $response) private function decodeResponse(Response $response): array
{ {
return json_decode($response->getBody(), true); return json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
} }
/** /**
* Wrap call to Ovh APIs for GET requests * Wrap call to Ovh APIs for GET requests
* *
* @param string $path path ask inside api * @param string $path path ask inside api
* @param array $content content to send inside body of request * @param array $content content to send inside body of request
* @param array headers custom HTTP headers to add on the request * @param array headers custom HTTP headers to add on the request
* @param bool is_authenticated if the request need to be authenticated * @param bool is_authenticated if the request need to be authenticated
* *
* @return array * @return array
* @throws \GuzzleHttp\Exception\ClientException if http request is an error * @throws ClientException if http request is an error
* @throws \JsonException
*/ */
public function get($path, $content = null, $headers = null, $is_authenticated = true) public function get($path, $content = null, $headers = null, $is_authenticated = true): array
{ {
if (preg_match('/^\/[^\/]+\.json$/', $path)) { if (preg_match('/^\/[^\/]+\.json$/', $path)) {
// Schema description must be access without authentication // Schema description must be access without authentication
return $this->decodeResponse( return $this->decodeResponse(
$this->rawCall("GET", $path, $content, false, $headers) $this->rawCall("GET", $path, $content, false, $headers)
); );
} else {
return $this->decodeResponse(
$this->rawCall("GET", $path, $content, $is_authenticated, $headers)
);
} }
return $this->decodeResponse(
$this->rawCall("GET", $path, $content, $is_authenticated, $headers)
);
} }
/** /**
@ -344,9 +351,9 @@ class Api
* @param bool is_authenticated if the request need to be authenticated * @param bool is_authenticated if the request need to be authenticated
* *
* @return array * @return array
* @throws \GuzzleHttp\Exception\ClientException if http request is an error * @throws ClientException if http request is an error
*/ */
public function post($path, $content = null, $headers = null, $is_authenticated = true) public function post($path, $content = null, $headers = null, $is_authenticated = true): array
{ {
return $this->decodeResponse( return $this->decodeResponse(
$this->rawCall("POST", $path, $content, $is_authenticated, $headers) $this->rawCall("POST", $path, $content, $is_authenticated, $headers)
@ -362,9 +369,9 @@ class Api
* @param bool is_authenticated if the request need to be authenticated * @param bool is_authenticated if the request need to be authenticated
* *
* @return array * @return array
* @throws \GuzzleHttp\Exception\ClientException if http request is an error * @throws ClientException if http request is an error
*/ */
public function put($path, $content, $headers = null, $is_authenticated = true) public function put($path, $content, $headers = null, $is_authenticated = true): array
{ {
return $this->decodeResponse( return $this->decodeResponse(
$this->rawCall("PUT", $path, $content, $is_authenticated, $headers) $this->rawCall("PUT", $path, $content, $is_authenticated, $headers)
@ -380,9 +387,9 @@ class Api
* @param bool is_authenticated if the request need to be authenticated * @param bool is_authenticated if the request need to be authenticated
* *
* @return array * @return array
* @throws \GuzzleHttp\Exception\ClientException if http request is an error * @throws ClientException if http request is an error
*/ */
public function delete($path, $content = null, $headers = null, $is_authenticated = true) public function delete($path, $content = null, $headers = null, $is_authenticated = true): array
{ {
return $this->decodeResponse( return $this->decodeResponse(
$this->rawCall("DELETE", $path, $content, $is_authenticated, $headers) $this->rawCall("DELETE", $path, $content, $is_authenticated, $headers)
@ -392,7 +399,7 @@ class Api
/** /**
* Get the current consumer key * Get the current consumer key
*/ */
public function getConsumerKey() public function getConsumerKey(): ?string
{ {
return $this->consumer_key; return $this->consumer_key;
} }
@ -400,7 +407,7 @@ class Api
/** /**
* Return instance of http client * Return instance of http client
*/ */
public function getHttpClient() public function getHttpClient(): ?Client
{ {
return $this->http_client; return $this->http_client;
} }