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 ( ca3a96...4daed7 )
by Martin
03:06
created

Wurfl   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 98.68%

Importance

Changes 9
Bugs 0 Features 2
Metric Value
wmc 26
c 9
b 0
f 2
lcom 1
cbo 11
dl 0
loc 202
ccs 75
cts 76
cp 0.9868
rs 10

12 Methods

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