Completed
Push — master ( f7cfb0...fc8807 )
by Richard
08:29 queued 12s
created

Folder::sort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
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