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 ( 4daed7...47dd2c )
by Martin
02:40
created

Wurfl::getName()   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;
5
use UserAgentParser\Model;
6
use Wurfl\CustomDevice;
7
use Wurfl\Manager as WurflManager;
8
9
class Wurfl extends AbstractProvider
10
{
11
    protected $detectionCapabilities = [
12
13
        'browser' => [
14
            'name'    => true,
15
            'version' => true,
16
        ],
17
18
        'renderingEngine' => [
19
            'name'    => false,
20
            'version' => false,
21
        ],
22
23
        'operatingSystem' => [
24
            'name'    => true,
25
            'version' => true,
26
        ],
27
28
        'device' => [
29
            'model'    => true,
30
            'brand'    => true,
31
            'type'     => true,
32
            'isMobile' => true,
33
            'isTouch'  => true,
34
        ],
35
36
        'bot' => [
37
            'isBot' => true,
38
            'name'  => false,
39
            'type'  => false,
40
        ],
41
    ];
42
43
    protected $defaultValues = [
44
        'Generic',
45
    ];
46
47
    /**
48
     *
49
     * @var WurflManager
50
     */
51
    private $parser;
52
53
    /**
54
     *
55
     * @param WurflManager $parser
56
     */
57 12
    public function __construct(WurflManager $parser)
58
    {
59 12
        $this->setParser($parser);
60 12
    }
61
62 1
    public function getName()
63
    {
64 1
        return 'Wurfl';
65
    }
66
67 1
    public function getComposerPackageName()
68
    {
69 1
        return 'mimmi20/wurfl';
70
    }
71
72 2
    public function getVersion()
73
    {
74 2
        $version      = $this->getParser()->getWurflInfo()->version;
75 2
        $versionParts = explode(' - ', $version);
76
77 2
        if (count($versionParts) === 2) {
78 1
            return trim($versionParts[1]);
79
        }
80
81 1
        return;
82
    }
83
84
    /**
85
     *
86
     * @param WurflManager $parser
87
     */
88 12
    public function setParser(WurflManager $parser)
89
    {
90 12
        $this->parser = $parser;
91 12
    }
92
93
    /**
94
     *
95
     * @return WurflManager
96
     */
97 9
    public function getParser()
98
    {
99 9
        return $this->parser;
100
    }
101
102
    /**
103
     *
104
     * @param  CustomDevice $device
105
     * @return boolean
106
     */
107 6
    private function hasResult(CustomDevice $device)
108
    {
109 6
        if ($device->id !== null && $device->id != '' && $device->id !== 'generic') {
110 5
            return true;
111
        }
112
113 1
        return false;
114
    }
115
116
    /**
117
     * @param mixed $value
118
     *
119
     * @return bool
120
     */
121 4
    private function isRealDeviceModel($value)
122
    {
123 4
        if ($this->isRealResult($value) !== true) {
124 2
            return false;
125
        }
126
127 2
        $value = (string) $value;
128
129
        $defaultValues = [
130 2
            'Android',
131 2
            'Firefox',
132 2
            'unrecognized',
133 2
            'Windows Mobile',
134 2
            'Windows Phone',
135 2
            'Windows RT',
136 2
        ];
137
138 2
        foreach ($defaultValues as $defaultValue) {
139 2
            if (substr($value, 0, strlen($defaultValue)) == $defaultValue) {
140 1
                return false;
141
            }
142 1
        }
143
144 1
        return true;
145
    }
146
147
    /**
148
     *
149
     * @param Model\Browser $browser
150
     * @param CustomDevice  $deviceRaw
151
     */
152 4
    private function hydrateBrowser(Model\Browser $browser, CustomDevice $deviceRaw)
153
    {
154 4
        $browser->setName($deviceRaw->getVirtualCapability('advertised_browser'));
155 4
        $browser->getVersion()->setComplete($deviceRaw->getVirtualCapability('advertised_browser_version'));
156 4
    }
157
158
    /**
159
     *
160
     * @param Model\OperatingSystem $os
161
     * @param CustomDevice          $deviceRaw
162
     */
163 4
    private function hydrateOperatingSystem(Model\OperatingSystem $os, CustomDevice $deviceRaw)
164
    {
165 4
        $os->setName($deviceRaw->getVirtualCapability('advertised_device_os'));
166 4
        $os->getVersion()->setComplete($deviceRaw->getVirtualCapability('advertised_device_os_version'));
167 4
    }
168
169
    /**
170
     *
171
     * @param Model\UserAgent $device
172
     * @param CustomDevice    $deviceRaw
173
     */
174 4
    private function hydrateDevice(Model\Device $device, CustomDevice $deviceRaw)
175
    {
176 4
        if ($deviceRaw->getVirtualCapability('is_full_desktop') !== 'true') {
177 4
            if ($this->isRealDeviceModel($deviceRaw->getCapability('model_name')) === true) {
178 1
                $device->setModel($deviceRaw->getCapability('model_name'));
179
            }
180
181 4
            if ($this->isRealResult($deviceRaw->getCapability('brand_name')) === true) {
182 2
                $device->setBrand($deviceRaw->getCapability('brand_name'));
183
            }
184
185 4
            if ($deviceRaw->getVirtualCapability('is_mobile') === 'true') {
186 2
                $device->setIsMobile(true);
187
            }
188
189 4
            if ($deviceRaw->getVirtualCapability('is_touchscreen') === 'true') {
190 2
                $device->setIsTouch(true);
191
            }
192
        }
193
194
        // @see the list of all types http://web.wurfl.io/
195 4
        $device->setType($deviceRaw->getVirtualCapability('form_factor'));
196 4
    }
197
198 6
    public function parse($userAgent, array $headers = [])
199
    {
200 6
        $parser = $this->getParser();
201
202 6
        $deviceRaw = $parser->getDeviceForUserAgent($userAgent);
203
204
        /*
205
         * No result found?
206
         */
207 6
        if ($this->hasResult($deviceRaw) !== true) {
208 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
209
        }
210
211
        /*
212
         * Hydrate the model
213
         */
214 5
        $result = new Model\UserAgent();
215 5
        $result->setProviderResultRaw([
216 5
            'virtual' => $deviceRaw->getAllVirtualCapabilities(),
217 5
            'all'     => $deviceRaw->getAllCapabilities(),
218 5
        ]);
219
220
        /*
221
         * Bot detection
222
         */
223 5
        if ($deviceRaw->getVirtualCapability('is_robot') === 'true') {
224 1
            $bot = $result->getBot();
225 1
            $bot->setIsBot(true);
226
227
            // brand_name seems to be always google, so dont use it
228
229 1
            return $result;
230
        }
231
232
        /*
233
         * hydrate the result
234
         */
235 4
        $this->hydrateBrowser($result->getBrowser(), $deviceRaw);
236
        // renderingEngine not available
237 4
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $deviceRaw);
238 4
        $this->hydrateDevice($result->getDevice(), $deviceRaw);
239
240 4
        return $result;
241
    }
242
}
243