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

Page::views()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 31
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 31
rs 9.9
1
<?php
2
3
4
namespace Riclep\Storyblok;
5
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
use Riclep\Storyblok\Traits\ProcessesBlocks;
9
10
abstract class Page
11
{
12
	use ProcessesBlocks;
13
14
	private $processedJson;
15
	private $content;
16
	private $seo;
17
	protected $title;
18
19
	public function __construct($rawStory)
20
	{
21
		$this->processedJson = $rawStory;
22
	}
23
24
25
	/**
26
	 * Performs any actions on the Storyblok content before it is parsed into Block classes
27
	 * Move SEO plugin out of content to the root of the page’s response
28
	 */
29
	public function preprocess() {
30
		if (array_key_exists('seo', $this->processedJson['content'])) {
31
			$this->processedJson['seo'] = $this->processedJson['content']['seo'];
32
			unset($this->processedJson['content']['seo']);
33
		}
34
35
		return $this;
36
	}
37
38
	/**
39
	 * Processes the page’s meta content
40
	 */
41
	public function process() {
42
		$this->seo = array_key_exists('seo', $this->processedJson) ? $this->processedJson['seo'] : null;
43
44
		return $this;
45
	}
46
47
	public function getBlocks() {
48
		$this->content = $this->processBlock($this->processedJson['content'], 'root');
49
50
		return $this;
51
	}
52
53
	/**
54
	 * @return array
55
	 */
56
	protected function views() {
57
		$views = [];
58
59
		//$viewFile = strtolower(subStr((new \ReflectionClass($this))->getShortName(), 0, -4));
60
61
		$segments = explode('/', rtrim($this->slug(), '/'));
62
63
		// match full path first
64
		$views[] = 'storyblok.pages.' . implode('.', $segments);
65
66
		// creates an array of dot paths for each path segment
67
		// site.com/this/that/them becomes:
68
		// this.that.them
69
		// this.that
70
		// this
71
		while (count($segments) >= 1) {
72
			// singular views next so we match children with a folder
73
			if (!in_array($path = 'storyblok.pages.' . Str::singular(implode('.', $segments)), $views)) {
74
				$views[] = 'storyblok.pages.' . Str::singular(implode('.', $segments));
75
			}
76
77
			if (!in_array($path = 'storyblok.pages.' . implode('.', $segments), $views)) {
78
				$views[] = $path;
79
			}
80
81
			array_pop($segments);
82
		}
83
84
		$views[] = config('storyblok.view_path') . 'pages.default';
85
86
		return $views;
87
	}
88
89
90
	public function view() {
91
		foreach ($this->views() as $view) {
92
			$view = view()->exists($view) ? $view : false;
93
			break;
94
		}
95
96
		return $view;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $view does not seem to be defined for all execution paths leading up to this point.
Loading history...
97
	}
98
99
	/**
100
	 * Reads the story
101
	 *
102
	 * @return array
103
	 */
104
	public function render($additionalContent = null) {
105
		$content = [
106
			'title' => $this->title(),
107
			'meta_description' => $this->metaDescription(),
108
			'content' => $this->content(),
109
			'seo' => $this->seo,
110
		];
111
112
		if ($additionalContent) {
113
			$content = array_merge($content, $additionalContent);
114
		}
115
116
		return view()->first(
117
			$this->views(),
118
			$content,
119
		);
120
	}
121
122
	public function title() {
123
		if ($this->seo) {
124
			return $this->seo['title'];
125
		}
126
127
		return $this->processedJson['name'];
128
	}
129
130
	public function metaDescription() {
131
		if ($this->seo) {
132
			return $this->seo['description'];
133
		}
134
135
		return  config('seo.default-description');
136
	}
137
138
	public function content() {
139
		return $this->content;
140
	}
141
142
	public function slug()
143
	{
144
		return $this->processedJson['full_slug'];
145
	}
146
}