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.

Zsxsoft::hasResult()   B
last analyzed

Complexity

Conditions 9
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 7.756
cc 9
eloc 10
nc 5
nop 3
crap 9
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use UserAgent;
5
use UserAgentParser\Exception\NoResultFoundException;
6
use UserAgentParser\Exception\PackageNotLoadedException;
7
use UserAgentParser\Model;
8
9
/**
10
 * Abstraction for zsxsoft/php-useragent
11
 *
12
 * @author Martin Keckeis <[email protected]>
13
 * @license MIT
14
 * @see https://github.com/zsxsoft/php-useragent
15
 */
16
class Zsxsoft extends AbstractProvider
17
{
18
    /**
19
     * Name of the provider
20
     *
21
     * @var string
22
     */
23
    protected $name = 'Zsxsoft';
24
25
    /**
26
     * Homepage of the provider
27
     *
28
     * @var string
29
     */
30
    protected $homepage = 'https://github.com/zsxsoft/php-useragent';
31
32
    /**
33
     * Composer package name
34
     *
35
     * @var string
36
     */
37
    protected $packageName = 'zsxsoft/php-useragent';
38
39
    protected $detectionCapabilities = [
40
41
        'browser' => [
42
            'name'    => true,
43
            'version' => true,
44
        ],
45
46
        'renderingEngine' => [
47
            'name'    => false,
48
            'version' => false,
49
        ],
50
51
        'operatingSystem' => [
52
            'name'    => true,
53
            'version' => true,
54
        ],
55
56
        'device' => [
57
            'model'    => true,
58
            'brand'    => true,
59
            'type'     => false,
60
            'isMobile' => false,
61
            'isTouch'  => false,
62
        ],
63
64
        'bot' => [
65
            'isBot' => false,
66
            'name'  => false,
67
            'type'  => false,
68
        ],
69
    ];
70
71
    protected $defaultValues = [
72
73
        'general' => [
74
            '/^Unknown$/i',
75
        ],
76
77
        'browser' => [
78
            'name' => [
79
                '/^Mozilla Compatible$/i',
80
            ],
81
        ],
82
83
        'device' => [
84
            'model' => [
85
                '/^Browser$/i',
86
                '/^Android$/i',
87
            ],
88
        ],
89
    ];
90
91
    private $parser;
92
93
    /**
94
     *
95
     * @param  UserAgent                 $parser
96
     * @throws PackageNotLoadedException
97
     */
98 16
    public function __construct(UserAgent $parser = null)
99
    {
100 16
        if ($parser === null) {
101 8
            $this->checkIfInstalled();
102
        }
103
104 16
        $this->parser = $parser;
105 16
    }
106
107
    /**
108
     *
109
     * @return UserAgent
110
     */
111 9
    public function getParser()
112
    {
113 9
        if ($this->parser !== null) {
114 9
            return $this->parser;
115
        }
116
117 1
        $this->parser = new UserAgent();
118
119 1
        return $this->parser;
120
    }
121
122
    /**
123
     *
124
     * @param array $browser
125
     * @param array $os
126
     * @param array $device
127
     *
128
     * @return bool
129
     */
130 8
    private function hasResult(array $browser, array $os, array $device)
131
    {
132 8
        if (isset($browser['name']) && $this->isRealResult($browser['name'], 'browser', 'name')) {
133 2
            return true;
134
        }
135
136 6
        if (isset($os['name']) && $this->isRealResult($os['name'])) {
137 1
            return true;
138
        }
139
140 5
        if (isset($device['brand']) && $this->isRealResult($device['brand'])) {
141 1
            return true;
142
        }
143
144 4
        if (isset($device['model']) && $this->isRealResult($device['model'], 'device', 'model')) {
145 1
            return true;
146
        }
147
148 3
        return false;
149
    }
150
151
    /**
152
     *
153
     * @param Model\Browser $browser
154
     * @param array         $browserRaw
155
     */
156 5
    private function hydrateBrowser(Model\Browser $browser, array $browserRaw)
157
    {
158 5
        if (isset($browserRaw['name'])) {
159 2
            $browser->setName($this->getRealResult($browserRaw['name'], 'browser', 'name'));
160
        }
161
162 5
        if (isset($browserRaw['version'])) {
163 2
            $browser->getVersion()->setComplete($this->getRealResult($browserRaw['version']));
164
        }
165 5
    }
166
167
    /**
168
     *
169
     * @param Model\OperatingSystem $os
170
     * @param array                 $osRaw
171
     */
172 5
    private function hydrateOperatingSystem(Model\OperatingSystem $os, array $osRaw)
173
    {
174 5
        if (isset($osRaw['name'])) {
175 1
            $os->setName($this->getRealResult($osRaw['name']));
176
        }
177
178 5
        if (isset($osRaw['version'])) {
179 1
            $os->getVersion()->setComplete($this->getRealResult($osRaw['version']));
180
        }
181 5
    }
182
183
    /**
184
     *
185
     * @param Model\Device $device
186
     * @param array        $deviceRaw
187
     */
188 5
    private function hydrateDevice(Model\Device $device, array $deviceRaw)
189
    {
190 5
        if (isset($deviceRaw['model'])) {
191 2
            $device->setModel($this->getRealResult($deviceRaw['model'], 'device', 'model'));
192
        }
193
194 5
        if (isset($deviceRaw['brand'])) {
195 1
            $device->setBrand($this->getRealResult($deviceRaw['brand']));
196
        }
197 5
    }
198
199 8
    public function parse($userAgent, array $headers = [])
200
    {
201 8
        $parser = $this->getParser();
202 8
        $parser->analyze($userAgent);
203
204 8
        $browser  = $parser->browser;
205 8
        $os       = $parser->os;
206 8
        $device   = $parser->device;
207 8
        $platform = $parser->platform;
208
209
        /*
210
         * No result found?
211
         */
212 8
        if ($this->hasResult($browser, $os, $device) !== true) {
213 3
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
214
        }
215
216
        /*
217
         * Hydrate the model
218
         */
219 5
        $result = new Model\UserAgent($this->getName(), $this->getVersion());
220 5
        $result->setProviderResultRaw([
221 5
            'browser'  => $browser,
222 5
            'os'       => $os,
223 5
            'device'   => $device,
224 5
            'platform' => $platform,
225
        ]);
226
227
        /*
228
         * hydrate the result
229
         */
230 5
        $this->hydrateBrowser($result->getBrowser(), $browser);
231 5
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $os);
232 5
        $this->hydrateDevice($result->getDevice(), $device);
233
234 5
        return $result;
235
    }
236
}
237