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
Pull Request — master (#44)
by Martin
05:39
created

BrowscapPhp::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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
    /**
12
     * Name of the provider
13
     *
14
     * @var string
15
     */
16
    protected $name = 'BrowscapPhp';
17
18
    /**
19
     * Homepage of the provider
20
     *
21
     * @var string
22
     */
23
    protected $homepage = 'https://github.com/browscap/browscap-php';
24
25
    /**
26
     * Composer package name
27
     *
28
     * @var string
29
     */
30
    protected $packageName = 'browscap/browscap-php';
31
32
    protected $detectionCapabilities = [
33
34
        'browser' => [
35
            'name'    => true,
36
            'version' => true,
37
        ],
38
39
        'renderingEngine' => [
40
            'name'    => true,
41
            'version' => true,
42
        ],
43
44
        'operatingSystem' => [
45
            'name'    => true,
46
            'version' => true,
47
        ],
48
49
        'device' => [
50
            'model'    => true,
51
            'brand'    => true,
52
            'type'     => true,
53
            'isMobile' => true,
54
            'isTouch'  => true,
55
        ],
56
57
        'bot' => [
58
            'isBot' => true,
59
            'name'  => true,
60
            'type'  => true,
61
        ],
62
    ];
63
64
    protected $defaultValues = [
65
        'DefaultProperties',
66
        'Default Browser',
67
68
        'unknown',
69
    ];
70
71
    /**
72
     *
73
     * @var Browscap
74
     */
75
    private $parser;
76
77 16
    public function __construct(Browscap $parser)
78
    {
79 16
        $this->parser = $parser;
80 16
    }
81
82 1
    public function getVersion()
83
    {
84 1
        return $this->getParser()
85 1
            ->getCache()
86 1
            ->getVersion();
87
    }
88
89
    public function getUpdateDate()
90
    {
91
        return;
92
    }
93
94
    /**
95
     *
96
     * @return Browscap
97
     */
98 12
    public function getParser()
99
    {
100 12
        return $this->parser;
101
    }
102
103
    /**
104
     *
105
     * @return array
106
     */
107 1
    private function getDeviceModelDefaultValues()
108
    {
109
        return [
110 1
            'general Desktop',
111 1
            'general Mobile Device',
112 1
            'general Mobile Phone',
113 1
            'general Tablet',
114 1
        ];
115
    }
116
117
    /**
118
     *
119
     * @param stdClass $resultRaw
120
     *
121
     * @return bool
122
     */
123 10
    private function hasResult(stdClass $resultRaw)
124
    {
125 10
        if (! isset($resultRaw->browser)) {
126 1
            return false;
127
        }
128
129 9
        if ($this->isRealResult($resultRaw->browser) !== true) {
130 4
            return false;
131
        }
132
133 5
        return true;
134
    }
135
136
    /**
137
     *
138
     * @param  stdClass $resultRaw
139
     * @return boolean
140
     */
141 5
    private function isBot(stdClass $resultRaw)
142
    {
143 5
        if (! isset($resultRaw->crawler) || $resultRaw->crawler !== true) {
144 2
            return false;
145
        }
146
147 3
        return true;
148
    }
149
150
    /**
151
     *
152
     * @param Model\Bot $bot
153
     * @param stdClass  $resultRaw
154
     */
155 3
    private function hydrateBot(Model\Bot $bot, stdClass $resultRaw)
156
    {
157 3
        $bot->setIsBot(true);
158
159 3
        if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser) === true) {
160 3
            $bot->setName($resultRaw->browser);
161
        }
162
163
        // @todo convert to a common set of types (over all vendors)
164 3
        if (isset($resultRaw->issyndicationreader) && $resultRaw->issyndicationreader === true) {
165 1
            $bot->setType('RSS');
166 3
        } elseif (isset($resultRaw->browser_type) && $resultRaw->browser_type === 'Bot/Crawler') {
167 1
            $bot->setType('Crawler');
168 2
        } elseif (isset($resultRaw->browser_type) && $this->isRealResult($resultRaw->browser_type) === true) {
169 1
            $bot->setType($resultRaw->browser_type);
170
        }
