Passed
Push — master ( 24c314...71620f )
by Richard
02:09
created

Renderable::view()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
4
namespace Riclep\Storyblok\Traits;
5
6
trait Renderable
7
{
8
	protected function views() {
9
		$views[] = 'storyblok.blocks.uuid.' . $this->_uid;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$views was never initialized. Although not strictly required by PHP, it is generally a good practice to add $views = array(); before regardless.
Loading history...
10
		$segments = explode('/', rtrim(app()->make('Page')->slug(), '/'));
11
		// creates an array of dot paths for each path segment
12
		// site.com/this/that/them becomes:
13
		// this.that.them
14
		// this.that
15
		// this
16
		$views[] = 'storyblok.blocks.' . implode('.', $segments) . '.=' . $this->component;
17
		$views[] = 'storyblok.blocks.' . implode('.', $segments) . '.' . $this->component;
18
		while (count($segments) > 1) {
19
			array_pop($segments);
20
			$views[] = 'storyblok.blocks.' . implode('.', $segments) . '.' . $this->component;
21
		}
22
23
		$views[] = 'storyblok.blocks.' . $this->component;
24
		$views[] = 'storyblok.blocks.default';
25
26
		return $views;
27
	}
28
29
	/**
30
	 * Returns the first matching view
31
	 *
32
	 * @return bool|mixed
33
	 */
34
	public function view() {
35
		foreach ($this->views() as $view) {
36
			$view = view()->exists($view) ? $view : false;
37
			break;
38
		}
39
40
		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...
41
	}
42
43
	/**
44
	 * Finds the view used to display this block’s content
45
	 * it will always fall back to a default view.
46
	 *
47
	 */
48
	public function render()
49
	{
50
		return view()->first(
51
			$this->views(),
52
			[
53
				'block' => $this
54
			]
55
		);
56
	}
57
}