Skip to content

Commit

Permalink
Merge pull request #75 from roberto-butti/feature/74-phpstan
Browse files Browse the repository at this point in the history
Feature: Introducing Static Code Analysis, PHPStan and refactor
  • Loading branch information
roberto-butti authored Dec 17, 2022
2 parents 3c5dbae + 64ff83b commit c94e540
Show file tree
Hide file tree
Showing 9 changed files with 658 additions and 216 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@ jobs:
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run Static Code Analysis
run: composer phpstan

- name: Run test suite
run: ./vendor/bin/pest --ci
run: composer test-ci
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,15 @@ $client->resolveRelations('component_name1.field_name1,component_name2.field_nam
$client->getStoryBySlug('home');
```

Another example:

```php
use Storyblok\Client;
$client = new Client('your-storyblok-private-token');
$client->resolveRelations('popular-articles.articles');
$result = $client->getStoryBySlug("home")->getBody();
```

In order to resolve links, you can use the `resolveLinks` method passing the specific type of resolving you want to perform among `url`, `story` or `link`:

```php
Expand Down
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
}
],
"autoload": {
"psr-0": {
"Storyblok": "src"
"psr-4": {
"Storyblok\\": "src/Storyblok"
}
},
"autoload-dev": {
Expand All @@ -49,13 +49,21 @@
},
"scripts": {
"test": "vendor/bin/pest",
"test-ci": "vendor/bin/pest --ci",
"phpstan": "phpstan analyse",
"test-coverage": "vendor/bin/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": [
"@format",
"@phpstan",
"@test"
],
"all-check-ci": [
"@cs",
"@phpstan",
"@test-ci"
]

}
}
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
parameters:
level: 0
level: 2
paths:
- src
201 changes: 5 additions & 196 deletions src/Storyblok/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class BaseClient
const DEFAULT_PER_PAGE = 25;

/**
* @var stdClass
* @var \stdClass
*/
public $responseBody;

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

Expand Down Expand Up @@ -63,18 +63,6 @@ class BaseClient
* @var string
*/
private $apiKey;
/**
* List of resolved relations.
*
* @var array
*/
private $resolvedRelations;
/**
* List of resolved relations.
*
* @var array
*/
private $resolvedLinks;

/**
* @param string $apiKey
Expand Down Expand Up @@ -140,7 +128,7 @@ public function retryDecider()
/**
* delay 1s 2s 3s 4s 5s.
*
* @return Closure
* @return \Closure
*/
public function retryDelay()
{
Expand Down Expand Up @@ -312,9 +300,11 @@ public function responseHandler($responseObj, $queryString = [])
$result->httpResponseCode = $httpResponseCode;
$result->httpResponseHeaders = $responseObj->getHeaders();

/*
if (\is_array($result->httpResponseBody) && isset($result->httpResponseBody['story']) || isset($result->httpResponseBody['stories'])) {
$result->httpResponseBody = $this->enrichStories($result->httpResponseBody, $queryString);
}
*/

return $result;
}
Expand Down Expand Up @@ -357,114 +347,6 @@ public function getCode()
return $this->responseCode;
}

/**
* Enrich the Stories with resolved links and stories.
*
* @param \stdClass
* @param mixed $data
*
* @return \stdClass
*/
function enrichContent($data)
{
$enrichedContent = $data;

if (\is_array($data) && isset($data['component'])) {
if (!isset($story['_stopResolving'])) {
foreach ($data as $fieldName => $fieldValue) {
$enrichedContent[$fieldName] = $this->insertRelations($data['component'], $fieldName, $fieldValue);
$enrichedContent[$fieldName] = $this->insertLinks($enrichedContent[$fieldName]);
$enrichedContent[$fieldName] = $this->enrichContent($enrichedContent[$fieldName]);
}
}
} elseif (\is_array($data)) {
foreach ($data as $key => $value) {
$enrichedContent[$key] = $this->enrichContent($value);
}
}

return $enrichedContent;
}

