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 ( 192bb3...47c5f3 )
by Martin
06:04
created

Wurfl::setParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
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->parser = $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
     * @return WurflManager
87
     */
88 9
    public function getParser()
89
    {
90 9
        return $this->parser;
91
    }
92
93
    /**
94
     *
95
     * @param  CustomDevice $device
96
     * @return boolean
97
     */
98 6
    private function hasResult(CustomDevice $device)
99
    {
100 6
        if ($device->id !== null && $device->id != '' && $device->id !== 'generic') {
101 5
            return true;
102
        }
103
104 1
        return false;
105
    }
106
107
    /**
108
     * @param mixed $value
109
     *
110
     * @return bool
111
     */
112 4
    private function isRealDeviceModel($value)
113
    {
114 4
        if ($this->isRealResult($value) !== true) {
115 2
            return false;
116
        }
117
118 2
        $value = (string) $value;
119
120
        $defaultValues = [
121 2
            'Android',
122 2
            'Firefox',
123 2
            'unrecognized',
124 2
            'Windows Mobile',
125 2
            'Windows Phone',
126 2
            'Windows RT',
127 2
        ];
128
129 2
        foreach ($defaultValues as $defaultValue) {
130 2
            if (substr($value, 0, strlen($defaultValue)) == $defaultValue) {
131 1
                return false;
132
            }
133 1
        }
134
135 1
        return true;
136
    }
137
138
    /**
139
     *
140
     * @param Model\Browser $browser
141
     * @param CustomDevice  $deviceRaw
142
     */
143 4
    private function hydrateBrowser(Model\Browser $browser, CustomDevice $deviceRaw)
144
    {
145 4
        $browser->setName($deviceRaw->getVirtualCapability('advertised_browser'));
146 4
        $browser->getVersion()->setComplete($deviceRaw->getVirtualCapability('advertised_browser_version'));
147 4
    }
148
149
    /**
150
     *
151
     * @param Model\OperatingSystem $os
152
     * @param CustomDevice          $deviceRaw
153
     */
154 4
    private function hydrateOperatingSystem(Model\OperatingSystem $os, CustomDevice $deviceRaw)
155
    {
156 4
        $os->setName($deviceRaw->getVirtualCapability('advertised_device_os'));
157 4
        $os->getVersion()->setComplete($deviceRaw->getVirtualCapability('advertised_device_os_version'));
158 4
    }
159
160
    /**
161
     *
162
     * @param Model\UserAgent $device
163
     * @param CustomDevice    $deviceRaw
164
     */
165 4
    private function hydrateDevice(Model\Device $device, CustomDevice $deviceRaw)
166
    {
167 4
        if ($deviceRaw->getVirtualCapability('is_full_desktop') !== 'true') {
168 4
            if ($this->isRealDeviceModel($deviceRaw->getCapability('model_name')) === true) {
169 1
                $device->setModel($deviceRaw->getCapability('model_name'));
170
            }
171
172 4
            if ($this->isRealResult($deviceRaw->getCapability('brand_name')) === true) {
173 2
                $device->setBrand($deviceRaw->getCapability('brand_name'));
174
            }
175
176 4
            if ($deviceRaw->getVirtualCapability('is_mobile') === 'true') {
177 2
                $device->setIsMobile(true);
178
            }
179
180 4
            if ($deviceRaw->getVirtualCapability('is_touchscreen') === 'true') {
181 2
                $device->setIsTouch(true);
182
            }
183
        }
184
185
        // @see the list of all types http://web.wurfl.io/
186 4
        $device->setType($deviceRaw->getVirtualCapability('form_factor'));
187 4
    }
188
189 6
    public function parse($userAgent, array $headers = [])
190
    {
191 6
        $parser = $this->getParser();
192
193 6
        $deviceRaw = $parser->getDeviceForUserAgent($userAgent);
194
195
        /*
196
         * No result found?
197
         */
198 6
        if ($this->hasResult($deviceRaw) !== true) {
199 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
200
        }
201
202
        /*
203
         * Hydrate the model
204
         */
205 5
        $result = new Model\UserAgent();
206 5
        $result->setProviderResultRaw([
207 5
            'virtual' => $deviceRaw->getAllVirtualCapabilities(),
208 5
            'all'     => $deviceRaw->getAllCapabilities(),
209 5
        ]);
210
211
        /*
212
         * Bot detection
213
         */
214 5
        if ($deviceRaw->getVirtualCapability('is_robot') === 'true') {
215 1
            $bot = $result->getBot();
216 1
            $bot->setIsBot(true);
217
218
            // brand_name seems to be always google, so dont use it
219
220 1
            return $result;
221
        }
222
223
        /*
224
         * hydrate the result
225
         */
226 4
        $this->hydrateBrowser($result->getBrowser(), $deviceRaw);
227
        // renderingEngine not available
228 4
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $deviceRaw);
229 4
        $this->hydrateDevice($result->getDevice(), $deviceRaw);
230
231 4
        return $result;
232
    }
233
}
234