Passed
Push — master ( 0076ce...72e11f )
by Richard
10:31 queued 15s
created

RequestStory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 20
c 1
b 0
f 0
dl 0
loc 63
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A makeRequest() 0 14 3
A prepareRelations() 0 2 1
A get() 0 16 4
1
<?php
2
3
4
namespace Riclep\Storyblok;
5
6
7
use Illuminate\Support\Facades\Cache;
8
use Illuminate\Support\Str;
9
10
class RequestStory
11
{
12
	/**
13
	 * @var array An array of relations to resolve matching: component_name.field_name
14
	 * @see https://www.storyblok.com/tp/using-relationship-resolving-to-include-other-content-entries
15
	 */
16
	private $resolveRelations;
17
18
	/**
19
	 * Caches the response if needed
20
	 *
21
	 * @param $slugOrUuid
22
	 * @return mixed
23
	 * @throws \Storyblok\ApiException
24
	 */
25
	public function get($slugOrUuid) {
26
		if (request()->has('_storyblok') || !config('storyblok.cache')) {
27
			$response = $this->makeRequest($slugOrUuid);
28
		} else {
29
			$cache = Cache::getFacadeRoot();
30
31
			if (Cache::getStore() instanceof \Illuminate\Cache\TaggableStore) {
32
				$cache = $cache->tags('storyblok');
33
			}
34
35
			$response = $cache->remember($slugOrUuid, config('storyblok.cache_duration') * 60, function () use ($slugOrUuid) {
36
				return $this->makeRequest($slugOrUuid);
37
			});
38
		}
39
40
		return $response['story'];
41
	}
42
43
	/**
44
	 * Prepares the relations so the format is correct for the API call
45
	 *
46
	 * @param $resolveRelations
47
	 */
48
	public function prepareRelations($resolveRelations) {
49
		$this->resolveRelations = implode(',', $resolveRelations);
0 ignored issues
show
Documentation Bug introduced by
It seems like implode(',', $resolveRelations) of type string is incompatible with the declared type array of property $resolveRelations.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
	}
51
52
	/**
53
	 * Makes the API request
54
	 *
55
	 * @param $slugOrUuid
56
	 * @return array|\Storyblok\stdClass
0 ignored issues
show
Bug introduced by
The type Storyblok\stdClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
	 * @throws \Storyblok\ApiException
58
	 */
59
	private function makeRequest($slugOrUuid) {
60
		$storyblokClient = resolve('Storyblok\Client');
61
62
		if ($this->resolveRelations) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->resolveRelations of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
63
			$storyblokClient = $storyblokClient->resolveRelations($this->resolveRelations);
64
		}
65
66
		if (Str::isUuid($slugOrUuid)) {
67
			$storyblokClient =  $storyblokClient->getStoryByUuid($slugOrUuid);
68
		} else {
69
			$storyblokClient =  $storyblokClient->getStoryBySlug($slugOrUuid);
70
		}
71
72
		return $storyblokClient->getBody();
73
	}
74
}