Skip to content

Commit

Permalink
PHP Stan Level 3
Browse files Browse the repository at this point in the history
close #77
  • Loading branch information
roberto-butti committed Dec 17, 2022
1 parent 4070c0f commit 1a90aff
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</p>

## 🚀 Usage
With the Storyblok PHP client you can integrate two kinds of Storyblok APIs:
With the **Storyblok PHP client** you can integrate two kinds of Storyblok APIs:

- Management API: typically used for managing data, like creating data, blocks, settings etc.
- Content Delivery API: typically used for retrieving data, for example when you want to build your public Web application.
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
}
},
"scripts": {
"test": "vendor/bin/pest",
"test-ci": "vendor/bin/pest --ci",
"test": "pest",
"test-ci": "pest --ci",
"phpstan": "phpstan analyse",
"test-coverage": "vendor/bin/pest --coverage",
"test-coverage": "pest --coverage",
"format": "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix --allow-risky=yes --using-cache=no",
"cs": "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix --dry-run --using-cache=no",
"all-check": [
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
parameters:
level: 2
level: 3
paths:
- src
52 changes: 34 additions & 18 deletions src/Storyblok/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Storyblok;

use GuzzleHttp\Client as Guzzle;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\Handler\CurlHandler;
Expand All @@ -18,15 +19,16 @@
class BaseClient
{
const EXCEPTION_GENERIC_HTTP_ERROR = 'An HTTP Error has occurred!';

const DEFAULT_PER_PAGE = 25;

/**
* @var \stdClass
* @var array|string
*/
public $responseBody;

/**
* @var \stdClass
* @var array
*/
public $responseHeaders;

Expand Down Expand Up @@ -83,6 +85,11 @@ function __construct($apiKey = null, $apiEndpoint = null, $apiVersion = 'v2', $s
]);
}

/**
* @param mixed $version
*
* @return $this
*/
public function mockable(array $mocks, $version = 'v2')
{
$handlerStack = HandlerStack::create(new MockHandler($mocks));
Expand All @@ -96,7 +103,7 @@ public function mockable(array $mocks, $version = 'v2')
return $this;
}

public function retryDecider()
public function retryDecider(): \Closure
{
return function (
$retries,
Expand Down Expand Up @@ -137,11 +144,19 @@ public function retryDelay()
};
}

public function setApiKey($apiKey)
/**
* @return self
*/
public function setApiKey(string $apiKey)
{
$this->apiKey = $apiKey;

return $this;
}

/**
* @return string
*/
public function getApiKey()
{
return $this->apiKey;
Expand Down Expand Up @@ -170,7 +185,7 @@ public function getMaxRetries()
/**
* @param array|string $proxy see http://docs.guzzlephp.org/en/stable/request-options.html#proxy for possible values
*
* @return Client
* @return self
*/
public function setProxy($proxy)
{
Expand All @@ -179,6 +194,9 @@ public function setProxy($proxy)
return $this;
}

/**
* @return array|string
*/
public function getProxy()
{
return $this->proxy;
Expand Down Expand Up @@ -210,7 +228,7 @@ public function getTimeout()
* @param string $endpointUrl
* @param array $queryString
*
* @return \stdClass
* @return Response
*
* @throws ApiException
*/
Expand All @@ -236,7 +254,7 @@ public function get($endpointUrl, $queryString = [])
$responseObj = $this->client->request('GET', $endpointUrl, $requestOptions);

return $this->responseHandler($responseObj, $queryString);
} catch (\GuzzleHttp\Exception\ClientException $e) {
} catch (ClientException $e) {
throw new ApiException(self::EXCEPTION_GENERIC_HTTP_ERROR . ' - ' . $e->getMessage(), $e->getCode());
}
}
Expand Down Expand Up @@ -283,22 +301,20 @@ public function getAll(string $endpointUrl, array $queryString = [], bool $retur
}

/**
* @param \Psr\Http\Message\ResponseInterface $responseObj
* @param array $queryString
*
* @return \stdClass
* @param ResponseInterface $responseObj
* @param array $queryString
*/
public function responseHandler($responseObj, $queryString = [])
public function responseHandler($responseObj, $queryString = []): Response
{
$httpResponseCode = $responseObj->getStatusCode();
$result = new Response();
$result->setCode($responseObj->getStatusCode());
$result->setHeaders($responseObj->getHeaders());
$result->setBodyFromStreamInterface($responseObj->getBody());
$data = (string) $responseObj->getBody();
$jsonResponseData = (array) json_decode($data, true);
$result = new \stdClass();

// return response data as json if possible, raw if not
$result->httpResponseBody = $data && empty($jsonResponseData) ? $data : $jsonResponseData;
$result->httpResponseCode = $httpResponseCode;
$result->httpResponseHeaders = $responseObj->getHeaders();

/*
if (\is_array($result->httpResponseBody) && isset($result->httpResponseBody['story']) || isset($result->httpResponseBody['stories'])) {
Expand All @@ -312,7 +328,7 @@ public function responseHandler($responseObj, $queryString = [])
/**
* Gets the json response body.
*
* @return array
* @return array|\stdClass
*/
public function getBody()
{
Expand Down Expand Up @@ -340,7 +356,7 @@ public function getHeaders()
/**
* Gets the response status.
*
* @return array
* @return int
*/
public function getCode()
{
Expand Down
18 changes: 8 additions & 10 deletions src/Storyblok/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,12 @@ public function setCacheVersion()
/**
* Gets cache version from cache or as timestamp.
*
* @return int
* @return null|int
*/
function getCacheVersion()
{
if (empty($this->cv)) {
return '';
return null;
}

return $this->cv;
Expand Down Expand Up @@ -722,7 +722,7 @@ public function enrichContent($data)
return $enrichedContent;
}

public function responseHandler($responseObj, $queryString = [])
public function responseHandler($responseObj, $queryString = []): Response
{
$result = parent::responseHandler($responseObj, $queryString);

Expand Down Expand Up @@ -750,7 +750,7 @@ private function getStory($slug, $byUuid = false)

$this->reCacheOnPublish($cachekey);
if ('published' === $this->getVersion() && $this->cache && $cachedItem = $this->cacheGet($cachekey)) {
if ($this->cacheNotFound && 404 === $cachedItem->httpResponseCode) {
if ($this->cacheNotFound && 404 === $cachedItem->getCode()) {
throw new ApiException(self::EXCEPTION_GENERIC_HTTP_ERROR, 404);
}

Expand Down Expand Up @@ -926,9 +926,9 @@ private function _generateTree($items, $parent = 0)
/**
* Save's the current response in the cache if version is published.
*
* @param array|\stdClass $response
* @param string $key
* @param string $version
* @param Response $response
* @param string $key
* @param string $version
*/
private function _save($response, $key, $version)
{
Expand Down Expand Up @@ -964,10 +964,8 @@ private function cacheGet(string $key)

/**
* Assigns the httpResponseBody and httpResponseHeader to '$this';.
*
* @param mixed $response
*/
private function _assignState($response)
private function _assignState(Response $response)
{
$this->responseBody = $response->httpResponseBody;
$this->responseHeaders = $response->httpResponseHeaders;
Expand Down
21 changes: 5 additions & 16 deletions src/Storyblok/ManagementClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,17 @@ function __construct($apiKey = null, $apiEndpoint = 'mapi.storyblok.com', $apiVe
/**
* @param \Psr\Http\Message\ResponseInterface $responseObj
* @param null|mixed $queryString
*
* @return \stdClass
*/
public function responseHandler($responseObj, $queryString = null)
public function responseHandler($responseObj, $queryString = null): Response
{
$httpResponseCode = $responseObj->getStatusCode();
$data = (string) $responseObj->getBody();
$jsonResponseData = (array) json_decode($data, true);

// return response data as json if possible, raw if not
$this->responseBody = $data && empty($jsonResponseData) ? $data : $jsonResponseData;
$this->responseCode = $httpResponseCode;
$this->responseHeaders = $responseObj->getHeaders();

return $this;
return parent::responseHandler($responseObj, $queryString);
}

/**
* @param string $endpointUrl
* @param array $payload
*
* @return \stdClass
* @return Response
*
* @throws ApiException
*/
Expand Down Expand Up @@ -72,7 +61,7 @@ public function post($endpointUrl, $payload)
* @param string $endpointUrl
* @param array $payload
*
* @return \stdClass
* @return Response
*
* @throws ApiException
*/
Expand All @@ -99,7 +88,7 @@ public function put($endpointUrl, $payload)
/**
* @param string $endpointUrl
*
* @return \stdClass
* @return Response
*
* @throws ApiException
*/
Expand Down
84 changes: 84 additions & 0 deletions src/Storyblok/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Storyblok;

use Psr\Http\Message\StreamInterface;

class Response
{
public $httpResponseBody;

public $httpResponseCode;

public $httpResponseHeaders;

/**
* @return mixed
*/
public function getBody()
{
return $this->httpResponseBody;
}

/**
* @param mixed $httpResponseBody
*
* @return Response
*/
public function setBody($httpResponseBody)
{
$this->httpResponseBody = $httpResponseBody;

return $this;
}

public function setBodyFromStreamInterface(StreamInterface $body): self
{
$data = (string) $body;
$jsonResponseData = (array) json_decode($data, true);
// return response data as json if possible, raw if not
$this->httpResponseBody = $data && empty($jsonResponseData) ? $data : $jsonResponseData;

return $this;
}

/**
* @return null|int
*/
public function getCode()
{
return $this->httpResponseCode;
}

/**
* @param mixed $httpResponseCode
*
* @return Response
*/
public function setCode($httpResponseCode)
{
$this->httpResponseCode = $httpResponseCode;

return $this;
}

/**
* @return mixed
*/
public function getHeaders()
{
return $this->httpResponseHeaders;
}

/**
* @param mixed $httpResponseHeaders
*
* @return Response
*/
public function setHeaders($httpResponseHeaders)
{
$this->httpResponseHeaders = $httpResponseHeaders;

return $this;
}
}
6 changes: 3 additions & 3 deletions tests/Storyblok/ManagementClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
$put = $client->put('test', []);
$delete = $client->delete('test', []);

$this->assertEquals('127.0.0.1', $post->getProxy());
$this->assertEquals('127.0.0.1', $put->getProxy());
$this->assertEquals('127.0.0.1', $delete->getProxy());
$this->assertEquals('127.0.0.1', $client->getProxy());
$this->assertEquals(201, $post->httpResponseCode);
$this->assertEquals(200, $put->httpResponseCode);
});

test('post request throws exception on error', function () {
Expand Down

0 comments on commit 1a90aff

Please sign in to comment.