Link   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 194
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A appendFile() 0 12 2
A prependFile() 0 12 2
A removeFile() 0 8 2
A rewrite() 0 7 1
A concat() 0 14 1
A render() 0 27 3
A _getRewriteArray() 0 4 2
A _setRewriteArray() 0 4 1
1
<?php
2
namespace Redaxscript\Head;
3
4
use Redaxscript\Asset;
5
use Redaxscript\Html;
6
use Redaxscript\Registry;
7
use function array_key_exists;
8
use function array_merge;
9
10
/**
11
 * children class to create the link tag
12
 *
13
 * @since 3.0.0
14
 *
15
 * @package Redaxscript
16
 * @category Head
17
 * @author Henry Ruhs
18
 * @author Balázs Szilágyi
19
 */
20
21
class Link extends HeadAbstract
22
{
23
	/**
24
	 * options of the link
25
	 *
26
	 * @var string
27
	 */
28
29
	protected static $_optionArray =
30
	[
31
		'directory' => 'cache/styles',
32
		'extension' => 'css',
33
		'attribute' => 'href',
34
		'lifetime' => 3600
35
	];
36
37
	/**
38
	 * rewrite of the link
39
	 *
40
	 * @var string
41
	 */
42
43
	protected static $_rewriteArray = [];
44
45
	/**
46
	 * append link file
47
	 *
48
	 * @since 3.0.0
49
	 *
50
	 * @param string|array $reference
51
	 *
52
	 * @return self
53
	 */
54
55 1
	public function appendFile($reference = null) : self
56
	{
57 1
		foreach ((array)$reference as $value)
58
		{
59 1
			$this->append(
60
			[
61 1
				'href' => $value,
62 1
				'rel' => 'stylesheet'
63
			]);
64
		}
65 1
		return $this;
66
	}
67
68
	/**
69
	 * prepend link file
70
	 *
71
	 * @since 3.0.0
72
	 *
73
	 * @param string|array $reference
74
	 *
75
	 * @return self
76
	 */
77
78 1
	public function prependFile($reference = null) : self
79
	{
80 1
		foreach ((array)$reference as $value)
81
		{
82 1
			$this->prepend(
83
			[
84 1
				'href' => $value,
85 1
				'rel' => 'stylesheet'
86
			]);
87
		}
88 1
		return $this;
89
	}
90
91
	/**
92
	 * remove link file
93
	 *
94
	 * @since 3.0.0
95
	 *
96
	 * @param string|array $reference
97
	 *
98
	 * @return self
99
	 */
100
101 1
	public function removeFile($reference = null) : self
102
	{
103 1
		foreach ((array)$reference as $value)
104
		{
105 1
			$this->remove('href', $value);
106
		}
107 1
		return $this;
108
	}
109
110
	/**
111
	 * rewrite the link
112
	 *
113
	 * @since 3.0.0
114
	 *
115
	 * @param array $pathArray
116
	 *
117
	 * @return self
118
	 */
119
120 1
	public function rewrite(array $pathArray = []) : self
121
	{
122 1
		$rewriteArray = $this->_getRewriteArray();
123 1
		$rewriteArray = array_merge($rewriteArray, $pathArray);
124 1
		$this->_setRewriteArray($rewriteArray);
125 1
		return $this;
126
	}
127
128
	/**
129
	 * concat the link
130
	 *
131
	 * @since 3.0.0
132
	 *
133
	 * @param array $optionArray
134
	 *
135
	 * @return self
136
	 */
137
138 1
	public function concat(array $optionArray = []) : self
139
	{
140 1
		$optionArray = array_merge(self::$_optionArray, $optionArray);
141 1
		$loader = new Asset\Loader(Registry::getInstance());
142
		$loader
143 1
			->init($this->_getCollectionArray())
144 1
			->concat($optionArray, $this->_getRewriteArray());
145
146
		/* update collection */
147
148 1
		$this->_setRewriteArray();
149 1
		$this->_setCollectionArray($loader->getCollectionArray());
150 1
		return $this;
151
	}
152
153
	/**
154
	 * render the link
155
	 *
156
	 * @since 3.0.0
157
	 *
158
	 * @return string|null
159
	 */
160
161 7
	public function render() : ?string
162
	{
163 7
		$output = null;
164
165
		/* html element */
166
167 7
		$linkElement = new Html\Element();
168 7
		$linkElement->init('link');
169
170
		/* handle collection */
171
172 7
		$collectionArray = $this->_getCollectionArray();
173
174
		/* process collection */
175
176 7
		foreach ($collectionArray as $attribute)
177
		{
178 4
			if (array_key_exists('href', $attribute))
179
			{
180
				$output .= $linkElement
181 4
					->copy()
182 4
					->attr($attribute);
183
			}
184
		}
185 7
		$this->clear();
186 7
		return $output;
187
	}
188
189
	/**
190
	 * get the rewrite array
191
	 *
192
	 * @since 3.0.0
193
	 *
194
	 * @return array
195
	 */
196
197 2
	protected function _getRewriteArray() : array
198
	{
199 2
		return array_key_exists(self::$_namespace, self::$_rewriteArray) ? self::$_rewriteArray[self::$_namespace] : [];
200
	}
201
202
	/**
203
	 * set the rewrite array
204
	 *
205
	 * @since 3.0.0
206
	 *
207
	 * @param array $rewriteArray
208
	 */
209
210 2
	protected function _setRewriteArray(array $rewriteArray = []) : void
211
	{
212 2
		self::$_rewriteArray[self::$_namespace] = $rewriteArray;
213 2
	}
214
}
215