Completed
Pull Request — master (#2)
by Tomáš
09:22
created

DeviceInfo::getOs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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