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

UserAgentParser   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 1
dl 0
loc 54
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A getOperatingSystemVersion() 0 6 3
A getUserAgent() 0 12 4
A getUserAgentVersion() 0 6 3
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