/**
* Retrieve or resolve the relations.
*
* @param \stdClass $data
* @param array $queryString
*/
function getResolvedRelations($data, $queryString)
{
$this->resolvedRelations = [];
$relations = [];

if (isset($data['rels'])) {
$relations = $data['rels'];
} elseif (isset($data['rel_uuids'])) {
$relSize = \count($data['rel_uuids']);
$chunks = [];
$chunkSize = 50;

for ($i = 0; $i < $relSize; $i += $chunkSize) {
$end = min($relSize, $i + $chunkSize);
$chunks[] = \array_slice($data['rel_uuids'], $i, $end);
}

for ($chunkIndex = 0; $chunkIndex < \count($chunks); ++$chunkIndex) {
$relationsParams = [
'per_page' => $chunkSize,
'by_uuids' => implode(',', $chunks[$chunkIndex]),
];
if (isset($queryString['language'])) {
$relationsParams['language'] = $queryString['language'];
}
$relationsRes = $this->getStories($relationsParams);

$relations = array_merge($relations, $relationsRes->responseBody['stories']);
}
}

foreach ($relations as $rel) {
$this->resolvedRelations[$rel['uuid']] = $rel;
}
}

/**
* Retrieve or resolve the Links.
*
* @param \stdClass $data
*/
function getResolvedLinks($data, array $queryString)
{
$this->resolvedLinks = [];
$links = [];

if (isset($data['links'])) {
$links = $data['links'];
} elseif (isset($data['link_uuids'])) {
$linksSize = \count($data['link_uuids']);
$chunks = [];
$chunkSize = 50;

for ($i = 0; $i < $linksSize; $i += $chunkSize) {
$end = min($linksSize, $i + $chunkSize);
$chunks[] = \array_slice($data['link_uuids'], $i, $end);
}

for ($chunkIndex = 0; $chunkIndex < \count($chunks); ++$chunkIndex) {
$linksRes = $this->getStories([
'per_page' => $chunkSize,
'language' => isset($queryString['language']) ? $queryString['language'] : 'default',
'by_uuids' => implode(',', $chunks[$chunkIndex]),
]);

$links = array_merge($links, $linksRes->responseBody['stories']);
}
}
foreach ($links as $link) {
$this->resolvedLinks[$link['uuid']] = $link;
}
}

/**
* @return string
*/
Expand Down Expand Up @@ -509,77 +391,4 @@ private function generateEndpoint($apiEndpoint, $apiVersion, $ssl, $apiRegion)

return $protocol . $apiEndpoint . '/' . $apiVersion . $prefix . '/';
}

private function enrichStories($data, $queryString)
{
$enrichedData = $data;
$this->getResolvedRelations($data, $queryString);
$this->getResolvedLinks($data, $queryString);

if (isset($data['story'])) {
$enrichedData['story']['content'] = $this->enrichContent($data['story']['content']);
} elseif (isset($data['stories'])) {
$stories = [];
foreach ($data['stories'] as $index => $story) {
$story = $data['stories'][$index];
$story['content'] = $this->enrichContent($story['content']);
$stories[] = $story;
}
$enrichedData['stories'] = $stories;
}

return $enrichedData;
}

/**
* Insert the resolved relations in a story.
*
* @param string $component
* @param string $field
* @param array|string $value
*/
private function insertRelations($component, $field, $value)
{
$filteredNode = $value;
if (isset($this->_relationsList[$component]) && $field === $this->_relationsList[$component]) {
if (\is_string($value)) {
if (isset($this->resolvedRelations[$value])) {
$filteredNode = $this->resolvedRelations[$value];
$filteredNode['_stopResolving'] = true;
}
} elseif (\is_array($value)) {
$filteredNode = [];
foreach ($value as $item) {
if (\is_string($item) && isset($this->resolvedRelations[$item])) {
$story = $this->resolvedRelations[$item];
$story['_stopResolving'] = true;
$filteredNode[] = $story;
}
}
}
}

return $filteredNode;
}

/**
* Insert the resolved links in a story.
*
* @param \stdClass $node
*
* @return \stdClass
*/
private function insertLinks($node)
{
$filteredNode = $node;
if (isset($node['fieldtype']) && 'multilink' === $node['fieldtype'] && 'story' === $node['linktype']) {
if (isset($node['id']) && \is_string($node['id']) && isset($this->resolvedLinks[$node['id']])) {
$filteredNode['story'] = $this->resolvedLinks[$node['id']];
} elseif (isset($node['uuid']) && \is_string($node['uuid']) && isset($this->resolvedLinks[$node['uuid']])) {
$filteredNode['story'] = $this->resolvedLinks[$node['uuid']];
}
}

return $filteredNode;
}
}
Loading

0 comments on commit c94e540

Please sign in to comment.