GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 0994ac...8bf298 )
by Martin
04:01
created

UAParser::isBot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use UAParser\Parser;
5
use UserAgentParser\Exception;
6
use UserAgentParser\Model;
7
8
class UAParser extends AbstractProvider
9
{
10
    protected $defaultValues = [
11
        'Other',
12
    ];
13
14
    private $parser;
15
16 1
    public function getName()
17
    {
18 1
        return 'UAParser';
19
    }
20
21 2
    public function getComposerPackageName()
22
    {
23 2
        return 'ua-parser/uap-php';
24
    }
25
26
    /**
27
     *
28
     * @param Parser $parser
29
     */
30 6
    public function setParser(Parser $parser = null)
31
    {
32 6
        $this->parser = $parser;
33 6
    }
34
35
    /**
36
     *
37
     * @return Parser
38
     */
39 6
    public function getParser()
40
    {
41 6
        if ($this->parser !== null) {
42 6
            return $this->parser;
43
        }
44
45 1
        $this->parser = Parser::create();
46
47 1
        return $this->parser;
48
    }
49
50
    /**
51
     *
52
     * @param \UAParser\Result\Client $resultRaw
53
     *
54
     * @return bool
55
     */
56 5
    private function hasResult(\UAParser\Result\Client $resultRaw)
57
    {
58 5
        if ($this->isRealResult($resultRaw->ua->family)) {
59 2
            return true;
60
        }
61
62 3
        if ($this->isRealResult($resultRaw->os->family)) {
63 1
            return true;
64
        }
65
66 2
        if ($this->isRealResult($resultRaw->device->model)) {
67 1
            return true;
68
        }
69
70 1
        return false;
71
    }
72
73 3
    private function getDeviceModelDefaultValues()
74
    {
75
        return [
76 3
            'Feature Phone',
77 3
            'iOS-Device',
78 3
            'Smartphone',
79 3
        ];
80
    }
81
82 3
    private function getDeviceBrandDefaultValues()
83
    {
84
        return [
85 3
            'Generic',
86 3
            'Generic_Android',
87 3
            'Generic_Inettv',
88 3
        ];
89
    }
90
91
    /**
92
     *
93
     * @param \UAParser\Result\Client $resultRaw
94
     *
95
     * @return bool
96
     */
97 4
    private function isBot(\UAParser\Result\Client $resultRaw)
98
    {
99 4
        if ($resultRaw->device->family === 'Spider') {
100 1
            return true;
101
        }
102
103 3
        return false;
104
    }
105
106 5
    public function parse($userAgent, array $headers = [])
107
    {
108 5
        $parser = $this->getParser();
109
110
        /* @var $resultRaw \UAParser\Result\Client */
111 5
        $resultRaw = $parser->parse($userAgent);
112
113
        /*
114
         * No result found?
115
         */
116 5
        if ($this->hasResult($resultRaw) !== true) {
117 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
118
        }
119
120
        /*
121
         * Hydrate the model
122
         */
123 4
        $result = new Model\UserAgent();
124 4
        $result->setProviderResultRaw($resultRaw);
125
126
        /*
127
         * Bot detection
128
         */
129 4
        if ($this->isBot($resultRaw) === true) {
130 1
            $bot = $result->getBot();
131 1
            $bot->setIsBot(true);
132
133 1
            if ($this->isRealResult($resultRaw->ua->family) === true) {
134 1
                $bot->setName($resultRaw->ua->family);
135
            }
136
137 1
            return $result;
138
        }
139
140
        /*
141
         * Browser
142
         */
143 3
        $browser = $result->getBrowser();
144
145 3
        if ($this->isRealResult($resultRaw->ua->family) === true) {
146 1
            $browser->setName($resultRaw->ua->family);
147
        }
148
149 3
        if ($this->isRealResult($resultRaw->ua->major) === true) {
150 1
            $browser->getVersion()->setMajor($resultRaw->ua->major);
151
        }
152
153 3
        if ($this->isRealResult($resultRaw->ua->minor) === true) {
154 1
            $browser->getVersion()->setMinor($resultRaw->ua->minor);
155
        }
156
157 3
        if ($this->isRealResult($resultRaw->ua->patch) === true) {
158 1
            $browser->getVersion()->setPatch($resultRaw->ua->patch);
159
        }
160
161
        /*
162
         * renderingEngine - is currently not possible!
163
         */
164
165
        /*
166
         * operatingSystem
167
         */
168 3
        $operatingSystem = $result->getOperatingSystem();
169
170 3
        if ($this->isRealResult($resultRaw->os->family) === true) {
171 1
            $operatingSystem->setName($resultRaw->os->family);
172
        }
173
174 3
        if ($this->isRealResult($resultRaw->os->major) === true) {
175 1
            $operatingSystem->getVersion()->setMajor($resultRaw->os->major);
176
        }
177
178 3
        if ($this->isRealResult($resultRaw->os->minor) === true) {
179 1
            $operatingSystem->getVersion()->setMinor($resultRaw->os->minor);
180
        }
181
182 3
        if ($this->isRealResult($resultRaw->os->patch) === true) {
183 1
            $operatingSystem->getVersion()->setPatch($resultRaw->os->patch);
184
        }
185
186
        /*
187
         * device
188
         */
189 3
        $device = $result->getDevice();
190
191 3
        if ($this->isRealResult($resultRaw->device->model, $this->getDeviceModelDefaultValues()) === true) {
192 1
            $device->setModel($resultRaw->device->model);
193
        }
194
195 3
        if ($this->isRealResult($resultRaw->device->brand, $this->getDeviceBrandDefaultValues()) === true) {
196 1
            $device->setBrand($resultRaw->device->brand);
197
        }
198
199 3
        return $result;
200
    }
201
}
202