DeviceInfo::fill()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\ForgotPassword\Entity;
6
7
use Doctrine\ORM\Mapping as ORM;
8
use DeviceDetector\DeviceDetector;
9
10
/**
11
 * @ORM\Embeddable
12
 */
13
final class DeviceInfo
14
{
15
	/**
16
	 * @ORM\Column(type="string", length=45)
17
	 *
18
	 * @var string
19
	 */
20
	private $ip = '';
21
22
	/**
23
	 * @ORM\Column(type="string", length=100)
24
	 *
25
	 * @var string
26
	 */
27
	private $os = '';
28
29
	/**
30
	 * @ORM\Column(type="string", length=100)
31
	 *
32
	 * @var string
33
	 */
34
	private $userAgent = '';
35
36
	/**
37
	 * @return void
38
	 */
39
	public function fill(): void
40
	{
41
		$this->ip = $_SERVER['HTTP_CLIENT_IP'] ?? ($_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR']);
42
43
		static $detector = NULL;
44
		if (NULL === $detector) {
45
			$detector = new DeviceDetector($_SERVER['HTTP_USER_AGENT']);
46
			$detector->parse();
47
		}
48
		$detector->parse();
49
		$this->os = sprintf(
50
			'%s %s %s',
51
			$detector->getOs('name'),
52
			$detector->getOs('version'),
53
			$detector->getOs('platform')
54
		);
55
		$this->userAgent = sprintf(
56
			'%s - %s %s, engine: %s %s',
57
			$detector->getClient('type'),
58
			$detector->getClient('name'),
59
			$detector->getClient('version'),
60
			$detector->getClient('engine'),
61
			$detector->getClient('engine_version')
62
		);
63
	}
64
65
	/**
66
	 * @return string
67
	 */
68
	public function getIp(): string
69
	{
70
		return $this->ip;
71
	}
72
73
	/**
74
	 * @return string
75
	 */
76
	public function getOs(): string
77
	{
78
		return $this->os;
79
	}
80
81
	/**
82
	 * @return string
83
	 */
84
	public function getUserAgent(): string
85
	{
86
		return $this->userAgent;
87
	}
88
}
89