Tag   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 391
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 31

Test Coverage

Coverage 73.04%

Importance

Changes 0
Metric Value
wmc 36
lcom 0
cbo 31
dl 0
loc 391
ccs 84
cts 115
cp 0.7304
rs 9.52
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A base() 0 5 1
A title() 0 5 1
A link() 0 4 1
A meta() 0 4 1
A script() 0 4 1
A style() 0 4 1
A breadcrumb() 0 5 1
A partial() 0 17 2
A content() 0 5 2
A _renderAdminContent() 0 11 2
A _renderContent() 0 6 1
A article() 0 5 1
A comment() 0 11 2
A extra() 0 5 1
B pagination() 0 31 6
B navigation() 0 29 6
A console() 0 10 2
A consoleForm() 0 5 1
A commentForm() 0 11 2
A searchForm() 0 5 1
1
<?php
2
namespace Redaxscript\Template;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Config;
6
use Redaxscript\Console;
7
use Redaxscript\Filesystem;
8
use Redaxscript\Head;
9
use Redaxscript\Language;
10
use Redaxscript\Model;
11
use Redaxscript\Navigation;
12
use Redaxscript\Registry;
13
use Redaxscript\Request;
14
use Redaxscript\Router;
15
use Redaxscript\View;
16
use function ceil;
17
use function htmlentities;
18
use function strlen;
19
20
/**
21
 * parent class to provide template tags
22
 *
23
 * @since 3.0.0
24
 *
25
 * @package Redaxscript
26
 * @category Template
27
 * @author Henry Ruhs
28
 */
