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 ( 57cbc1...b45bb0 )
by Martin
10s
created

Wurfl::getParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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