Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
24 / 24 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
DeviceService | |
100.00% |
24 / 24 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDeviceDetector | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
doDeviceFields | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setDeviceFields | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
3 | |||
hasLibrary | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Drupal\visitors\Service; |
4 | |
5 | use DeviceDetector\ClientHints; |
6 | use DeviceDetector\DeviceDetector; |
7 | use Drupal\Core\Database\Connection; |
8 | use Drupal\visitors\VisitorsDeviceInterface; |
9 | |
10 | /** |
11 | * Detects the device type. |
12 | * |
13 | * @package Drupal\visitors\Service |
14 | */ |
15 | class DeviceService implements VisitorsDeviceInterface { |
16 | |
17 | /** |
18 | * The database connection. |
19 | * |
20 | * @var \Drupal\Core\Database\Connection |
21 | */ |
22 | protected $database; |
23 | |
24 | /** |
25 | * DeviceService constructor. |
26 | * |
27 | * @param \Drupal\Core\Database\Connection $database |
28 | * The database connection. |
29 | */ |
30 | public function __construct(Connection $database) { |
31 | $this->database = $database; |
32 | } |
33 | |
34 | /** |
35 | * Gets the device detector. |
36 | * |
37 | * @param string $user_agent |
38 | * The user agent string. |
39 | * @param array $server |
40 | * The server array. |
41 | * |
42 | * @return \DeviceDetector\DeviceDetector |
43 | * The device detector. |
44 | */ |
45 | protected function getDeviceDetector(string $user_agent, ?array $server = NULL): DeviceDetector { |
46 | $client_hints = NULL; |
47 | if ($server) { |
48 | $client_hints = ClientHints::factory($server); |
49 | } |
50 | |
51 | $dd = new DeviceDetector($user_agent, $client_hints); |
52 | $dd->parse(); |
53 | |
54 | return $dd; |
55 | } |
56 | |
57 | /** |
58 | * {@inheritdoc} |
59 | */ |
60 | public function doDeviceFields(array &$fields, string $user_agent, ?array $server = NULL): void { |
61 | $dd = $this->getDeviceDetector($user_agent, $server); |
62 | $this->setDeviceFields($fields, $dd); |
63 | |
64 | } |
65 | |
66 | /** |
67 | * Assigns the device fields to the fields array. |
68 | * |
69 | * @param array $fields |
70 | * The fields array. |
71 | * @param \DeviceDetector\DeviceDetector $dd |
72 | * The DeviceDetector object. |
73 | */ |
74 | protected function setDeviceFields(&$fields, DeviceDetector $dd): void { |
75 | $fields['config_browser_engine'] = $dd->getClient('engine'); |
76 | $fields['config_browser_name'] = $dd->getClient('short_name'); |
77 | $fields['config_browser_version'] = $dd->getClient('version'); |
78 | $fields['config_client_type'] = $dd->getClient('type'); |
79 | $fields['config_device_brand'] = $dd->getBrandName(); |
80 | $fields['config_device_model'] = $dd->getModel(); |
81 | $fields['config_device_type'] = $dd->getDeviceName(); |
82 | $fields['config_os'] = $dd->getOs('short_name'); |
83 | $fields['config_os_version'] = $dd->getOs('version'); |
84 | $fields['bot'] = (int) $dd->isBot(); |
85 | |
86 | $nullable = [ |
87 | 'config_browser_engine', |
88 | 'config_browser_name', |
89 | 'config_browser_version', |
90 | 'config_client_type', |
91 | 'config_os', |
92 | 'config_os_version', |
93 | ]; |
94 | foreach ($nullable as $field) { |
95 | $value = strtolower($fields[$field]); |
96 | if ($value == 'unk') { |
97 | $fields[$field] = NULL; |
98 | } |
99 | } |
100 | } |
101 | |
102 | /** |
103 | * {@inheritdoc} |
104 | */ |
105 | public function hasLibrary($class_name = 'DeviceDetector\ClientHints'): bool { |
106 | return class_exists($class_name); |
107 | } |
108 | |
109 | } |