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

Page   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 13
Bugs 0 Features 0
Metric Value
wmc 16
eloc 29
c 13
b 0
f 0
dl 0
loc 76
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A slug() 0 2 1
A getBlock() 0 4 1
A updatedAt() 0 2 1
A publishedAt() 0 2 1
A block() 0 2 1
A preprocess() 0 3 1
A __get() 0 9 4
A __construct() 0 6 1
A hasTag() 0 2 1
A getBlockClass() 0 8 2
A tags() 0 6 2
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
}