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 (#33)
by Martin
04:38
created

BrowscapPhp::getVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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