171 3
    }
172
173
    /**
174
     *
175
     * @param Model\Browser $browser
176
     * @param stdClass      $resultRaw
177
     */
178 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...
179
    {
180 2
        if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser) === true) {
181 2
            $browser->setName($resultRaw->browser);
182
        }
183
184 2
        if (isset($resultRaw->version) && $this->isRealResult($resultRaw->version) === true) {
185 2
            $browser->getVersion()->setComplete($resultRaw->version);
186
        }
187 2
    }
188
189
    /**
190
     *
191
     * @param Model\RenderingEngine $engine
192
     * @param stdClass              $resultRaw
193
     */
194 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...
195
    {
196 2
        if (isset($resultRaw->renderingengine_name) && $this->isRealResult($resultRaw->renderingengine_name) === true) {
197 1
            $engine->setName($resultRaw->renderingengine_name);
198
        }
199
200 2
        if (isset($resultRaw->renderingengine_version) && $this->isRealResult($resultRaw->renderingengine_version) === true) {
201 1
            $engine->getVersion()->setComplete($resultRaw->renderingengine_version);
202
        }
203 2
    }
204
205
    /**
206
     *
207
     * @param Model\OperatingSystem $os
208
     * @param stdClass              $resultRaw
209
     */
210 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...
211
    {
212 2
        if (isset($resultRaw->platform) && $this->isRealResult($resultRaw->platform) === true) {
213 1
            $os->setName($resultRaw->platform);
214
        }
215
216 2
        if (isset($resultRaw->platform_version) && $this->isRealResult($resultRaw->platform_version) === true) {
217 1
            $os->getVersion()->setComplete($resultRaw->platform_version);
218
        }
219 2
    }
220
221
    /**
222
     *
223
     * @param Model\UserAgent $device
224
     * @param stdClass        $resultRaw
225
     */
226 2
    private function hydrateDevice(Model\Device $device, stdClass $resultRaw)
227
    {
228 2
        if (isset($resultRaw->device_name) && $this->isRealResult($resultRaw->device_name, $this->getDeviceModelDefaultValues()) === true) {
229 1
            $device->setModel($resultRaw->device_name);
230
        }
231
232 2 View Code Duplication
        if (isset($resultRaw->device_brand_name) && $this->isRealResult($resultRaw->device_brand_name) === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
233 1
            $device->setBrand($resultRaw->device_brand_name);
234
        }
235
236 2
        if (isset($resultRaw->device_type) && $this->isRealResult($resultRaw->device_type) === true) {
237
            // @todo convert to a common set of types (over all vendors)
238 1
            $device->setType($resultRaw->device_type);
239
        }
240
241 2
        if (isset($resultRaw->ismobiledevice) && $this->isRealResult($resultRaw->ismobiledevice) === true && $resultRaw->ismobiledevice === true) {
242 1
            $device->setIsMobile(true);
243
        }
244
245 2
        if (isset($resultRaw->device_pointing_method) && $resultRaw->device_pointing_method == 'touchscreen') {
246 1
            $device->setIsTouch(true);
247
        }
248 2
    }
249
250 10 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...
251
    {
252 10
        $parser = $this->getParser();
253
254
        /* @var $resultRaw \stdClass */
255 10
        $resultRaw = $parser->getBrowser($userAgent);
256
257
        /*
258
         * No result found?
259
         */
260 10
        if ($this->hasResult($resultRaw) !== true) {
261 5
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
262
        }
263
264
        /*
265
         * Hydrate the model
266
         */
267 5
        $result = new Model\UserAgent();
268 5
        $result->setProviderResultRaw($resultRaw);
269
270
        /*
271
         * Bot detection (does only work with full_php_browscap.ini)
272
         */
273 5
        if ($this->isBot($resultRaw) === true) {
274 3
            $this->hydrateBot($result->getBot(), $resultRaw);
275
276 3
            return $result;
277
        }
278
279
        /*
280
         * hydrate the result
281
         */
282 2
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
283 2
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw);
284 2
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw);
285 2
        $this->hydrateDevice($result->getDevice(), $resultRaw);
286
287 2
        return $result;
288
    }
289
}
290