Lines: 80.95%
| Line | Hits | Source |
|---|---|---|
| 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 | 11 | $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 | 11 | $client_hints = NULL; |
| 47 | 11 | if ($server) { |
| 48 | 11 | $client_hints = ClientHints::factory($server); |
| 49 | } | |
| 50 | ||
| 51 | 11 | $dd = new DeviceDetector($user_agent, $client_hints); |
| 52 | 11 | $dd->parse(); |
| 53 | ||
| 54 | 11 | return $dd; |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * {@inheritdoc} | |
| 59 | */ | |
| 60 | public function doDeviceFields(array &$fields, string $user_agent, ?array $server = NULL): void { | |
| 61 | 11 | $dd = $this->getDeviceDetector($user_agent, $server); |
| 62 | 11 | $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 | 11 | $fields['config_browser_engine'] = $dd->getClient('engine'); |
| 76 | 11 | $fields['config_browser_name'] = $dd->getClient('short_name'); |
| 77 | 11 | $fields['config_browser_version'] = $dd->getClient('version'); |
| 78 | 11 | $fields['config_client_type'] = $dd->getClient('type'); |
| 79 | 11 | $fields['config_device_brand'] = $dd->getBrandName(); |
| 80 | 11 | $fields['config_device_model'] = $dd->getModel(); |
| 81 | 11 | $fields['config_device_type'] = $dd->getDeviceName(); |
| 82 | 11 | $fields['config_os'] = $dd->getOs('short_name'); |
| 83 | 11 | $fields['config_os_version'] = $dd->getOs('version'); |
| 84 | 11 | $name = ''; |
| 85 | 11 | $bot = $dd->getBot(); |
| 86 | 11 | if (is_array($bot)) { |
| 87 | 2 | $raw = $bot['name'] ?? ''; |
| 88 | 2 | if (is_string($raw)) { |
| 89 | 2 | $name = $raw; |
| 90 | } | |
| 91 | } | |
| 92 | 11 | if ($name !== '') { |
| 93 | 1 | $fields['bot'] = 1; |
| 94 | 1 | $fields['bot_name'] = mb_substr($name, 0, 255); |
| 95 | } | |
| 96 | else { | |
| 97 | 11 | $fields['bot'] = 0; |
| 98 | 11 | $fields['bot_name'] = NULL; |
| 99 | } | |
| 100 | ||
| 101 | 0 | $nullable = [ |
| 102 | 0 | 'config_browser_engine', |
| 103 | 0 | 'config_browser_name', |
| 104 | 0 | 'config_browser_version', |
| 105 | 0 | 'config_client_type', |
| 106 | 0 | 'config_os', |
| 107 | 0 | 'config_os_version', |
| 108 | 0 | ]; |
| 109 | 11 | foreach ($nullable as $field) { |
| 110 | 11 | $value = strtolower($fields[$field]); |
| 111 | 11 | if ($value == 'unk') { |
| 112 | 11 | $fields[$field] = NULL; |
| 113 | } | |
| 114 | } | |
| 115 | } | |
| 116 | ||
| 117 | /** | |
| 118 | * {@inheritdoc} | |
| 119 | */ | |
| 120 | public function hasLibrary($class_name = 'DeviceDetector\ClientHints'): bool { | |
| 121 | 11 | return class_exists($class_name); |
| 122 | } | |
| 123 | ||
| 124 | } |