1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Riclep\Storyblok; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Cache; |
8
|
|
|
use Riclep\Storyblok\Traits\ProcessesBlocks; |
9
|
|
|
use Storyblok\Client; |
10
|
|
|
|
11
|
|
|
abstract class Folder |
12
|
|
|
{ |
13
|
|
|
use ProcessesBlocks; |
14
|
|
|
|
15
|
|
|
protected $startPage = false; |
16
|
|
|
protected $currentPage = 1; |
17
|
|
|
protected $perPage = 10; |
18
|
|
|
protected $sortBy = 'published_at:asc'; |
19
|
|
|
private $slug; |
20
|
|
|
private $settings = []; |
21
|
|
|
|
22
|
|
|
public function read() { |
23
|
|
|
$response = $this->requestStories(resolve('Storyblok\Client')); |
24
|
|
|
|
25
|
|
|
$stories = collect($response->responseBody['stories']); |
26
|
|
|
|
27
|
|
|
$stories->transform(function ($story) { |
28
|
|
|
$blockClass = $this->getBlockClass($story['content']['component']); |
29
|
|
|
|
30
|
|
|
return new $blockClass($story); |
31
|
|
|
}); |
32
|
|
|
|
33
|
|
|
return $stories; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function slug($slug) { |
37
|
|
|
$this->slug = $slug; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function sort($sortBy) { |
41
|
|
|
$this->sortBy = $sortBy; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function settings($settings) { |
45
|
|
|
$this->settings = $settings; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function requestStories(Client $storyblokClient) { |
49
|
|
|
|
50
|
|
|
if (request()->has('_storyblok') || !config('storyblok.cache')) { |
51
|
|
|
$response = $storyblokClient->getStories(array_merge([ |
52
|
|
|
'is_startpage' => $this->startPage, |
53
|
|
|
'sort_by' => $this->sortBy, |
54
|
|
|
'starts_with' => $this->slug, |
55
|
|
|
'page' => $this->currentPage, |
56
|
|
|
'per_page' => $this->perPage, |
57
|
|
|
], $this->settings)); |
58
|
|
|
} else { |
59
|
|
|
$response = Cache::remember('folder-' . $this->slug, config('config.cache_duration') * 60, function () use ($storyblokClient) { |
60
|
|
|
return $storyblokClient->getStories(array_merge([ |
61
|
|
|
'is_startpage' => $this->startPage, |
62
|
|
|
'sort_by' => $this->sortBy, |
63
|
|
|
'starts_with' => $this->slug, |
64
|
|
|
'page' => $this->currentPage, |
65
|
|
|
'per_page' => $this->perPage, |
66
|
|
|
], $this->settings)); |
67
|
|
|
}); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $response; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|