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 | * @return Folder |
||
121 | */ |
||
122 | public function slug(string $slug): Folder |
||
123 | { |
||
124 | $this->slug = $slug; |
||
125 | |||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | |||
130 | /** |
||
131 | * The field and order in which we want to sort the stories by |
||
132 | * |
||
133 | * @param string $sortBy |
||
134 | * @param string|null $sortOrder |
||
135 | * @return Folder |
||
136 | */ |
||
137 | public function sort(string $sortBy, string $sortOrder = null): Folder |
||
138 | { |
||
139 | $this->sortBy = $sortBy; |
||
140 | |||
141 | if ($sortOrder) { |
||
142 | $this->sortOrder = $sortOrder; |
||
143 | } |
||
144 | |||
145 | return $this; |
||
146 | } |
||
147 | |||
148 | |||
149 | /** |
||
150 | * Sort ascending |
||
151 | */ |
||
152 | public function asc(): Folder |
||
153 | { |
||
154 | $this->sortOrder = 'asc'; |
||
155 | |||
156 | return $this; |
||
157 | } |
||
158 | |||
159 | |||
160 | /** |
||
161 | * Sort descending |
||
162 | */ |
||
163 | public function desc(): Folder |
||
164 | { |
||
165 | $this->sortOrder = 'desc'; |
||
166 | |||
167 | return $this; |
||
168 | } |
||
169 | |||
170 | |||
171 | /** |
||
172 | * Define the settings for the API call |
||
173 | * |
||
174 | * @param array $settings |
||
175 | */ |
||
176 | public function settings(array $settings): void |
||
177 | { |
||
178 | $this->settings = $settings; |
||
179 | } |
||
180 | |||
181 | |||
182 | /** |
||
183 | * Returns the total number of stories for this page |
||
184 | * |
||
185 | * @return int |
||
186 | */ |
||
187 | public function count(): int |
||
188 | { |
||
189 | return $this->stories->count() ?? 0; |
||
0 ignored issues
–
show
|
|||
190 | } |
||
191 | |||
192 | |||
193 | /** |
||
194 | * Sets the number of items per page |
||
195 | * |
||
196 | * @param $perPage |
||
197 | * @return Folder |
||
198 | */ |
||
199 | public function perPage($perPage): Folder |
||
200 | { |
||
201 | $this->perPage = $perPage; |
||
202 | |||
203 | return $this; |
||
204 | } |
||
205 | |||
206 | |||
207 | /** |
||
208 | * Caches the response and returns just the bit we want |
||
209 | * |
||
210 | * @return Collection |
||
211 | */ |
||
212 | protected function get() |
||
213 | { |
||
214 | if (request()->has('_storyblok') || !config('storyblok.cache')) { |
||
215 | $response = $this->makeRequest(); |
||
216 | } else { |
||
217 | $uniqueTag = md5(serialize($this->getSettings())); |
||
218 | |||
219 | $response = Cache::remember($this->cacheKey . $this->slug . '-' . $uniqueTag, config('storyblok.cache_duration') * 60, function () { |
||
220 | return $this->makeRequest(); |
||
221 | }); |
||
222 | } |
||
223 | |||
224 | $this->totalStories = $response['headers']['Total'][0]; |
||
225 | |||
226 | return collect($response['stories']); |
||
227 | } |
||
228 | |||
229 | |||
230 | /** |
||
231 | * Makes the actual request |
||
232 | * |
||
233 | * @return array |
||
234 | */ |
||
235 | protected function makeRequest(): array |
||
236 | { |
||
237 | $storyblokClient = resolve('Storyblok\Client'); |
||
238 | |||
239 | $storyblokClient = $storyblokClient->getStories($this->getSettings()); |
||
240 | |||
241 | return [ |
||
242 | 'headers' => $storyblokClient->getHeaders(), |
||
243 | 'stories' => $storyblokClient->getBody()['stories'], |
||
244 | ]; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Returns the settings for the folder |
||
249 | * |
||
250 | * @return array |
||
251 | */ |
||
252 | protected function getSettings(): array |
||
253 | { |
||
254 | return array_merge([ |
||
255 | 'is_startpage' => $this->startPage, |
||
256 | 'sort_by' => $this->sortBy . ':' . $this->sortOrder, |
||
257 | 'starts_with' => $this->slug, |
||
258 | 'page' => $this->currentPage, |
||
259 | 'per_page' => $this->perPage, |
||
260 | ], $this->settings); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Returns the Stories as an array |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | public function toArray(): array |
||
269 | { |
||
270 | return $this->stories->toArray(); |
||
271 | } |
||
272 | } |
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.