Useragent   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 95
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A agent() 0 4 1
A __construct() 0 5 1
A for() 0 6 1
A parser() 0 6 1
A parse() 0 5 1
A result() 0 6 1
B resultResolver() 0 47 6
1
<?php
2
3
namespace Gemz\Useragent;
4
5
use DeviceDetector\DeviceDetector;
6
7
class Useragent
8
{
9
    /** DeviceDetector */
10
    protected $parser;
11
12
    /** @var string  */
13
    protected $useragent;
14
15
    public static function agent(string $useragent)
16
    {
17
        return new static($useragent);
18
    }
19
20
    public function __construct(string $useragent)
21
    {
22
        $this->useragent = $useragent;
23
        $this->parser = new DeviceDetector();
24
    }
25
26
    public function for(string $useragent): self
27
    {
28
        $this->useragent = $useragent;
29
30
        return $this;
31
    }
32
33
    public function parser(): DeviceDetector
34
    {
35
        $this->parse();
36
37
        return $this->parser;
38
    }
39
40
    protected function parse(): void
41
    {
42
        $this->parser->setUserAgent($this->useragent);
43
        $this->parser->parse();
44
    }
45
46
    public function result(): array
47
    {
48
        $this->parse();
49
50
        return $this->resultResolver();
51
    }
52
53
    protected function resultResolver(): array
54
    {
55
        $client = $this->parser->getClient();
56
        $os = $this->parser->getOs();
57
58
        return [
59
            // is this a bot?
60
            'isBot' => $this->parser->isBot(),
61
62
            // browser
63
            'browserType' => $client !== null
64
                ? ($client['type'] ?? '')
65
                : '',
66
67
            // browser engine
68
            'browserEngine' => $client !== null
69
                ? ($client['engine'] ?? '')
70
                : '',
71
72
            // browser name like Chrome, Safari, Firefox etc.
73
            'browserName' => $client !== null
74
                ? ($client['name'] ?? '')
75
                : '',
76
77
            // browser version
78
            'browserVersion' => $client !== null
79
                ? ($client['version'] ?? '')
80
                : '',
81
82
            // device name
83
            'device' => $this->parser->getDeviceName(),
84
85
            // device model if applicable
86
            'deviceModel' => $this->parser->getModel(),
87
88
            // device brand if applicable
89
            'deviceBrand' => $this->parser->getBrandName(),
90
91
            // operating system
92
            'os' => $os !== null
93
                ? ($os['name'] ?? '')
94
                : '',
95
96
            // is mobile?
97
            'isMobile' => $this->parser->isMobile(),
98
        ];
99
    }
100
101
}
102