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 (#59)
by Martin
04:01
created

Wurfl::isRealDeviceModel()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
ccs 16
cts 16
cp 1
rs 8.5806
cc 4
eloc 15
nc 4
nop 1
crap 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Wurfl::getParser() 0 4 1
A Wurfl::hasResult() 0 8 4
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) === 2) {
116 1
            $versionPart = $versionParts[0];
117 1
            $versionPart = str_replace('for API', '', $versionPart);
118 1
            $versionPart = str_replace(', db.scientiamobile.com', '', $versionPart);
119
120 1
            return trim($versionPart);
121
        }
122
123 1
        return;
124
    }
125
126 2
    public function getUpdateDate()
127
    {
128
        // 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...
129 2
        $lastUpdated = $this->getParser()->getWurflInfo()->lastUpdated;
130
131 2
        if ($lastUpdated == '') {
132 1
            return;
133
        }
134
135 1
        $date = \DateTime::createFromFormat('Y-m-d H:i:s O', $lastUpdated);
136 1
        $date->setTimezone(new \DateTimeZone('UTC'));
137
138 1
        return $date;
139
    }
140
141
    /**
142
     *
143
     * @return WurflManager
144
     */
145 12
    public function getParser()
146
    {
147 12
        return $this->parser;
148
    }
149
150
    /**
151
     *
152
     * @param  CustomDevice $device
153
     * @return boolean
154
     */
155 7
    private function hasResult(CustomDevice $device)
156
    {
157 7
        if ($device->id !== null && $device->id != '' && $device->id !== 'generic') {
158 6
            return true;
159
        }
160
161 1
        return false;
162
    }
163
164
    /**
165
     *
166
     * @param Model\Browser $browser
167
     * @param CustomDevice  $deviceRaw
168
     */
169 5
    private function hydrateBrowser(Model\Browser $browser, CustomDevice $deviceRaw)
170
    {
171 5
        $browser->setName($deviceRaw->getVirtualCapability('advertised_browser'));
172 5
        $browser->getVersion()->setComplete($deviceRaw->getVirtualCapability('advertised_browser_version'));
173 5
    }
174
175
    /**
176
     *
177
     * @param Model\OperatingSystem $os
178
     * @param CustomDevice          $deviceRaw
179
     */
180 5
    private function hydrateOperatingSystem(Model\OperatingSystem $os, CustomDevice $deviceRaw)
181
    {
182 5
        if ($this->isRealResult($deviceRaw->getVirtualCapability('advertised_device_os'), 'operatingSystem', 'name')) {
183 1
            $os->setName($deviceRaw->getVirtualCapability('advertised_device_os'));
184 1
        }
185
186 5
        $os->getVersion()->setComplete($deviceRaw->getVirtualCapability('advertised_device_os_version'));
187 5
    }
188
189
    /**
190
     *
191
     * @param Model\UserAgent $device
192
     * @param CustomDevice    $deviceRaw
193
     */
194 5
    private function hydrateDevice(Model\Device $device, CustomDevice $deviceRaw)
195
    {
196
        // @see the list of all types http://web.wurfl.io/
197 5
        $device->setType($deviceRaw->getVirtualCapability('form_factor'));
198
199 5
        if ($deviceRaw->getVirtualCapability('is_full_desktop') === 'true') {
200
            return;
201
        }
202
203 5
        if ($this->isRealResult($deviceRaw->getCapability('model_name'), 'device', 'model') === true) {
204 1
            $device->setModel($deviceRaw->getCapability('model_name'));
205 1
        }
206
207 5
        if ($this->isRealResult($deviceRaw->getCapability('brand_name'), 'device', 'brand') === true) {
208 1
            $device->setBrand($deviceRaw->getCapability('brand_name'));
209 1
        }
210
211 5
        if ($deviceRaw->getVirtualCapability('is_mobile') === 'true') {
212 1
            $device->setIsMobile(true);
213 1
        }
214
215 5
        if ($deviceRaw->getVirtualCapability('is_touchscreen') === 'true') {
216 1
            $device->setIsTouch(true);
217 1
        }
218 5
    }
219
220 7
    public function parse($userAgent, array $headers = [])
221
    {
222 7
        $parser = $this->getParser();
223
224 7
        $deviceRaw = $parser->getDeviceForUserAgent($userAgent);
225
226
        /*
227
         * No result found?
228
         */
229 7
        if ($this->hasResult($deviceRaw) !== true) {
230 1
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
231
        }
232
233
        /*
234
         * Hydrate the model
235
         */
236 6
        $result = new Model\UserAgent();
237 6
        $result->setProviderResultRaw([
238 6
            'virtual' => $deviceRaw->getAllVirtualCapabilities(),
239 6
            'all'     => $deviceRaw->getAllCapabilities(),
240 6
        ]);
241
242
        /*
243
         * Bot detection
244
         */
245 6
        if ($deviceRaw->getVirtualCapability('is_robot') === 'true') {
246 1
            $bot = $result->getBot();
247 1
            $bot->setIsBot(true);
248
249
            // brand_name seems to be always google, so dont use it
250
251 1
            return $result;
252
        }
253
254
        /*
255
         * hydrate the result
256
         */
257 5
        $this->hydrateBrowser($result->getBrowser(), $deviceRaw);
258
        // renderingEngine not available
259 5
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $deviceRaw);
260 5
        $this->hydrateDevice($result->getDevice(), $deviceRaw);
261
262 5
        return $result;
263
    }
264
}
265