Passed
Push — develop ( cd9e2a...0290d2 )
by Richard
03:26 queued 13s
created

RequestStory::language()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
4
namespace Riclep\Storyblok;
5
6
7
use Illuminate\Support\Facades\Cache;
8
use Illuminate\Support\Str;
9
use Storyblok\ApiException;
10
11
class RequestStory
12
{
13
	/**
14
	 * @var array An array of relations to resolve matching: component_name.field_name
15
	 * @see https://www.storyblok.com/tp/using-relationship-resolving-to-include-other-content-entries
16
	 */
17
	protected array $resolveRelations = [];
18
19
20
	/**
21
	 * @var string The language version of the Story to load
22
	 * @see https://www.storyblok.com/docs/guide/in-depth/internationalization
23
	 */
24
	protected string $language;
25
26
	/**
27
	 * @var string The fallback language version of the Story to load
28
	 * @see https://www.storyblok.com/docs/guide/in-depth/internationalization
29
	 */
30
	protected string $fallbackLanguage;
31
32
	/**
33
	 * Caches the response if needed
34
	 *
35
	 * @param $slugOrUuid
36
	 * @return mixed
37
	 * @throws ApiException
38
	 */
39
	public function get($slugOrUuid): mixed
40
	{
41
		if (request()->has('_storyblok') || !config('storyblok.cache')) {
42
			$response = $this->makeRequest($slugOrUuid);
43
		} else {
44
			$cache = Cache::getFacadeRoot();
45
46
			if (Cache::getStore() instanceof \Illuminate\Cache\TaggableStore) {
47
				$cache = $cache->tags('storyblok');
48
			}
49
50
			$api_hash = md5(config('storyblok.api_public_key') ?? config('storyblok.api_preview_key'));
51
			$response = $cache->remember($slugOrUuid . '_' . $api_hash , config('storyblok.cache_duration') * 60, function () use ($slugOrUuid) {
52
				return $this->makeRequest($slugOrUuid);
53
			});
54
		}
55
56
		return $response['story'];
57
	}
58
59
	/**
60
	 * Prepares the relations so the format is correct for the API call
61
	 *
62
	 * @param $resolveRelations
63
	 */
64
	public function prepareRelations($resolveRelations): void
65
	{
66
		$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...
67
	}
68
69
	/**
70
	 * Set the language and fallback language to use for this Story, will default to ‘default’
71
	 *
72
	 * @param string $language
73
	 * @param string|null $fallbackLanguage
74
	 */
75
	public function language($language, $fallbackLanguage = null) {
76
		$this->language = $language;
77
		$this->fallbackLanguage = $fallbackLanguage;
78
	}
79
80
	/**
81
	 * Makes the API request
82
	 *
83
	 * @param $slugOrUuid
84
	 * @return array
85
	 * @throws ApiException
86
	 */
87
	private function makeRequest($slugOrUuid): array
88
	{
89
		$storyblokClient = resolve('Storyblok\Client');
90
91
		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...
92
			$storyblokClient = $storyblokClient->resolveRelations($this->resolveRelations);
93
		}
94
95
		if (config('storyblok.resolve_links')) {
96
			$storyblokClient = $storyblokClient->resolveLinks(config('storyblok.resolve_links'));
97
		}
98
99
		if ($this->language) {
100
			$storyblokClient = $storyblokClient->language($this->language);
101
		}
102
103
		if ($this->fallbackLanguage) {
104
			$storyblokClient = $storyblokClient->fallbackLanguage($this->fallbackLanguage);
105
		}
106
107
		if (Str::isUuid($slugOrUuid)) {
108
			$storyblokClient =  $storyblokClient->getStoryByUuid($slugOrUuid);
109
		} else {
110
			$storyblokClient =  $storyblokClient->getStoryBySlug($slugOrUuid);
111
		}
112
113
		return $storyblokClient->getBody();
114
	}
115
}