Passed
Push — master ( 37d5c2...da7383 )
by Richard
03:04 queued 12s
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
	/**
9
	 * Returns an array of possible views for rendering this Block’s content with
10
	 * It checks the URL of the current request and matches this to the folder
11
	 * structure of the views
12
	 *
13
	 * @return array
14
	 * @throws \Illuminate\Contracts\Container\BindingResolutionException
15
	 */
16
	protected function views() {
17
		$views = [];
18
19
		$views[] = config('storyblok.view_path') . 'blocks.uuid.' . $this->_uid;
20
		$segments = explode('/', rtrim(app()->make('Page')->slug(), '/'));
21
		// creates an array of dot paths for each path segment
22
		// site.com/this/that/them becomes:
23
		// this.that.them
24
		// this.that
25
		// this
26
		$views[] = config('storyblok.view_path') . 'blocks.' . implode('.', $segments) . '.=' . $this->component;
27
		$views[] = config('storyblok.view_path') . 'blocks.' . implode('.', $segments) . '.' . $this->component;
28
		while (count($segments) > 1) {
29
			array_pop($segments);
30
			$views[] = config('storyblok.view_path') . 'blocks.' . implode('.', $segments) . '.' . $this->component;
31
		}
32
33
		$views[] = config('storyblok.view_path') . 'blocks.' . $this->component;
34
		$views[] = config('storyblok.view_path') . 'blocks.default';
35
36
		return $views;
37
	}
38
39
	/**
40
	 * Finds the view used to display this block’s content
41
	 * it will always fall back to a default view.
42
	 *
43
	 */
44
	public function render()
45
	{
46
		return view()->first(
47
			$this->views(),
48
			[
49
				'block' => $this
50
			]
51
		);
52
	}
53
}