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

BrowscapPhp::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 2
Bugs 0 Features 0
Metric Value
c 2
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 BrowscapPHP\Browscap;
5
use stdClass;
6
use UserAgentParser\Exception;
7
use UserAgentParser\Model;
8
9
class BrowscapPhp extends AbstractProvider
10
{
11
    protected $detectionCapabilities = [
12
13
        'browser' => [
14
            'name'    => true,
15
            'version' => true,
16
        ],
17
18
        'renderingEngine' => [
19
            'name'    => true,
20
            'version' => true,
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'  => true,
39
            'type'  => true,
40
        ],
41
    ];
42
43
    protected $defaultValues = [
44
        'DefaultProperties',
45
        'Default Browser',
46
47
        'unknown',
48
    ];
49
50
    /**
51
     *
52
     * @var Browscap
53
     */
54
    private $parser;
55
56 16
    public function __construct(Browscap $parser)
57
    {
58 16
        $this->parser = $parser;
59 16
    }
60
61 1
    public function getName()
62
    {
63 1
        return 'BrowscapPhp';
64
    }
65
66 1
    public function getComposerPackageName()
67
    {
68 1
        return 'browscap/browscap-php';
69
    }
70
71 1
    public function getVersion()
72
    {
73 1
        return $this->getParser()
74 1
            ->getCache()
75 1
            ->getVersion();
76
    }
77
78
    /**
79
     *
80
     * @return Browscap
81
     */
82 13
    public function getParser()
83
    {
84 13
        return $this->parser;
85
    }
86
87
    /**
88
     *
89
     * @return array
90
     */
91 1
    private function getDeviceModelDefaultValues()
92
    {
93
        return [
94 1
            'general Desktop',
95 1
            'general Mobile Device',
96 1
            'general Mobile Phone',
97 1
            'general Tablet',
98 1
        ];
99
    }
100
101
    /**
102
     *
103
     * @param stdClass $resultRaw
104
     *
105
     * @return bool
106
     */
107 11
    private function hasResult(stdClass $resultRaw)
108
    {
109 11
        if (! isset($resultRaw->browser)) {
110 2
            return false;
111
        }
112
113 9
        if ($this->isRealResult($resultRaw->browser) !== true) {
114 4
            return false;
115
        }
116
117 5
        return true;
118
    }
119
120
    /**
121
     *
122
     * @param  stdClass $resultRaw
123
     * @return boolean
124
     */
125 5
    private function isBot(stdClass $resultRaw)
126
    {
127 5
        if (! isset($resultRaw->crawler) || $resultRaw->crawler !== true) {
128 2
            return false;
129
        }
130
131 3
        return true;
132
    }
133
134
    /**
135
     *
136
     * @param Model\Bot $bot
137
     * @param stdClass  $resultRaw
138
     */
139 3
    private function hydrateBot(Model\Bot $bot, stdClass $resultRaw)
140
    {
141 3
        $bot->setIsBot(true);
142
143 3
        if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser) === true) {
144 3
            $bot->setName($resultRaw->browser);
145
        }
146
147
        // @todo convert to a common set of types (over all vendors)
148 3
        if (isset($resultRaw->issyndicationreader) && $resultRaw->issyndicationreader === true) {
149 1
            $bot->setType('RSS');
150 3
        } elseif (isset($resultRaw->browser_type) && $resultRaw->browser_type === 'Bot/Crawler') {
151 1
            $bot->setType('Crawler');
152 2
        } elseif (isset($resultRaw->browser_type) && $this->isRealResult($resultRaw->browser_type) === true) {
153 1
            $bot->setType($resultRaw->browser_type);
154
        }
155 3
    }
156
157
    /**
158
     *
159
     * @param Model\Browser $browser
160
     * @param stdClass      $resultRaw
161
     */
162 2 View Code Duplication
    private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
    {
164 2
        if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser) === true) {
165 2
            $browser->setName($resultRaw->browser);
166
        }
