Passed
Branch develop (7ed98a)
by Richard
04:39
created

Page::views()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 31
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 2
b 0
f 0
nc 6
nop 0
dl 0
loc 31
rs 9.8666

1 Method

Rating   Name   Duplication   Size   Complexity  
A Page::getBlock() 0 4 1
1
<?php
2
3
4
namespace Riclep\Storyblok;
5
6
use Exception;
7
use Carbon\Carbon;
8
use Illuminate\Support\Str;
9
10
class Page
11
{
12
	public $_componentPath = ['page'];
13
	protected $_meta = [];
14
15
	private $block;
16
	private $story;
17
18
	public function __construct($story) {
19
		$this->story = $story;
20
21
		$this->preprocess();
22
23
		$this->block = $this->getBlock($this->story['content']);
24
	}
25
26
	public function publishedAt() {
27
		return Carbon::parse($this->story['first_published_at']);
28
	}
29
30
	public function updatedAt() {
31
		return Carbon::parse($this->story['published_at']);
32
	}
33
34
	public function slug() {
35
		return $this->story['full_slug'];
36
	}
37
38
	public function tags($alphabetical = false) {
39
		if ($alphabetical) {
40
			sort($this->story['tag_list']);
41
		}
42
43
		return $this->story['tag_list'];
44
	}
45
46
	public function hasTag($tag) {
47
		return in_array($tag, $this->tags());
48
	}
49
50
	public function block() {
51
		return $this->block;
52
	}
53
54
	public function __get($name) {
55
		try {
56
			if ($this->block && $this->block->has($name)) {
57
				return $this->block->{$name};
58
			}
59
60
			return false;
61
		} catch (Exception $e) {
62
			return 'Caught exception: ' .  $e->getMessage();
63
		}
64
	}
65
66
67
	private function preprocess() {
68
		// TODO extract SEO plugin
69
		$this->story;
70
	}
71
72
	private function getBlockClass($content) {
73
		$component = $content['component'];
74
75
		if (class_exists(config('storyblok.component_class_namespace') . 'Blocks\\' . Str::studly($component))) {
76
			return config('storyblok.component_class_namespace') . 'Blocks\\' . Str::studly($component);
77
		}
78
79
		return config('storyblok.component_class_namespace') . 'Block';
80
	}
81
82
	private function getBlock($content) {
83
		$class = $this->getBlockClass($content);
84
85
		return new $class($content, $this);
86
	}
87
88
	// TODO methods for accessing meta data
89
}