Passed
Push — master ( 2e0e22...399417 )
by Richard
02:44 queued 10s
created

Folder::requestStories()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 25
rs 9.7333
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 = 5;
18
	private $folderPath;
19
20
	public function __construct($folderPath)
21
	{
22
		$this->folderPath = $folderPath;
23
	}
24
25
26
	public function read() {
27
		$response = $this->requestStories(resolve('Storyblok\Client'));
28
29
		$stories = collect($response->responseBody['stories']);
30
31
		$stories->transform(function ($story) {
32
			$blockClass = $this->getBlockClass($story['content']['component']);
33
34
			return new $blockClass($story);
35
		});
36
37
		return $stories;
38
	}
39
40
	private function requestStories(Client $storyblokClient) {
41
42
		if (request()->has('_storyblok') || !config('storyblok.cache')) {
43
			$response = $storyblokClient->getStories([
44
				'is_startpage' => $this->startPage,
45
				'sort_by' => 'content.publish_date:desc',
46
				'starts_with' => $this->folderPath,
47
				'page' => $this->currentPage,
48
				'per_page' => $this->perPage,
49
			]);
50
		} else {
51
			$response = Cache::remember('folder-' . $this->folderPath, config('config.cache_duration') * 60, function () use ($storyblokClient) {
52
				return $storyblokClient->getStories([
53
					'is_startpage' => $this->startPage,
54
					'sort_by' => 'content.publish_date:desc',
55
					'starts_with' => $this->folderPath,
56
					'page' => $this->currentPage,
57
					'per_page' => $this->perPage,
58
				]);
59
			});
60
61
62
		}
63
64
		return $response;
65
66
	}
67
}