Completed
Push — master ( da7383...0076ce )
by Richard
09:03 queued 29s
created

Page   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Importance

Changes 13
Bugs 0 Features 0
Metric Value
eloc 61
c 13
b 0
f 0
dl 0
loc 206
rs 9.68
wmc 34

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getBlocks() 0 4 1
A view() 0 4 3
A views() 0 31 4
A postProcess() 0 11 3
A slug() 0 3 1
A content() 0 2 1
A render() 0 10 2
A title() 0 14 6
A preprocess() 0 9 2
A __get() 0 9 4
A metaDescription() 0 14 6
A __construct() 0 3 1
1
<?php
2
3
4
namespace Riclep\Storyblok;
5
6
use Exception;
7
use Riclep\Storyblok\Traits\ProcessesBlocks;
8
9
abstract class Page
10
{
11
	use ProcessesBlocks;
12
13
	public $_meta;
14
15
	protected $title;
16
17
	private $content;
18
	private $processedJson;
19
20
	public function __construct($rawStory)
21
	{
22
		$this->processedJson = $rawStory;
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
			$this->_meta['seo'] = $this->processedJson['seo'];
35
		}
36
37
		return $this;
38
	}
39
40
	/**
41
	 * Perform actions on the data after all blocks have been prepared
42
	 *
43
	 * @return Page
44
	 */
45
	public function postProcess()
46
	{
47
		$this->content()->makeComponentPath([]);
48
49
		foreach (class_uses_recursive($this) as $trait) {
50
			if (method_exists($this, $method = 'init' . class_basename($trait))) {
51
				$this->{$method}();
52
			}
53
		}
54
55
		return $this;
56
	}
57
58
	/**
59
	 * Process the root Block
60
	 *
61
	 * @return $this
62
	 */
63
	public function getBlocks() {
64
		$this->content = $this->processBlock($this->processedJson['content'], 'root');
65
66
		return $this;
67
	}
68
69
70
	/**
71
	 * Returns an array of possible views for the current page
72
	 *
73
	 * @return array
74
	 */
75
	protected function views() {
76
		$views = [];
77
78
		//$viewFile = strtolower(subStr((new \ReflectionClass($this))->getShortName(), 0, -4));
79
80
		$segments = explode('/', rtrim($this->slug(), '/'));
81
82
		// match full path first
83
		$views[] = config('storyblok.view_path') . 'pages.' . implode('.', $segments);
84
85
		// creates an array of dot paths for each path segment
86
		// site.com/this/that/them becomes:
87
		// this.that.them
88
		// this.that
89
		// this
90
		while (count($segments) >= 1) {
91
			if (!in_array($path = config('storyblok.view_path') . 'pages.' . implode('.', $segments), $views)) {
92
				$views[] = config('storyblok.view_path') . 'pages.' . implode('.', $segments) . '.' . $this->content()->component();
93
				$views[] = $path;
94
			}
95
96
			array_pop($segments);
97
		}
98
99
		if (!in_array($path = config('storyblok.view_path') . 'pages.' . $this->content()->component(), $views)) {
100
			$views[] = config('storyblok.view_path') . 'pages.' . $this->content()->component();
101
		}
102
103
		$views[] = config('storyblok.view_path') . 'pages.default';
104
105
		return $views;
106
	}
107
108
109
	/**
110
	 * Returns tne matching view
111
	 *
112
	 * @return mixed
113
	 */
114
	public function view() {
115
		foreach ($this->views() as $view) {
116
			if (view()->exists($view)) {
117
				return $view;
118
			}
119
		}
120
	}
121
122
	/**
123
	 * Reads the story
124
	 *
125
	 * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
126
	 */
127
	public function render($additionalContent = null) {
128
		$content = [
129
			'story' => $this,
130
		];
131
132
		if ($additionalContent) {
133
			$content = array_merge($content, $additionalContent);
134
		}
135
136
		return view($this->view(), $content);
137
	}
138
139
	/**
140
	 * Returns the Page’s title
141
	 *
142
	 * @return string
143
	 */
144
	public function title() {
145
		if (property_exists($this, 'titleField') && $this->titleField) {
0 ignored issues
show
Bug Best Practice introduced by
The property titleField does not exist on Riclep\Storyblok\Page. Since you implemented __get, consider adding a @property annotation.
Loading history...
146
			return strip_tags($this->content[$this->titleField]);
147
		}
148
149
		if ($this->_meta['seo'] && $this->_meta['seo']['title']) {
150
			return $this->_meta['seo']['title'];
151
		}
152
153
		if (config('seo.default_title')) {
154
			return config('seo.default_title');
155
		}
156
157
		return $this->processedJson['name'];
158
	}
159
160
	/**
161
	 * Returns the Page’s meta description
162
	 *
163
	 * @return string
164
	 */
165
	public function metaDescription() {
166
		if (property_exists($this, 'descriptionField') && $this->descriptionField) {
0 ignored issues
show
Bug Best Practice introduced by
The property descriptionField does not exist on Riclep\Storyblok\Page. Since you implemented __get, consider adding a @property annotation.
Loading history...
167
			return strip_tags($this->content[$this->descriptionField]);
168
		}
169
170
		if ($this->_meta['seo'] && $this->_meta['seo']['description']) {
171
			return $this->_meta['seo']['description'];
172
		}
173
174
		if (config('seo.default_description')) {
175
			return config('seo.default_description');
176
		}
177
178
		return null;
179
	}
180
181
	/**
182
	 * Return the Page’s content Collection
183
	 *
184
	 * @return mixed
185
	 */
186
	public function content() {
187
		return $this->content;
188
	}
189
190
	/**
191
	 * Get the Page’s content
192
	 *
193
	 * @return string
194
	 */
195
	public function slug()
196
	{
197
		return $this->processedJson['full_slug'];
198
	}
199
200
	/**
201
	 * Returns content items from the page’s content-type Block
202
	 *
203
	 * @param $name
204
	 * @return bool|string
205
	 */
206
	public function __get($name) {
207
		try {
208
			if ($this->content && $this->content->has($name)) {
209
				return $this->content->{$name};
210
			}
211
212
			return false;
213
		} catch (Exception $e) {
214
			return 'Caught exception: ' .  $e->getMessage();
215
		}
216
	}
217
}