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
02:49
created

Wurfl   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 256
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 14
Bugs 0 Features 2
Metric Value
wmc 22
c 14
b 0
f 2
lcom 1
cbo 11
dl 0
loc 256
ccs 84
cts 84
cp 1
rs 10

9 Methods

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