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 (#44)
by Martin
05:39
created

Wurfl::getUpdateDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0117

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
rs 9.4286
cc 2
eloc 7
nc 2
nop 0
crap 2.0117
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use UserAgentParser\Exception;
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
        'Generic',
66
    ];
67
68
    /**
69
     *
70
     * @var WurflManager
71
     */
72
    private $parser;
73
74
    /**
75
     *
76
     * @param WurflManager $parser
77
     */
78 14
    public function __construct(WurflManager $parser)
79
    {
80 14
        $this->parser = $parser;
81 14
    }
82
83 2
    public function getVersion()
84
    {
85 2
        $version      = $this->getParser()->getWurflInfo()->version;
86 2
        $versionParts = explode(' - ', $version);
87
88 2
        if (count($versionParts) === 2) {
89 1
            $versionPart = $versionParts[0];
90 1
            $versionPart = str_replace('for API', '', $versionPart);
91 1
            $versionPart = str_replace(', db.scientiamobile.com', '', $versionPart);
92
93 1
            return trim($versionPart);
94
        }
95
96 1
        return;
97
    }
98
99 1
    public function getUpdateDate()
100
    {
101
        // 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...
102 1
        $lastUpdated  = $this->getParser()->getWurflInfo()->lastUpdated;
103
104 1
        if ($lastUpdated == '') {
105
            return;
106
        }
107
108 1
        $date = \DateTime::createFromFormat('Y-m-d H:i:s O', $lastUpdated);
109 1
        $date->setTimezone(new \DateTimeZone('UTC'));
110
111 1
        return $date;
112
    }
113
114
    /**
115
     *
116
     * @return WurflManager
117
     */
118 10
    public function getParser()
119
    {
120 10
        return $this->parser;
121
    }
122
123
    /**
124
     *
125
     * @param  CustomDevice $device
126
     * @return boolean
127
     */
128 6
    private function hasResult(CustomDevice $device)
129
    {
130 6
        if ($device->id !== null && $device->id != '' && $device->id !== 'generic') {
131 5
            return true;
132
        }
133
134 1
        return false;
135
    }
136
137
    /**
138
     * @param mixed $value
139
     *
140
     * @return bool
141
     */
142 4
    private function isRealDeviceModel($value)
143
    {
144 4
        if ($this->isRealResult($value) !== true) {
145 2
            return false;
146
        }
147
148 2
        $value = (string) $value;
149
150
        $defaultValues = [
151 2
            'Android',
152 2
            'Firefox',
153 2
            'unrecognized',
154 2
            'Windows Mobile',
155 2
            'Windows Phone',
156 2
            'Windows RT',
157 2
        ];
158
159 2
        foreach ($defaultValues as $defaultValue) {
160 2
            if (substr($value, 0, strlen($defaultValue)) == $defaultValue) {
161 1
                return false;
162
            }
163 1
        }
164
165 1
        return true;
166
    }
167
168
    /**
169
     *
170
     * @param Model\Browser $browser
171
     * @param CustomDevice  $deviceRaw
172
     */
173 4
    private function hydrateBrowser(Model\Browser $browser, CustomDevice $deviceRaw)
174
    {
175 4
        $browser->setName($deviceRaw->getVirtualCapability('advertised_browser'));
176 4
        $browser->getVersion()->setComplete($deviceRaw->getVirtualCapability('advertised_browser_version'));
177 4
    }
178
179
    /**
180
     *
181
     * @param Model\OperatingSystem $os
182
     * @param CustomDevice          $deviceRaw
183
     */
184 4
    private function hydrateOperatingSystem(Model\OperatingSystem $os, CustomDevice $deviceRaw)
185
    {
186 4
        $os->setName($deviceRaw->getVirtualCapability('advertised_device_os'));
187 4
        $os->getVersion()->setComplete($deviceRaw->getVirtualCapability('advertised_device_os_version'));
188 4
    }
189
190
    /**
191
     *
192
     * @param Model\UserAgent $device
193
     * @param CustomDevice    $deviceRaw
194
     */
195 4
    private function hydrateDevice(Model\Device $device, CustomDevice $deviceRaw)
196
    {
197 4
        if ($deviceRaw->getVirtualCapability('is_full_desktop') !== 'true') {
198 4
            if ($this->isRealDeviceModel($deviceRaw->getCapability('model_name')) === true) {
199 1
                $device->setModel($deviceRaw->getCapability('model_name'));
200
            }
201
202 4
            if ($this->isRealResult($deviceRaw->getCapability('brand_name')) === true) {
203 2
                $device->setBrand($deviceRaw->getCapability('brand_name'));
204
            }
205
206 4
            if ($deviceRaw->getVirtualCapability('is_mobile') === 'true') {
207 2
                $device->setIsMobile(true);
208
            }
209
210 4
            if ($deviceRaw->getVirtualCapability('is_touchscreen') === 'true') {
211 2
                $device->setIsTouch(true);
212
            }
213
        }
214
215
        // @see the list of all types http://web.wurfl.io/
216 4
        $device->setType($deviceRaw->getVirtualCapability('form_factor'));
217 4
    }
218
219 6
    public function parse($userAgent, array $headers = [])
220
    {
221 6
        $parser = $this->getParser();
222
223 6
        $deviceRaw = $parser->getDeviceForUserAgent($userAgent);
224
225
        /*
226
         * No result found?
227
         */
228 6
        if ($this->hasResult($deviceRaw) !== true) {
229 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
230
        }
231
232
        /*
233
         * Hydrate the model
234
         */
235 5
        $result = new Model\UserAgent();
236 5
        $result->setProviderResultRaw([
237 5
            'virtual' => $deviceRaw->getAllVirtualCapabilities(),
238 5
            'all'     => $deviceRaw->getAllCapabilities(),
239 5
        ]);
240
241
        /*
242
         * Bot detection
243
         */
244 5
        if ($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 4
        $this->hydrateBrowser($result->getBrowser(), $deviceRaw);
257
        // renderingEngine not available
258 4
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $deviceRaw);
259 4
        $this->hydrateDevice($result->getDevice(), $deviceRaw);
260
261 4
        return $result;
262
    }
263
}
264