Agent::search()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 4
nop 1
1
<?php
2
3
/**
4
 * @package Cadmium\Framework\Agent
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace {
11
12
	abstract class Agent {
13
14
		private static $mobiles = [], $robots = [];
15
16
		/**
17
		 * Search an agent in the list
18
		 *
19
		 * @return bool : true if the agent is present in the list, otherwise false
20
		 */
21
22
		private static function search(array &$list) : bool {
23
24
			if (empty($_SERVER['HTTP_USER_AGENT'])) return false;
25
26
			foreach ($list as $item) if (false !== stripos($_SERVER['HTTP_USER_AGENT'], $item)) return true;
27
28
			# ------------------------
29
30
			return false;
31
		}
32
33
		/**
34
		 * Autoloader
35
		 */
36
37
		public static function __autoload() {
38
39
			$file_mobiles = (DIR_DATA . 'Agent/Mobiles.php');
40
41
			$file_robots = (DIR_DATA . 'Agent/Robots.php');
42
43
			if (is_array($mobiles = Explorer::include($file_mobiles))) self::$mobiles = $mobiles;
44
45
			if (is_array($robots = Explorer::include($file_robots))) self::$robots = $robots;
46
		}
47
48
		/**
49
		 * Check if a current user agent is a mobile device
50
		 *
51
		 * @return bool : true if the agent is present in the mobile devices list, otherwise false
52
		 */
53
54
		public static function isMobile() : bool {
55
56
			return self::search(self::$mobiles);
57
		}
58
59
		/**
60
		 * Check if a current user agent is a robot
61
		 *
62
		 * @return bool : true if the agent is present in the robots list, otherwise false
63
		 */
64
65
		public static function isRobot() : bool {
66
67
			return self::search(self::$robots);
68
		}
69
	}
70
}
71