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 ( 800c59...cb857f )
by Martin
09:25 queued 04:49
created

AbstractBrowscap::hydrateDevice()   C

Complexity

Conditions 12
Paths 32

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 12

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 5.2987
cc 12
eloc 11
nc 32
nop 2
crap 12

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace UserAgentParser\Provider;
3
4
use BrowscapPHP\Browscap;
5
use DateTime;
6
use stdClass;
7
use UserAgentParser\Exception\InvalidArgumentException;
8
use UserAgentParser\Exception\NoResultFoundException;
9
use UserAgentParser\Model;
10
11
abstract class AbstractBrowscap extends AbstractProvider
12
{
13
    /**
14
     * Homepage of the provider
15
     *
16
     * @var string
17
     */
18
    protected $homepage = 'https://github.com/browscap/browscap-php';
19
20
    /**
21
     * Composer package name
22
     *
23
     * @var string
24
     */
25
    protected $packageName = 'browscap/browscap-php';
26
27
    protected $defaultValues = [
28
29
        'general' => [
30
            '/^unknown$/i',
31
        ],
32
33
        'browser' => [
34
            'name' => [
35
                '/^Default Browser$/i',
36
            ],
37
        ],
38
39
        'device' => [
40
            'model' => [
41
                '/^general/i',
42
                '/desktop$/i',
43
            ],
44
        ],
45
46
        'bot' => [
47
            'name' => [
48
                '/^General Crawlers/i',
49
            ],
50
        ],
51
    ];
52
53
    /**
54
     *
55
     * @var Browscap
56
     */
57
    private $parser;
58
59 27
    public function __construct(Browscap $parser, $expectedType = '')
60
    {
61 27
        $this->parser = $parser;
62
63 27
        if ($parser->getCache()->getType() === null) {
64
            throw new InvalidArgumentException('You need to warm-up the cache first to use this provider');
65
        }
66
67 27
        if ($expectedType !== $parser->getCache()->getType()) {
68 1
            throw new InvalidArgumentException('Expected the "' . $expectedType . '" data file. Instead got the "' . $parser->getCache()->getType() . '" data file');
69
        }
70 26
    }
71
72 1
    public function getVersion()
73
    {
74 1
        return $this->getParser()
75 1
            ->getCache()
76 1
            ->getVersion();
77
    }
78
79 1
    public function getUpdateDate()
80
    {
81 1
        $releaseDate = $this->getParser()
82 1
            ->getCache()
83 1
            ->getReleaseDate();
84
85 1
        return DateTime::createFromFormat('D, d M Y H:i:s O', $releaseDate);
86
    }
87
88
    /**
89
     *
90
     * @return Browscap
91
     */
92 15
    public function getParser()
93
    {
94 15
        return $this->parser;
95
    }
96
97
    /**
98
     *
99
     * @param stdClass $resultRaw
100
     *
101
     * @return bool
102
     */
103 12
    private function hasResult(stdClass $resultRaw)
104
    {
105 12
        if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser, 'browser', 'name') === true) {
106 8
            return true;
107
        }
108
109 4
        return false;
110
    }
111
112
    /**
113
     *
114
     * @param  stdClass $resultRaw
115
     * @return boolean
116
     */
117 8
    private function isBot(stdClass $resultRaw)
118
    {
119 8
        if (isset($resultRaw->crawler) && $resultRaw->crawler === true) {
120 4
            return true;
121
        }
122
123 4
        return false;
124
    }
125
126
    /**
127
     *
128
     * @param Model\Bot $bot
129
     * @param stdClass  $resultRaw
130
     */
131 4
    private function hydrateBot(Model\Bot $bot, stdClass $resultRaw)
132
    {
133 4
        $bot->setIsBot(true);
134
135 4
        if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser, 'bot', 'name') === true) {
136 3
            $bot->setName($resultRaw->browser);
137
        }
138
139 4
        if (isset($resultRaw->issyndicationreader) && $resultRaw->issyndicationreader === true) {
140 1
            $bot->setType('RSS');
141 3
        } elseif (isset($resultRaw->browser_type) && $this->isRealResult($resultRaw->browser_type) === true) {
142 2
            $bot->setType($resultRaw->browser_type);
143
        }