167
168 2
        if (isset($resultRaw->version) && $this->isRealResult($resultRaw->version) === true) {
169 2
            $browser->getVersion()->setComplete($resultRaw->version);
170
        }
171 2
    }
172
173
    /**
174
     *
175
     * @param Model\RenderingEngine $engine
176
     * @param stdClass              $resultRaw
177
     */
178 2 View Code Duplication
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
    {
180 2
        if (isset($resultRaw->renderingengine_name) && $this->isRealResult($resultRaw->renderingengine_name) === true) {
181 1
            $engine->setName($resultRaw->renderingengine_name);
182
        }
183
184 2
        if (isset($resultRaw->renderingengine_version) && $this->isRealResult($resultRaw->renderingengine_version) === true) {
185 1
            $engine->getVersion()->setComplete($resultRaw->renderingengine_version);
186
        }
187 2
    }
188
189
    /**
190
     *
191
     * @param Model\OperatingSystem $os
192
     * @param stdClass              $resultRaw
193
     */
194 2 View Code Duplication
    private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
195
    {
196 2
        if (isset($resultRaw->platform) && $this->isRealResult($resultRaw->platform) === true) {
197 1
            $os->setName($resultRaw->platform);
198
        }
199
200 2
        if (isset($resultRaw->platform_version) && $this->isRealResult($resultRaw->platform_version) === true) {
201 1
            $os->getVersion()->setComplete($resultRaw->platform_version);
202
        }
203 2
    }
204
205
    /**
206
     *
207
     * @param Model\UserAgent $device
208
     * @param stdClass        $resultRaw
209
     */
210 2
    private function hydrateDevice(Model\Device $device, stdClass $resultRaw)
211
    {
212 2
        if (isset($resultRaw->device_name) && $this->isRealResult($resultRaw->device_name, $this->getDeviceModelDefaultValues()) === true) {
213 1
            $device->setModel($resultRaw->device_name);
214
        }
215
216 2
        if (isset($resultRaw->device_brand_name) && $this->isRealResult($resultRaw->device_brand_name) === true) {
217 1
            $device->setBrand($resultRaw->device_brand_name);
218
        }
219
220 2
        if (isset($resultRaw->device_type) && $this->isRealResult($resultRaw->device_type) === true) {
221
            // @todo convert to a common set of types (over all vendors)
222 1
            $device->setType($resultRaw->device_type);
223
        }
224
225 2
        if (isset($resultRaw->ismobiledevice) && $this->isRealResult($resultRaw->ismobiledevice) === true && $resultRaw->ismobiledevice === true) {
226 1
            $device->setIsMobile(true);
227
        }
228
229 2
        if (isset($resultRaw->device_pointing_method) && $resultRaw->device_pointing_method == 'touchscreen') {
230 1
            $device->setIsTouch(true);
231
        }
232 2
    }
233
234 11 View Code Duplication
    public function parse($userAgent, array $headers = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
235
    {
236 11
        $parser = $this->getParser();
237
238
        /* @var $resultRaw \stdClass */
239 11
        $resultRaw = $parser->getBrowser($userAgent);
240
241
        /*
242
         * No result found?
243
         */
244 11
        if ($this->hasResult($resultRaw) !== true) {
245 6
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
246
        }
247
248
        /*
249
         * Hydrate the model
250
         */
251 5
        $result = new Model\UserAgent();
252 5
        $result->setProviderResultRaw($resultRaw);
253
254
        /*
255
         * Bot detection (does only work with full_php_browscap.ini)
256
         */
257 5
        if ($this->isBot($resultRaw) === true) {
258 3
            $this->hydrateBot($result->getBot(), $resultRaw);
259
260 3
            return $result;
261
        }
262
263
        /*
264
         * hydrate the result
265
         */
266 2
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
267 2
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw);
268 2
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw);
269 2
        $this->hydrateDevice($result->getDevice(), $resultRaw);
270
271 2
        return $result;
272
    }
273
}
274