Passed
Push — master ( 37d5c2...da7383 )
by Richard
03:04 queued 12s
created

Page   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 12
Bugs 0 Features 0
Metric Value
eloc 50
dl 0
loc 175
rs 10
c 12
b 0
f 0
wmc 22

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A preprocess() 0 10 2
A slug() 0 3 1
A getBlocks() 0 4 1
A view() 0 4 3
A views() 0 31 4
A content() 0 2 1
A render() 0 13 2
A title() 0 6 2
A metaDescription() 0 6 2
A postProcess() 0 11 3
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
	 * Performs any actions on the Storyblok content before it is parsed into Block classes
26
	 * Move SEO plugin out of content to the root of the page’s response
27
	 */
28
	public function preprocess() {
29
		if (array_key_exists('seo', $this->processedJson['content'])) {
30
			$this->processedJson['seo'] = $this->processedJson['content']['seo'];
31
			unset($this->processedJson['content']['seo']);
32
33
			$this->seo = $this->processedJson['seo'];
34
		}
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
			'title' => $this->title(),
130
			'meta_description' => $this->metaDescription(),
131
			'story' => $this->content(),
132
			'seo' => $this->seo,
133
		];
134
135
		if ($additionalContent) {
136
			$content = array_merge($content, $additionalContent);
137
		}
138
139
		return view($this->view(), $content);
140
	}
141
142
	/**
143
	 * Returns the Page’s title
144
	 *
145
	 * @return string
146
	 */
147
	public function title() {
148
		if ($this->seo) {
149
			return $this->seo['title'];
150
		}
151
152
		return $this->processedJson['name'];
153
	}
154
155
	/**
156
	 * Returns the Page’s meta description
157
	 *
158
	 * @return string
159
	 */
160
	public function metaDescription() {
161
		if ($this->seo) {
162
			return $this->seo['description'];
163
		}
164
165
		return  config('seo.default-description');
166
	}
167
168
	/**
169
	 * Return the Page’s content Collection
170
	 *
171
	 * @return mixed
172
	 */
173
	public function content() {
174
		return $this->content;
175
	}
176
177
	/**
178
	 * Get the Page’s content
179
	 *
180
	 * @return string
181
	 */
182
	public function slug()
183
	{
184
		return $this->processedJson['full_slug'];
185
	}
186
}