Passed
Push — develop ( 87c141...fa695b )
by Richard
06:55
created

Folder::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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
		$settings = array_merge([
213
			'is_startpage' => $this->startPage,
214
			'sort_by' => $this->sortBy . ':' . $this->sortOrder,
215
			'starts_with' => $this->slug,
216
			'page' => $this->currentPage,
217
			'per_page' => $this->perPage,
218
		], $this->settings);
219
220
		if (request()->has('_storyblok') || !config('storyblok.cache')) {
221
			$response = $this->makeRequest();
222
		} else {
223
			$uniqueTag = md5(serialize($settings));
224
225
			$response = Cache::remember($this->cacheKey . $this->slug . '-' . $uniqueTag, config('storyblok.cache_duration') * 60, function () {
226
				return $this->makeRequest();
227
			});
228
		}
229
230
		$this->totalStories = $response['headers']['Total'][0];
231
232
		return collect($response['stories']);
233
	}
234
235
236
	/**
237
	 * Makes the actual request
238
	 *
239
	 * @return array
240
	 */
241
	protected function makeRequest(): array
242
	{
243
		$storyblokClient = resolve('Storyblok\Client');
244
245
		$storyblokClient =  $storyblokClient->getStories(array_merge([
246
			'is_startpage' => $this->startPage,
247
			'sort_by' => $this->sortBy . ':' . $this->sortOrder,
248
			'starts_with' => $this->slug,
249
			'page' => $this->currentPage,
250
			'per_page' => $this->perPage,
251
		], $this->settings));
252
253
		return [
254
			'headers' => $storyblokClient->getHeaders(),
255
			'stories' => $storyblokClient->getBody()['stories'],
256
		];
257
	}
258
259
	public function toArray()
260
	{
261
		return $this->stories->toArray();
262
	}
263
}