Passed
Push — master ( 0076ce...72e11f )
by Richard
10:31 queued 15s
created

Folder::sort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
4
namespace Riclep\Storyblok;
5
6
7
use Illuminate\Support\Facades\Cache;
8
use Riclep\Storyblok\Traits\HasChildClasses;
9
use Storyblok\Client;
10
11
abstract class Folder
12
{
13
	use HasChildClasses;
14
15
	/**
16
	 * @var bool should we request the start / index page
17
	 */
18
	protected $startPage = false;
19
20
21
	/**
22
	 * @var int Current pagination page
23
	 */
24
	protected $currentPage = 0;
25
26
27
	/**
28
	 * @var int number of items to return
29
	 */
30
	protected $perPage = 10;
31
32
33
	/**
34
	 * @var string order to sort the returned stories
35
	 */
36
	protected $sortBy = 'content.date:desc';
37
38
39
	/**
40
	 * @var string the slug to start te request from
41
	 */
42
	protected $slug;
43
44
45
	/**
46
	 * @var array additional settings for the request
47
	 */
48
	protected $settings = [];
49
50
	/**
51
	 * Reads a content of the returned stories, processing each one
52
	 *
53
	 * @return \Illuminate\Support\Collection
54
	 */
55
	public function read() {
56
		$response = $this->get();
57
58
		$stories = collect($response);
59
60
		$stories->transform(function ($story) {
61
			$blockClass = $this->getChildClassName('Page', $story['content']['component']);
62
63
			return new $blockClass($story);
64
		});
65
66
		return $stories;
67
	}
68
69
	/**
70
	 * Sets the slug of the folder to request
71
	 *
72
	 * @param $slug
73
	 */
74
	public function slug($slug) {
75
		$this->slug = $slug;
76
	}
77
78
	/**
79
	 * The order in which we want the items in the response to be returned
80
	 *
81
	 * @param $sortBy
82
	 */
83
	public function sort($sortBy) {
84
		$this->sortBy = $sortBy;
85
	}
86
87
	/**
88
	 * Define the settings for the API call
89
	 *
90
	 * @param $settings
91
	 */
92
	public function settings($settings) {
93
		$this->settings = $settings;
94
	}
95
96
97
	/**
98
	 * Caches the response and returns just the bit we want
99
	 *
100
	 * @return array
101
	 */
102
	protected function get()
103
	{
104
		if (request()->has('_storyblok') || !config('storyblok.cache')) {
105
			$response = $this->makeRequest();
106
		} else {
107
			// TODO tagging cache
108
109
			$response = Cache::remember('folder-' . $this->slug, config('storyblok.cache_duration') * 60, function () {
110
				return $this->makeRequest();
111
			});
112
		}
113
114
		return $response['stories'];
115
	}
116
117
	/**
118
	 * Makes the actual request
119
	 *
120
	 * @return array|\Storyblok\stdClass
0 ignored issues
show
Bug introduced by
The type Storyblok\stdClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
121
	 */
122
	private function makeRequest() {
123
		$storyblokClient = resolve('Storyblok\Client');
124
125
		$storyblokClient =  $storyblokClient->getStories(array_merge([
126
			'is_startpage' => $this->startPage,
127
			'sort_by' => $this->sortBy,
128
			'starts_with' => $this->slug,
129
			'page' => $this->currentPage,
130
			'per_page' => $this->perPage,
131
		], $this->settings));
132
133
		return $storyblokClient->getBody();
134
	}
135
}