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 ( 800c59...cb857f )
by Martin
09:25 queued 04:49
created

Wurfl::hydrateDevice()   D

Complexity

Conditions 9
Paths 17

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 9

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 25
ccs 12
cts 12
cp 1
rs 4.909
cc 9
eloc 12
nc 17
nop 2
crap 9
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use UserAgentParser\Exception\NoResultFoundException;
5
use UserAgentParser\Model;
6
use Wurfl\CustomDevice;
7
use Wurfl\Manager as WurflManager;
8
9
class Wurfl extends AbstractProvider
10
{
11
    /**
12
     * Name of the provider
13
     *
14
     * @var string
15
     */
16
    protected $name = 'Wurfl';
17
18
    /**
19
     * Homepage of the provider
20
     *
21
     * @var string
22
     */
23
    protected $homepage = 'https://github.com/mimmi20/Wurfl';
24
25
    /**
26
     * Composer package name
27
     *
28
     * @var string
29
     */
30
    protected $packageName = 'mimmi20/wurfl';
31
32
    protected $detectionCapabilities = [
33
34
        'browser' => [
35
            'name'    => true,
36
            'version' => true,
37
        ],
38
39
        'renderingEngine' => [
40
            'name'    => false,
41
            'version' => false,
42
        ],
43
44
        'operatingSystem' => [
45
            'name'    => true,
46
            'version' => true,
47
        ],
48
49
        'device' => [
50
            'model'    => true,
51
            'brand'    => true,
52
            'type'     => true,
53
            'isMobile' => true,
54
            'isTouch'  => true,
55
        ],
56
57
        'bot' => [
58
            'isBot' => true,
59
            'name'  => false,
60
            'type'  => false,
61
        ],
62
    ];
63
64
    protected $defaultValues = [
65
66
        'general' => [],
67
68
        'operatingSystem' => [
69
            'name' => [
70
                '/^Unknown$/i',
71
            ],
72
        ],
73
74
        'device' => [
75
76
            'brand' => [
77
                '/^Generic$/i',
78
            ],
79
80
            'model' => [
81
                '/^Android/i',
82
                '/^SmartTV$/i',
83
                '/^Windows Phone/i',
84
                '/^Windows Mobile/i',
85
                '/^Firefox/i',
86
                '/^unrecognized/i',
87
                '/^Generic/i',
88
                '/^Disguised as Macintosh$/i',
89
                '/^Windows RT/i',
90
                '/^Tablet on Android$/i',
91
            ],
92
        ],
93
    ];
94
95
    /**
96
     *
97
     * @var WurflManager
98
     */
99
    private $parser;
100
101
    /**
102
     *
103
     * @param WurflManager $parser
104
     */
105 28
    public function __construct(WurflManager $parser)
106
    {
107 28
        $this->parser = $parser;
108 28
    }
109
110 2
    public function getVersion()
111
    {
112 2
        $version      = $this->getParser()->getWurflInfo()->version;
113 2
        $versionParts = explode(' - ', $version);
114
115 2
        if (count($versionParts) > 1) {
116 1
            $versionPart = $versionParts[0];
117 1
            $versionPart = str_replace('API', '', $versionPart);
118
119 1
            return trim($versionPart);
120
        }
121
122 1
        return;
123
    }
124
125 2
    public function getUpdateDate()
126
    {
127
        // 2015-10-16 11:09:44 -0400
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
128 2
        $lastUpdated = $this->getParser()->getWurflInfo()->lastUpdated;
129
130 2
        if ($lastUpdated == '') {
131 1
            return;
132
        }
133
134 1
        $date = \DateTime::createFromFormat('Y-m-d H:i:s O', $lastUpdated);
135 1
        $date->setTimezone(new \DateTimeZone('UTC'));
136
137 1
        return $date;
138
    }
139
140
    /**
141
     *
142
     * @return WurflManager
143
     */
144 12
    public function getParser()
145
    {
146 12
        return $this->parser;
147
    }
148
149
    /**
150
     *
151
     * @param  CustomDevice $device
152
     * @return boolean
153
     */
154 7
    private function hasResult(CustomDevice $device)
155
    {
156 7
        if ($device->id !== null && $device->id != '' && $device->id !== 'generic') {
157 6
            return true;
158
        }
159
160 1
        return false;
161
    }
162
163
    /**
164
     *
165
     * @param Model\Browser $browser
166
     * @param CustomDevice  $deviceRaw
167
     */
168 5
    private function hydrateBrowser(Model\Browser $browser, CustomDevice $deviceRaw)
169
    {
170 5
        $browser->setName($deviceRaw->getVirtualCapability('advertised_browser'));
171 5
        $browser->getVersion()->setComplete($deviceRaw->getVirtualCapability('advertised_browser_version'));
172 5
    }
173
174
    /**
175
     *
176
     * @param Model\OperatingSystem $os
177
     * @param CustomDevice          $deviceRaw
178
     */
179 5
    private function hydrateOperatingSystem(Model\OperatingSystem $os, CustomDevice $deviceRaw)
180
    {
181 5
        if ($this->isRealResult($deviceRaw->getVirtualCapability('advertised_device_os'), 'operatingSystem', 'name')) {
182 1
            $os->setName($deviceRaw->getVirtualCapability('advertised_device_os'));
183
        }
184
185 5
        $os->getVersion()->setComplete($deviceRaw->getVirtualCapability('advertised_device_os_version'));
186 5
    }
187
188
    /**
189
     *
190
     * @param Model\UserAgent $device
191
     * @param CustomDevice    $deviceRaw
192
     */
193 5
    private function hydrateDevice(Model\Device $device, CustomDevice $deviceRaw)
194
    {
195
        // @see the list of all types http://web.wurfl.io/
196 5
        $device->setType($deviceRaw->getVirtualCapability('form_factor'));
197
198 5
        if ($deviceRaw->getVirtualCapability('is_full_desktop') === 'true' || $deviceRaw->getVirtualCapability('is_full_desktop') === true) {
199
            return;
200
        }
201
202 5
        if ($this->isRealResult($deviceRaw->getCapability('model_name'), 'device', 'model') === true) {
203 1
            $device->setModel($deviceRaw->getCapability('model_name'));
204
        }
205
206 5
        if ($this->isRealResult($deviceRaw->getCapability('brand_name'), 'device', 'brand') === true) {
207 1
            $device->setBrand($deviceRaw->getCapability('brand_name'));
208
        }
209
210 5
        if ($deviceRaw->getVirtualCapability('is_mobile') === 'true' || $deviceRaw->getVirtualCapability('is_mobile') === true) {
211 1
            $device->setIsMobile(true);
212
        }
213
214 5
        if ($deviceRaw->getVirtualCapability('is_touchscreen') === 'true' || $deviceRaw->getVirtualCapability('is_touchscreen') === true) {
215 1
            $device->setIsTouch(true);
216
        }
217 5
    }
218
219 7
    public function parse($userAgent, array $headers = [])
220
    {
221 7
        $parser = $this->getParser();
222
223 7
        $deviceRaw = $parser->getDeviceForUserAgent($userAgent);
224
225
        /*
226
         * No result found?
227
         */
228 7
        if ($this->hasResult($deviceRaw) !== true) {
229 1
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
230
        }
231
232
        /*
233
         * Hydrate the model
234
         */
235 6
        $result = new Model\UserAgent();
236 6
        $result->setProviderResultRaw([
237 6
            'virtual' => $deviceRaw->getAllVirtualCapabilities(),
238 6
            'all'     => $deviceRaw->getAllCapabilities(),
239
        ]);
240
241
        /*
242
         * Bot detection
243
         */
244 6
        if ($deviceRaw->getVirtualCapability('is_robot') === 'true' || $deviceRaw->getVirtualCapability('is_robot') === true) {
245 1
            $bot = $result->getBot();
246 1
            $bot->setIsBot(true);
247
248
            // brand_name seems to be always google, so dont use it
249
250 1
            return $result;
251
        }
252
253
        /*
254
         * hydrate the result
255
         */
256 5
        $this->hydrateBrowser($result->getBrowser(), $deviceRaw);
257
        // renderingEngine not available
258 5
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $deviceRaw);
259 5
        $this->hydrateDevice($result->getDevice(), $deviceRaw);
260
261 5
        return $result;
262
    }
263
}
264