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
Pull Request — master (#96)
by Martin
05:46
created

HandsetDetection::hydrateDevice()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 8.8571
cc 5
eloc 4
nc 2
nop 2
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use HandsetDetection as Parser;
5
use UserAgentParser\Exception\NoResultFoundException;
6
use UserAgentParser\Model;
7
8
/**
9
 * Abstraction for ua-parser/uap-php
10
 *
11
 * @author Martin Keckeis <[email protected]>
12
 * @license MIT
13
 * @see https://github.com/HandsetDetection/php-apikit
14
 */
15
class HandsetDetection extends AbstractProvider
16
{
17
    /**
18
     * Name of the provider
19
     *
20
     * @var string
21
     */
22
    protected $name = 'HandsetDetection';
23
24
    /**
25
     * Homepage of the provider
26
     *
27
     * @var string
28
     */
29
    protected $homepage = 'https://github.com/HandsetDetection/php-apikit';
30
31
    /**
32
     * Composer package name
33
     *
34
     * @var string
35
     */
36
    protected $packageName = 'handsetdetection/php-apikit';
37
38
    protected $detectionCapabilities = [
39
40
        'browser' => [
41
            'name'    => true,
42
            'version' => true,
43
        ],
44
45
        'renderingEngine' => [
46
            'name'    => false,
47
            'version' => false,
48
        ],
49
50
        'operatingSystem' => [
51
            'name'    => true,
52
            'version' => true,
53
        ],
54
55
        'device' => [
56
            'model'    => true,
57
            'brand'    => true,
58
            'type'     => false,
59
            'isMobile' => false,
60
            'isTouch'  => false,
61
        ],
62
63
        'bot' => [
64
            'isBot' => false,
65
            'name'  => false,
66
            'type'  => false,
67
        ],
68
    ];
69
70
    protected $defaultValues = [
71
72
        'general' => [
73
            '/^generic$/i',
74
        ],
75
76
        'device' => [
77
            'model' => [
78
                '/analyzer/i',
79
                '/bot/i',
80
                '/crawler/i',
81
                '/library/i',
82
                '/spider/i',
83
            ],
84
        ],
85
    ];
86
87
    /**
88
     *
89
     * @var Parser\HD4
90
     */
91
    private $parser;
92
93
    /**
94
     *
95
     * @param Parser\HD4 $parser
96
     */
97
    public function __construct(Parser\HD4 $parser)
98
    {
99
        $this->parser = $parser;
100
    }
101
102
    /**
103
     *
104
     * @return Parser\HD4
105
     */
106
    public function getParser()
107
    {
108
        return $this->parser;
109
    }
110
111
    /**
112
     *
113
     * @param array $resultRaw
114
     *
115
     * @return bool
116
     */
117
    private function hasResult(array $resultRaw)
118
    {
119
        if (isset($resultRaw['general_browser']) && $this->isRealResult($resultRaw['general_browser'])) {
120
            return true;
121
        }
122
123
        if (isset($resultRaw['general_platform']) && $this->isRealResult($resultRaw['general_platform'])) {
124
            return true;
125
        }
126
127
        if (isset($resultRaw['general_model']) && $this->isRealResult($resultRaw['general_model'], 'device', 'model') && $this->isRealResult($resultRaw['general_vendor'], 'device', 'brand')) {
128
            return true;
129
        }
130
131
        return false;
132
    }
133
134
    /**
135
     *
136
     * @param Model\Browser $browser
137
     * @param array         $resultRaw
138
     */
139
    private function hydrateBrowser(Model\Browser $browser, array $resultRaw)
140
    {
141
        if (isset($resultRaw['general_browser'])) {
142
            $browser->setName($this->getRealResult($resultRaw['general_browser']));
143
        }
144
        if (isset($resultRaw['general_browser_version'])) {
145
            $browser->getVersion()->setComplete($this->getRealResult($resultRaw['general_browser_version']));
146
        }
147
    }
148
149
    /**
150
     *
151
     * @param Model\OperatingSystem $os
152
     * @param array                 $resultRaw
153
     */
154
    private function hydrateOperatingSystem(Model\OperatingSystem $os, array $resultRaw)
155
    {
156
        if (isset($resultRaw['general_platform'])) {
157
            $os->setName($this->getRealResult($resultRaw['general_platform']));
158
        }
159
        if (isset($resultRaw['general_platform_version'])) {
160
            $os->getVersion()->setComplete($this->getRealResult($resultRaw['general_platform_version']));
161
        }
162
    }
163
164
    /**
165
     *
166
     * @param Model\UserAgent $device
167
     * @param array           $resultRaw
168
     */
169
    private function hydrateDevice(Model\Device $device, array $resultRaw)
170
    {
171
        if (isset($resultRaw['general_model']) && $this->isRealResult($resultRaw['general_model'], 'device', 'model') && isset($resultRaw['general_vendor']) && $this->isRealResult($resultRaw['general_vendor'], 'device', 'brand')) {
172
            $device->setModel($this->getRealResult($resultRaw['general_model'], 'device', 'model'));
173
            $device->setBrand($this->getRealResult($resultRaw['general_vendor'], 'device', 'brand'));
174
        }
175
    }
176
177
    public function parse($userAgent, array $headers = [])
178
    {
179
        $headers['User-Agent'] = $userAgent;
180
181
        $parser = $this->getParser();
182
        // $config = $parser->config;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
183
184
        // $parser = new Parser\HD4($config);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
185
186
        /*
187
         * No result found?
188
         */
189
        if ($parser->deviceDetect($headers) !== true) {
190
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
191
        }
192
193
        $resultRaw = $parser->getReply();
194
195
        /*
196
         * No result found?
197
         */
198
        if (! isset($resultRaw['hd_specs']) || $this->hasResult($resultRaw['hd_specs']) !== true) {
199
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
200
        }
201
202
        /*
203
         * Hydrate the model
204
         */
205
        $result = new Model\UserAgent();
206
        $result->setProviderResultRaw($resultRaw['hd_specs']);
207
208
        /*
209
         * hydrate the result
210
         */
211
        $this->hydrateBrowser($result->getBrowser(), $resultRaw['hd_specs']);
212
        // renderingEngine not available
213
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw['hd_specs']);
214
        $this->hydrateDevice($result->getDevice(), $resultRaw['hd_specs']);
215
216
        return $result;
217
    }
218
}
219