Passed
Push — develop ( fa695b...e5bd83 )
by Richard
26:22
created

Folder::getSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
4
namespace Riclep\Storyblok;
5
6
7
use Illuminate\Pagination\LengthAwarePaginator;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\Facades\Cache;
10
use Riclep\Storyblok\Traits\HasChildClasses;
11
12
abstract class Folder
13
{
14
	use HasChildClasses;
15
16
17
	/**
18
	 * @var int Current pagination page
19
	 */
20
	public int $currentPage = 0;
21
22
23
	/**
24
	 * @var int the total number of stories matching the request
25
	 */
26
	public int $totalStories;
27
28
29
	/**
30
	 * @var null|Collection the collection of stories in the folder
31
	 */
32
	public ?Collection $stories;
33
34
35
	/**
36
	 * @var bool should we request the start / index page
37
	 */
38
	protected bool $startPage = false;
39
40
41
	/**
42
	 * @var int number of items to return
43
	 */
44
	protected int $perPage = 10;
45
46
47
	/**
48
	 * @var string the field to sort by
49
	 */
50
	protected string $sortBy = 'published_at';
51
52
53
	/**
54
	 * @var string order to sort the returned stories
55
	 */
56
	protected string $sortOrder = 'desc';
57
58
59
	/**
60
	 * @var string the slug to start te request from
61
	 */
62
	protected string $slug;
63
64
65
	/**
66
	 * @var array additional settings for the request
67
	 */
68
	protected array $settings = [];
69
70
	/**
71
	 * @var string key used for Laravel's cache
72
	 */
73
	protected string $cacheKey = 'folder-';
74
75
	/**
76
	 * @param $page
77
	 * @param string $pageName
78
	 * @return LengthAwarePaginator
79
	 */
80
	public function paginate($page = null, string $pageName = 'page'): LengthAwarePaginator
81
	{
82
		$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
83
84
		return new LengthAwarePaginator(
85
			$this->stories,
86
			$this->totalStories,
87
			$this->perPage,
88
			$page,
89
			[
90
				'path' => LengthAwarePaginator::resolveCurrentPath(),
91
				'pageName' => $pageName,
92
			]
93
		);
94
	}
95
96
97
	/**
98
	 * Reads a content of the returned stories, processing each one
99
	 *
100
	 * @return Folder
101
	 */
102
	public function read(): Folder
103
	{
104
		$stories = $this->get()->transform(function ($story) {
105
			$blockClass = $this->getChildClassName('Page', $story['content']['component']);
106
107
			return new $blockClass($story);
108
		});
109
110
		$this->stories = $stories;
111
112
		return $this;
113
	}
114
115
116
	/**
117
	 * Sets the slug of the folder to request
118
	 *
119
	 * @param string $slug
120
	 */
121
	public function slug(string $slug): Folder
122
	{
123
		$this->slug = $slug;
124
125
		return $this;
126
	}
127
128
129
	/**
130
	 * The field and order in which we want to sort the stories by
131
	 *
132
	 * @param string $sortBy
133
	 * @param string|null $sortOrder
134
	 */
135
	public function sort(string $sortBy, string $sortOrder = null): Folder
136
	{
137
		$this->sortBy = $sortBy;
138
139
		if ($sortOrder) {
140
			$this->sortOrder = $sortOrder;
141
		}
142
143
		return $this;
144
	}
145
146
147
	/**
148
	 * Sort ascending
149
	 */
150
	public function asc(): Folder
151
	{
152
		$this->sortOrder = 'asc';
153
154
		return $this;
155
	}
156
157
158
	/**
159
	 * Sort descending
160
	 */
161
	public function desc(): Folder
162
	{
163
		$this->sortOrder = 'desc';
164
165
		return $this;
166
	}
167
168
169
	/**
170
	 * Define the settings for the API call
171
	 *
172
	 * @param array $settings
173
	 */
174
	public function settings(array $settings): void
175
	{
176
		$this->settings = $settings;
177
	}
178
179
180
	/**
181
	 * Returns the total number of stories for this page
182
	 *
183
	 * @return int
184
	 */
185
	public function count(): int
186
	{
187
		return $this->stories->count() ?? 0;
0 ignored issues
show
Bug introduced by
The method count() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

187
		return $this->stories->/** @scrutinizer ignore-call */ count() ?? 0;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
188
	}
189
190
191
	/**
192
	 * Sets the number of items per page
193
	 *
194
	 * @param $perPage
195
	 * @return Folder
196
	 */
197
	public function perPage($perPage): Folder
198
	{
199
		$this->perPage = $perPage;
200
201
		return $this;
202
	}
203
204
205
	/**
206
	 * Caches the response and returns just the bit we want
207
	 *
208
	 * @return Collection
209
	 */
210
	protected function get()
211
	{
212
		if (request()->has('_storyblok') || !config('storyblok.cache')) {
213
			$response = $this->makeRequest();
214
		} else {
215
			$uniqueTag = md5(serialize($this->getSettings()));
216
217
			$response = Cache::remember($this->cacheKey . $this->slug . '-' . $uniqueTag, config('storyblok.cache_duration') * 60, function () {
218
				return $this->makeRequest();
219
			});
220
		}
221
222
		$this->totalStories = $response['headers']['Total'][0];
223
224
		return collect($response['stories']);
225
	}
226
227
228
	/**
229
	 * Makes the actual request
230
	 *
231
	 * @return array
232
	 */
233
	protected function makeRequest(): array
234
	{
235
		$storyblokClient = resolve('Storyblok\Client');
236
237
		$storyblokClient =  $storyblokClient->getStories($this->getSettings());
238
239
		return [
240
			'headers' => $storyblokClient->getHeaders(),
241
			'stories' => $storyblokClient->getBody()['stories'],
242
		];
243
	}
244
245
	/**
246
	 * Returns the settings for the folder
247
	 *
248
	 * @return array
249
	 */
250
	protected function getSettings(): array
251
	{
252
		return array_merge([
253
			'is_startpage' => $this->startPage,
254
			'sort_by' => $this->sortBy . ':' . $this->sortOrder,
255
			'starts_with' => $this->slug,
256
			'page' => $this->currentPage,
257
			'per_page' => $this->perPage,
258
		], $this->settings);
259
	}
260
261
	/**
262
	 * Returns the Stories as an array
263
	 *
264
	 * @return array
265
	 */
266
	public function toArray(): array
267
	{
268
		return $this->stories->toArray();
269
	}
270
}