Completed
Branch master (8f7741)
by
unknown
02:56
created

Helper::getKeywords()   C

Complexity

Conditions 7
Paths 10

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 35
Code Lines 16

Code Coverage

Tests 15
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 15
cts 15
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 16
nc 10
nop 0
crap 7
1
<?php
2
namespace Redaxscript\Template;
3
4
use Redaxscript\Asset;
5
use Redaxscript\Db;
6
use Redaxscript\Registry;
7
use Redaxscript\Language;
8
9
/**
10
 * helper class to provide template helpers
11
 *
12
 * @since 3.0.0
13
 *
14
 * @package Redaxscript
15
 * @category Helper
16
 * @author Henry Ruhs
17
 * @author Kim Kha Nguyen
18
 */
19
20
class Helper
21
{
22
	/**
23
	 * prefix for the class
24
	 *
25
	 * @var string
26
	 */
27
28
	protected static $_prefix = 'rs-';
29
30
	/**
31
	 * subset
32
	 *
33
	 * @var string
34
	 */
35
36
	protected static $_subset = 'latin';
37
38
	/**
39
	 * direction
40
	 *
41
	 * @var string
42
	 */
43
44
	protected static $_direction = 'ltr';
45
46
	/**
47
	 * array of robots
48
	 *
49
	 * @var array
50
	 */
51
52
	protected static $_robotArray =
53
	[
54
		'none',
55
		'all',
56
		'index',
57
		'follow',
58
		'index_no',
59
		'follow_no'
60
	];
61
62
	/**
63
	 * array of subsets
64
	 *
65
	 * @var array
66
	 */
67
68
	protected static $_subsetArray =
69
	[
70
		'cyrillic' =>
71
		[
72
			'bg',
73
			'ru'
74
		],
75
		'vietnamese' =>
76
		[
77
			'vi'
78
		]
79
	];
80
81
	/**
82
	 * array of directions
83
	 *
84
	 * @var array
85
	 */
86
87
	protected static $_directionArray =
88
	[
89
		'rtl' =>
90
		[
91
			'ar',
92
			'fa',
93
			'he'
94
		]
95
	];
96
97
	/**
98
	 * array of devices
99
	 *
100
	 * @var array
101
	 */
102
103
	protected static $_deviceArray =
104
	[
105
		'mobile' => 'myMobile',
106
		'tablet' => 'myTablet',
107
		'desktop' => 'myDesktop'
108
	];
109
110
	/**
111
	 * get the canonical
112
	 *
113
	 * @since 3.0.0
114
	 *
115
	 * @return string
116
	 */
117
118
	public static function getCanonical()
119
	{
120
		$lastTable = Registry::get('lastTable');
121
		$lastId = Registry::get('lastId');
122
		$canonicalUrl = Registry::get('root');
123
124
		/* find route */
125
126 2
		if ($lastTable === 'categories')
127
		{
128 2
			$articles = Db::forTablePrefix('articles')->where('category', $lastId);
129 2
			$articlesTotal = $articles->findMany()->count();
130 2
			if ($articlesTotal === 1)
131 2
			{
132 2
				$lastTable = 'articles';
133 2
				$lastId = $articles->findOne()->id;
134 2
			}
135
		}
136
		$canonicalRoute = build_route($lastTable, $lastId);
137
138 2
		/* handle route */
139 2
140 2
		if ($canonicalRoute)
141 2
		{
142 2
			return $canonicalUrl . '/' . Registry::get('parameterRoute') . $canonicalRoute;
143 2
		}
144
		return $canonicalUrl;
145
	}
146
147
	/**
148
	 * get the description
149
	 *
150 2
	 * @since 3.0.0
151
	 *
152
	 * @return string
153
	 */
154
155 2
	public static function getDescription()
156
	{
157
		$lastTable = Registry::get('lastTable');
158 2
		$lastId = Registry::get('lastId');
159
		$metaDescription = Registry::get('metaDescription');
160
161
		/* find description */
162
163
		if ($metaDescription)
164
		{
165
			$description = $metaDescription;
166
		}
167
		else if ($lastTable && $lastId)
168
		{
169 4
			$content = Db::forTablePrefix($lastTable)->whereIdIs($lastId)->findOne();
170
			$description = $content->description;
171 4
172 4
			/* handle parent */
173 4
174
			if (!$description)
175
			{
176
				$parentId = $content->category ? $content->category : $content->parent;
177
				$parent = Db::forTablePrefix('categories')->whereIdIs($parentId)->whereNull('access')->findOne();
178 4
				$description = $parent->description;
179 1
			}
180 1
		}
181 3
182 3
		/* handle description */
183 2
184 2
		if ($description)
185 2
		{
186
			return $description;
0 ignored issues
show
Bug introduced by redaxmedia
The variable $description does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
187
		}
188
		return Db::getSetting('description');
189
	}
190 4
191 3
	/**
192
	 * get the keywords
193 1
	 *
194
	 * @since 3.0.0
195
	 *
196
	 * @return string
197
	 */
198
199
	public static function getKeywords()
200
	{
201
		$lastTable = Registry::get('lastTable');
202
		$lastId = Registry::get('lastId');
203
		$metaKeywords = Registry::get('metaKeywords');
204 4
205
		/* find keywords */
206 4
207 4
		if ($metaKeywords)
208 4
		{
209
			$keywords = $metaKeywords;
210
		}
211
		else if ($lastTable && $lastId)
212
		{
213 4
			$content = Db::forTablePrefix($lastTable)->whereIdIs($lastId)->findOne();
214 1
			$keywords = $content->keywords;
215 1
216 3
			/* handle parent */
217 3
218 2
			if (!$keywords)
219 2
			{
220 2
				$parentId = $content->category ? $content->category : $content->parent;
221
				$parent = Db::forTablePrefix('categories')->whereIdIs($parentId)->whereNull('access')->findOne();
222
				$keywords = $parent->keywords;
223
			}
224
		}
225 4
226 3
		/* handle keywords */
227
228 1
		if ($keywords)
229
		{
230
			return $keywords;
0 ignored issues
show
Bug introduced by redaxmedia
The variable $keywords does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
231
		}
232
		return Db::getSetting('keywords');
233
	}
234
235
	/**
236
	 * get the keywords
237
	 *
238
	 * @since 3.0.0
239 5
	 *
240
	 * @return string
241 5
	 */
242 5
243 5
	public static function getRobots()
244 5
	{
245
		$lastTable = Registry::get('lastTable');
246
		$lastId = Registry::get('lastId');
247
		$contentError = Registry::get('contentError');
248
		$metaRobots = Registry::get('metaRobots');
249 5
250 1
		/* find robots */
251 1
252
		if ($metaRobots)
253 4
		{
254 1
			$robots = $metaRobots;
255 1
		}
256 3
		else if ($contentError)
257 3
		{
258 2
			$robots = 0;
259 2
		}
260 2
		else if ($lastTable && $lastId)
261
		{
262
			$content = Db::forTablePrefix($lastTable)->whereIdIs($lastId)->whereNull('access')->findOne();
263
			$robots = $content->robots;
264 5
265 5
			/* handle parent */
266 4
267
			if (!$robots)
268 1
			{
269
				$parentId = $content->category ? $content->category : $content->parent;
270
				$parent = Db::forTablePrefix('categories')->whereIdIs($parentId)->whereNull('access')->findOne();
271
				$robots = $parent->robots;
272
			}
273
		}
274
275
		/* handle robots */
276
277
		if (array_key_exists($robots, self::$_robotArray))
278
		{
279 1
			return self::$_robotArray[$robots];
0 ignored issues
show
Bug introduced by redaxmedia
The variable $robots does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
280
		}
281 1
		$robots = Db::getSetting('robots');
282 1
		return self::$_robotArray[$robots];
283
	}
284
285
	/**
286
	 * get the transport
287
	 *
288
	 * @since 3.0.0
289
	 *
290
	 * @return string
291
	 */
292
293 6
	public static function getTransport()
294
	{
295 6
		$transport = new Asset\Transport(Registry::getInstance(), Language::getInstance());
296
		return $transport->getArray();
297 6
	}
298 4
299 4
	/**
300
	 * get the subset
301 3
	 *
302 2
	 * @since 3.0.0
303
	 *
304
	 * @return string
305
	 */
306
307
	public static function getSubset()
308
	{
309
		$language = Registry::get('language');
310
		foreach (self::$_subsetArray as $subset => $valueArray)
311
		{
312
			if (in_array($language, $valueArray))
313 3
			{
314
				return $subset;
315 3
			}
316
		}
317 3
		return self::$_subset;
318 3
	}
319 1
320
	/**
321 2
	 * get the direction
322 2
	 *
323
	 * @since 3.0.0
324
	 *
325
	 * @return string
326
	 */
327
328
	public static function getDirection()
329
	{
330
		$language = Registry::get('language');
331
		foreach (self::$_directionArray as $direction => $valueArray)
332
		{
333 4
			if (in_array($language, $valueArray))
334
			{
335 4
				return $direction;
336 4
			}
337 4
		}
338 4
		return self::$_direction;
339
	}
340
341
	/**
342 4
	 * get the class
343
	 *
344 3
	 * @since 3.0.0
345 4
	 *
346 4
	 * @return string
347
	 */
348
349
	public static function getClass()
350
	{
351
		$classArray = array_unique(array_merge(
352
			self::_getBrowserArray(),
353
			self::_getDeviceArray()
354
		));
355
356
		/* process class */
357 4
358
		foreach ($classArray as $key => $value)
359
		{
360
			$classArray[$key] = self::$_prefix . 'is-' . $value;
361 4
		}
362 4
		return implode(' ', $classArray);
363 4
	}
364 4
365
	/**
366
	 * get the browser array
367
	 *
368
	 * @since 3.0.0
369
	 *
370
	 * @return array
371
	 */
372
373
	protected static function _getBrowserArray()
374
	{
375 4
		return
376
		[
377 4
			Registry::get('myBrowser'),
378
			Registry::get('myBrowserVersion'),
379 4
			Registry::get('myEngine')
380
		];
381 4
	}
382
383
	/**
384 3
	 * get the device array
385
	 *
386 3
	 * @since 3.0.0
387
	 *
388 3
	 * @return array
389 1
	 */
390
391
	protected static function _getDeviceArray()
392
	{
393
		foreach (self::$_deviceArray as $system => $value)
394
		{
395
			$device = Registry::get($value);
396
			if ($device)
397
			{
398
				return
399
				[
400
					$system,
401
					$device
402
				];
403
			}
404
		}
405
	}
406
}
407