Completed
Push — master ( 265d0d...5b91fe )
by Nazar
07:12
created

Meta::__call()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 18
nc 6
nop 2
dl 0
loc 30
rs 5.3846
c 0
b 0
f 0
ccs 19
cts 19
cp 1
crap 8
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2014-2017, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\Page;
9
use
10
	cs\Config,
11
	cs\Language,
12
	cs\Page,
13
	cs\Request,
14
	cs\Singleton,
15
	h;
16
17
/**
18
 * Meta class for generation of various meta tags
19
 *
20
 * @method $this og(string $property, string|string[] $content)
21
 */
22
class Meta {
23
	use
24
		Singleton;
25
	/**
26
	 * Is used as <head prefix="$head_prefix">
27
	 * @var string
28
	 */
29
	public $head_prefix = '';
30
	/**
31
	 * If false - &lt;head&gt; will not be added automatically, and should be in template if needed
32
	 * @var bool
33
	 */
34
	public    $no_head   = false;
35
	protected $links     = '';
36
	protected $og_data   = [];
37
	protected $og_type   = '';
38
	protected $image_src = false;
39
	/**
40
	 * Common wrapper to add all necessary meta tags with images
41
	 *
42
	 * @param string|string[] $images
43
	 *
44
	 * @return Meta
45
	 */
46 3
	public function image ($images) {
47 3
		if (!$images) {
48 3
			return $this;
49
		}
50 3
		$images = (array)$images;
51 3
		if (!$this->image_src && $images[0]) {
52 3
			$this->image_src = true;
53 3
			$this->links .= h::link(
0 ignored issues
show
Bug introduced by
The method link() does not exist on h. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
			$this->links .= h::/** @scrutinizer ignore-call */ link(
Loading history...
54
				[
55 3
					'href' => $images[0],
56 3
					'rel'  => 'image_src'
57
				]
58
			);
59
		}
60 3
		$this->__call('og', ['image', $images]);
61 3
		return $this;
62
	}
63
	/**
64
	 * Common wrapper for generation of various Open Graph protocol meta tags
65
	 *
66
	 * @param string  $type
67
	 * @param mixed[] $params
68
	 *
69
	 * @return Meta
70
	 */
71 21
	public function __call ($type, $params) {
72 21
		if (!$params) {
0 ignored issues
show
introduced by
The condition ! $params can never be false.
Loading history...
73 6
			$this->og_type         = $type;
74 6
			$this->og_data['type'] = h::meta(
0 ignored issues
show
Bug introduced by
The method meta() does not exist on h. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
			/** @scrutinizer ignore-call */ 
75
   $this->og_data['type'] = h::meta(
Loading history...
75
				[
76 6
					'property' => 'og:type',
77 6
					'content'  => $type
78
				]
79
			);
80 6
			return $this;
81
		}
82 21
		if (!$params[0]) {
83 3
			return $this;
84
		}
85 21
		if (is_array($params[1])) {
86 9
			foreach ($params[1] as $p) {
87 9
				$this->__call($type, [$params[0], $p]);
88
			}
89 21
		} elseif ($params[1] || $params[1] === 0) {
90 21
			if (!isset($this->og_data[$params[0]])) {
91 21
				$this->og_data[$params[0]] = '';
92
			}
93 21
			$this->og_data[$params[0]] .= h::meta(
94
				[
95 21
					'property' => "$type:$params[0]",
96 21
					'content'  => $params[1]
97
				]
98
			);
99
		}
100 21
		return $this;
101
	}
102
	/**
103
	 * Generates Open Graph protocol information, and puts it into HTML
104
	 *
105
	 * Usually called by system itself, there is no need to call it manually
106
	 */
107 21
	public function render () {
108 21
		$og = &$this->og_data;
109 21
		$this->fill_required_properties($og);
110 21
		$prefix = 'og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#';
111 21
		$type   = explode('.', $this->og_type, 2)[0];
112 21
		switch ($type) {
113
			case 'article':
114
			case 'blog':
115
			case 'book':
116
			case 'profile':
117
			case 'video':
118
			case 'website':
119 6
				$prefix .= " $type: http://ogp.me/ns/$type#";
120 6
				break;
121
		}
122 21
		$Page       = Page::instance();
123 21
		$Page->Head =
124 21
			$Page->Head.
125 21
			implode('', $og).
126 21
			$this->links;
127 21
		if (!$this->no_head) {
128 21
			$Page->Head = h::head(
0 ignored issues
show
Bug introduced by
The method head() does not exist on h. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
			/** @scrutinizer ignore-call */ 
129
   $Page->Head = h::head(
Loading history...
129 21
				$Page->Head,
130
				[
131 21
					'prefix' => $prefix.$this->head_prefix
132
				]
133
			);
134
		}
135 21
	}
136
	/**
137
	 * If type, title and other important properties were not specified - try to guess and fill them automatically
138
	 *
139
	 * @param array $og
140
	 */
141 21
	protected function fill_required_properties (&$og) {
142 21
		$Page = Page::instance();
143 21
		if (!@$og['title']) {
144 18
			$this->og('title', $Page->Title);
145
		}
146 21
		if (!@$og['description']) {
147 18
			$this->og('description', $Page->Description);
148
		}
149 21
		$Config = Config::instance();
150 21
		if (!@$og['url']) {
151 21
			$Request = Request::instance();
152
			/** @noinspection NestedTernaryOperatorInspection */
153 21
			$this->og(
154 21
				'url',
155 21
				$Request->home_page
156 18
					? $Config->base_url()
157 21
					: ($Page->canonical_url ?: $Config->base_url().'/'.$Request->path_normalized)
0 ignored issues
show
Bug Best Practice introduced by
The property $canonical_url is declared protected in cs\Page. Since you implement __get, consider adding a @property or @property-read.
Loading history...
158
			);
159
		}
160 21
		if (!@$og['site_name']) {
161 21
			$this->og('site_name', $Config->core['site_name']);
162
		}
163 21
		if (!@$og['type']) {
164 15
			$this->og('type', 'website');
165
		}
166 21
		$this->fill_required_properties_multilingual($og);
167 21
	}
168
	/*
169
	 * @param array $og
170
	 */
171 21
	protected function fill_required_properties_multilingual (&$og) {
172 21
		$Config = Config::instance();
173 21
		if (!$Config->core['multilingual']) {
174 18
			return;
175
		}
176 3
		$L = Language::instance();
177 3
		if (!@$og['locale']) {
178 3
			$this->og('locale', $L->clocale);
0 ignored issues
show
Bug Best Practice introduced by
The property clocale does not exist on cs\Language. Since you implemented __get, consider adding a @property annotation.
Loading history...
179
		}
180
		if (
181 3
			!@$og['locale:alternate'] &&
182 3
			count($Config->core['active_languages']) > 1
183
		) {
184 3
			foreach ($Config->core['active_languages'] as $lang) {
185 3
				if ($lang != $L->clanguage) {
186 3
					$this->og('locale:alternate', $L->get('clocale', $lang));
187
				}
188
			}
189
		}
190 3
	}
191
}
192