144 4
    }
145
146
    /**
147
     *
148
     * @param Model\Browser $browser
149
     * @param stdClass      $resultRaw
150
     */
151 4
    private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw)
152
    {
153 4
        if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser, 'browser', 'name') === true) {
154 4
            $browser->setName($resultRaw->browser);
155
        }
156
157 4
        if (isset($resultRaw->version) && $this->isRealResult($resultRaw->version) === true) {
158 2
            $browser->getVersion()->setComplete($resultRaw->version);
159
        }
160 4
    }
161
162
    /**
163
     *
164
     * @param Model\RenderingEngine $engine
165
     * @param stdClass              $resultRaw
166
     */
167 4
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw)
168
    {
169 4
        if (isset($resultRaw->renderingengine_name) && $this->isRealResult($resultRaw->renderingengine_name) === true) {
170 1
            $engine->setName($resultRaw->renderingengine_name);
171
        }
172
173 4
        if (isset($resultRaw->renderingengine_version) && $this->isRealResult($resultRaw->renderingengine_version) === true) {
174 1
            $engine->getVersion()->setComplete($resultRaw->renderingengine_version);
175
        }
176 4
    }
177
178
    /**
179
     *
180
     * @param Model\OperatingSystem $os
181
     * @param stdClass              $resultRaw
182
     */
183 4
    private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw)
184
    {
185 4
        if (isset($resultRaw->platform) && $this->isRealResult($resultRaw->platform) === true) {
186 1
            $os->setName($resultRaw->platform);
187
        }
188
189 4
        if (isset($resultRaw->platform_version) && $this->isRealResult($resultRaw->platform_version) === true) {
190 1
            $os->getVersion()->setComplete($resultRaw->platform_version);
191
        }
192 4
    }
193
194
    /**
195
     *
196
     * @param Model\UserAgent $device
197
     * @param stdClass        $resultRaw
198
     */
199 4
    private function hydrateDevice(Model\Device $device, stdClass $resultRaw)
200
    {
201 4
        if (isset($resultRaw->device_name) && $this->isRealResult($resultRaw->device_name, 'device', 'model') === true) {
202 1
            $device->setModel($resultRaw->device_name);
203
        }
204
205 4
        if (isset($resultRaw->device_brand_name) && $this->isRealResult($resultRaw->device_brand_name) === true) {
206 1
            $device->setBrand($resultRaw->device_brand_name);
207
        }
208
209 4
        if (isset($resultRaw->device_type) && $this->isRealResult($resultRaw->device_type) === true) {
210
            // @todo convert to a common set of types (over all vendors)
211 1
            $device->setType($resultRaw->device_type);
212
        }
213
214 4
        if (isset($resultRaw->ismobiledevice) && $this->isRealResult($resultRaw->ismobiledevice) === true && $resultRaw->ismobiledevice === true) {
215 1
            $device->setIsMobile(true);
216
        }
217
218 4
        if (isset($resultRaw->device_pointing_method) && $resultRaw->device_pointing_method == 'touchscreen') {
219 1
            $device->setIsTouch(true);
220
        }
221 4
    }
222
223 12
    public function parse($userAgent, array $headers = [])
224
    {
225 12
        $parser = $this->getParser();
226
227
        /* @var $resultRaw \stdClass */
228 12
        $resultRaw = $parser->getBrowser($userAgent);
229
230
        /*
231
         * No result found?
232
         */
233 12
        if ($this->hasResult($resultRaw) !== true) {
234 4
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
235
        }
236
237
        /*
238
         * Hydrate the model
239
         */
240 8
        $result = new Model\UserAgent();
241 8
        $result->setProviderResultRaw($resultRaw);
242
243
        /*
244
         * Bot detection (does only work with full_php_browscap.ini)
245
         */
246 8
        if ($this->isBot($resultRaw) === true) {
247 4
            $this->hydrateBot($result->getBot(), $resultRaw);
248
249 4
            return $result;
250
        }
251
252
        /*
253
         * hydrate the result
254
         */
255 4
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
256 4
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw);
257 4
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw);
258 4
        $this->hydrateDevice($result->getDevice(), $resultRaw);
259
260 4
        return $result;
261
    }
262
}
263