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 ( 65539f...8ccec6 )
by Martin
9s
created

AbstractBrowscap::hydrateBot()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7

Importance

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