Completed
Push — master ( 471490...1985a6 )
by Antonio Carlos
01:44
created

UserAgentParser::getUserAgent()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
3
namespace PragmaRX\Tracker\Support;
4
5
use UAParser\Parser;
6
7
class UserAgentParser
8
{
9
    public $parser;
10
11
    public $userAgent;
12
13
    public $operatingSystem;
14
15
    public $device;
16
17
    public $originalUserAgent;
18
19
    public function __construct($basePath, $userAgent = '')
20
    {
21
        $this->parser = Parser::create()->parse($this->getUserAgent($userAgent));
22
23
        $this->userAgent = $this->parser->ua;
24
25
        $this->operatingSystem = $this->parser->os;
26
27
        $this->device = $this->parser->device;
28
29
        $this->basePath = $basePath;
0 ignored issues
show
Bug introduced by
The property basePath does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
31
        $this->originalUserAgent = $this->parser->originalUserAgent;
32
    }
33
34
    public function getOperatingSystemVersion()
35
    {
36
        return    $this->operatingSystem->major.
37
                ($this->operatingSystem->minor !== null ? '.'.$this->operatingSystem->minor : '').
38
                ($this->operatingSystem->patch !== null ? '.'.$this->operatingSystem->patch : '');
39
    }
40
41
    protected function getUserAgent($userAgent)
42
    {
43
        if (!empty($userAgent)) {
44
            return $userAgent;
45
        }
46
47
        if (isset($_SERVER['HTTP_USER_AGENT']) && !empty($_SERVER['HTTP_USER_AGENT'])) {
48
            return $_SERVER['HTTP_USER_AGENT'];
49
        }
50
51
        return config('tracker.default_user_agent', '');
52
    }
53
54
    public function getUserAgentVersion()
55
    {
56
        return  $this->userAgent->major.
57
                ($this->userAgent->minor !== null ? '.'.$this->userAgent->minor : '').
58
                ($this->userAgent->patch !== null ? '.'.$this->userAgent->patch : '');
59
    }
60
}
61