29
30
class Tag
31
{
32
	/**
33
	 * base
34
	 *
35
	 * @since 3.0.0
36
	 *
37
	 * @return string
38
	 */
39
40 1
	public static function base() : string
41
	{
42 1
		$base = new Head\Base(Registry::getInstance());
43 1
		return $base->render();
44
	}
45
46
	/**
47
	 * title
48
	 *
49
	 * @since 3.0.0
50
	 *
51
	 * @param string $text
52
	 *
53
	 * @return string|null
54
	 */
55
56 1
	public static function title(string $text = null) : ?string
57
	{
58 1
		$title = new Head\Title();
59 1
		return $title->render($text);
60
	}
61
62
	/**
63
	 * link
64
	 *
65
	 * @since 3.0.0
66
	 *
67
	 * @return Head\Link
68
	 */
69
70 1
	public static function link() : Head\Link
71
	{
72 1
		return Head\Link::getInstance();
73
	}
74
75
	/**
76
	 * meta
77
	 *
78
	 * @since 3.0.0
79
	 *
80
	 * @return Head\Meta
81
	 */
82
83 1
	public static function meta() : Head\Meta
84
	{
85 1
		return Head\Meta::getInstance();
86
	}
87
88
	/**
89
	 * script
90
	 *
91
	 * @since 3.0.0
92
	 *
93
	 * @return Head\Script
94
	 */
95
96 1
	public static function script() : Head\Script
97
	{
98 1
		return Head\Script::getInstance();
99
	}
100
101
	/**
102
	 * style
103
	 *
104
	 * @since 3.0.0
105
	 *
106
	 * @return Head\Style
107
	 */
108
109 1
	public static function style() : Head\Style
110
	{
111 1
		return Head\Style::getInstance();
112
	}
113
114
	/**
115
	 * breadcrumb
116
	 *
117
	 * @since 2.3.0
118
	 *
119
	 * @param array $optionArray options of the breadcrumb
120
	 *
121
	 * @return string
122
	 */
123
124 1
	public static function breadcrumb(array $optionArray = []) : string
125
	{
126 1
		$breadcrumb = new View\Helper\Breadcrumb(Registry::getInstance(), Language::getInstance());
127 1
		return $breadcrumb->init($optionArray)->render();
128 1
	}
129
130
	/**
131
	 * partial
132
	 *
133
	 * @since 3.2.0
134
	 *
135
	 * @param string|array $partial
136
	 *
137
	 * @return string|null
138
	 */
139
140
	public static function partial($partial = null) : ?string
141 1
	{
142
		$output = null;
143 1
144
		/* template filesystem */
145
146
		$templateFilesystem = new Filesystem\File();
147 1
		$templateFilesystem->init('templates');
148 1
149
		/* process partial */
150
151
		foreach ((array)$partial as $file)
152 1
		{
153
			$output .= $templateFilesystem->renderFile($file);
154 1
		}
155
		return $output;
156 1
	}
157
158
	/**
159
	 * content
160
	 *
161
	 * @since 4.0.0
162
	 *
163
	 * @return string|null
164
	 */
165
166
	public static function content() : ?string
167
	{
168
		$adminContent = self::_renderAdminContent();
169
		return $adminContent ? : self::_renderContent();
170
	}
171
172
	/**
173
	 * render the admin content
174
	 *
175
	 * @since 3.3.0
176
	 *
177
	 * @return string|null
178
	 */
179
180
	protected static function _renderAdminContent() : ?string
181
	{
182
		$registry = Registry::getInstance();
183
		if ($registry->get('token') === $registry->get('loggedIn'))
184
		{
185
			$adminRouter = new Admin\Router\Router(Registry::getInstance(), Request::getInstance(), Language::getInstance(), Config::getInstance());
186
			$adminRouter->init();
187
			return $adminRouter->routeContent();
188
		}
189
		return null;
190
	}
191
192
	/**
193
	 * render the content
194
	 *
195
	 * @since 4.0.0
196
	 *
197
	 * @return string|null
198
	 */
199
200
	protected static function _renderContent() : ?string
201
	{
202
		$router = new Router\Router(Registry::getInstance(), Request::getInstance(), Language::getInstance(), Config::getInstance());
203
		$router->init();
204
		return $router->routeContent();
205
	}
206
207
	/**
208
	 * article
209
	 *
210
	 * @since 4.0.0
211
	 *
212
	 * @param int $categoryId identifier of the category
213
	 * @param int $articleId identifier of the article
214
	 * @param array $optionArray options of the content
215
	 *
216
	 * @return string
217
	 */
218
219
	public static function article(int $categoryId = null, int $articleId = null, array $optionArray = []) : string
220
	{
221
		$article = new View\Article(Registry::getInstance(), Request::getInstance(), Language::getInstance(), Config::getInstance());
222
		return $article->init($optionArray)->render($categoryId, $articleId);
223
	}
224
225
	/**
226
	 * comment
227
	 *
228
	 * @since 4.0.0
229
	 *
230
	 * @param int $articleId identifier of the article
231
	 * @param array $optionArray options of the comment
232
	 *
233
	 * @return string|null
234
	 */
235
236
	public static function comment(int $articleId = null, array $optionArray = []) : ?string
237
	{
238
		$articleModel = new Model\Article();
239
		$article = $articleModel->getById($articleId);
240
		if ($article->comments)
241
		{
242
			$comment = new View\Comment(Registry::getInstance(), Language::getInstance());
243
			return $comment->init($optionArray)->render($articleId);
244
		}
245
		return null;
246
	}
247
248
	/**
249
	 * extra
250
	 *
251
	 * @since 4.0.0
252
	 *
253
	 * @param int $extraId identifier of the extra
254
	 * @param array $optionArray options of the extra
255
	 *
256
	 * @return string
257
	 */
258
259
	public static function extra(int $extraId = null, array $optionArray = []) : string
260
	{
261
		$extra = new View\Extra(Registry::getInstance(), Request::getInstance(), Language::getInstance(), Config::getInstance());
262
		return $extra->init($optionArray)->render($extraId);
263
	}
264
265
	/**
266
	 * pagination
267
	 *
268
	 * @since 4.0.0
269
	 *
270
	 * @param string $type type of the pagination
271
	 * @param int $parentId identifier of the parent
272
	 * @param array $optionArray options of the pagination
273
	 *
274
	 * @return string|null
275
	 */
276
277
	public static function pagination(string $type = null, int $parentId, array $optionArray = []) : ?string
278
	{
279
		$settingModel = new Model\Setting();
280
		if ($settingModel->get('pagination'))
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $settingModel->get('pagination') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
281 3
		{
282
			$categoryModel = new Model\Category();
283 3
			$articleModel = new Model\Article();
284 3
			$commentModel = new Model\Comment();
285
			$registry = Registry::getInstance();
286 3
			$language = $registry->get('language');
287 3
			$route = null;
288 3
			$total = null;
289 3
			$current = $registry->get('lastSubParameter') ? : 1;
290 3
			if ($type === 'articles')
291 3
			{
292 3
				$route = $categoryModel->getRouteById($parentId);
293 3
				$total = ceil($articleModel->countByCategoryAndLanguage($parentId, $language) / $settingModel->get('limit'));
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $language defined by $registry->get('language') on line 286 can also be of type array; however, Redaxscript\Model\Articl...ByCategoryAndLanguage() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
294 3
			}
295
			if ($type === 'comments')
296 1
			{
297 1
				$route = $articleModel->getRouteById($parentId);
298
				$total = ceil($commentModel->countByArticleAndLanguage($parentId, $language) / $settingModel->get('limit'));
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $language defined by $registry->get('language') on line 286 can also be of type array; however, Redaxscript\Model\Commen...tByArticleAndLanguage() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
299 3
			}
300
			if ($total > 1)
301 1
			{
302 1
				$pagination = new View\Helper\Pagination(Registry::getInstance(), Language::getInstance());
303
				return $pagination->init($optionArray)->render($route, $current, $total);
304 3
			}
305
		}
306 2
		return null;
307 2
	}
308 2
309
	/**
310
	 * navigation
311 1
	 *
312
	 * @since 3.0.0
313
	 *
314
	 * @param string $type type of the navigation
315
	 * @param array $optionArray options of the navigation
316
	 *
317
	 * @return string|null
318
	 */
319
320
	public static function navigation(string $type = null, array $optionArray = []) : ?string
321
	{
322
		if ($type === 'articles')
323
		{
324
			$navigation = new Navigation\Article(Registry::getInstance(), Language::getInstance());
325 6
			return $navigation->init($optionArray)->render();
326
		}
327 6
		if ($type === 'categories')
328
		{
329 1
			$navigation = new Navigation\Category(Registry::getInstance(), Language::getInstance());
330 1
			return $navigation->init($optionArray)->render();
331 1
		}
332
		if ($type === 'comments')
333 5
		{
334
			$navigation = new Navigation\Comment(Registry::getInstance(), Language::getInstance());
335 1
			return $navigation->init($optionArray)->render();
336 1
		}
337 1
		if ($type === 'languages')
338
		{
339 4
			$navigation = new Navigation\Language(Registry::getInstance(), Language::getInstance());
340
			return $navigation->init($optionArray)->render();
341 1
		}
342 1
		if ($type === 'templates')
343 1
		{
344
			$navigation = new Navigation\Template(Registry::getInstance(), Language::getInstance());
345 3
			return $navigation->init($optionArray)->render();
346
		}
347 1
		return null;
348 1
	}
349 1
350
	/**
351 2
	 * console
352
	 *
353 1
	 * @since 3.0.0
354 1
	 *
355 1
	 * @return string|null
356
	 */
357 1
358
	public static function console() : ?string
359
	{
360
		$console = new Console\Console(Registry::getInstance(), Request::getInstance(), Language::getInstance(), Config::getInstance());
361
		$output = $console->init('template');
362
		if (strlen($output))
363
		{
364
			return htmlentities($output, ENT_QUOTES);
365
		}
366
		return null;
367
	}
368 2
369
	/**
370 2
	 * console form
371 2
	 *
372 2
	 * @since 3.0.0
373
	 *
374 1
	 * @return string|null
375
	 */
376 1
377
	public static function consoleForm() : string
378
	{
379
		$consoleForm = new View\ConsoleForm(Registry::getInstance(), Language::getInstance());
380
		return $consoleForm->render();
381
	}
382
383
	/**
384
	 * comment form
385
	 *
386
	 * @since 4.0.0
387 1
	 *
388
	 * @param int $articleId identifier of the article
389 1
	 *
390 1
	 * @return string
391
	 */
392
393
	public static function commentForm(int $articleId = null) : ?string
394
	{
395
		$articleModel = new Model\Article();
396
		$article = $articleModel->getById($articleId);
397
		if ($article->comments)
398
		{
399
			$commentForm = new View\CommentForm(Registry::getInstance(), Language::getInstance());
400
			return $commentForm->render($articleId);
401
		}
402
		return null;
403 1
	}
404
405 1
	/**
406 1
	 * search form
407 1
	 *
408
	 * @since 3.0.0
409 1
	 *
410 1
	 * @param string $table name of the table
411
	 *
412
	 * @return string
413
	 */
414
415
	public static function searchForm(string $table = null) : string
416
	{
417
		$searchForm = new View\SearchForm(Registry::getInstance(), Language::getInstance());
418
		return $searchForm->render($table);
419
	}
420
}
421