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 ( 804c75...489def )
by Martin
02:49
created

Wurfl::hydrateBrowser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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