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

WhichBrowser::getHomepage()   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 1
Bugs 0 Features 0
Metric Value
c 1
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 UserAgentParser\Exception;
5
use UserAgentParser\Model;
6
use WhichBrowser\Parser as WhichBrowserParser;
7
8
class WhichBrowser extends AbstractProvider
9
{
10
    /**
11
     * Name of the provider
12
     *
13
     * @var string
14
     */
15
    protected $name = 'WhichBrowser';
16
17
    /**
18
     * Homepage of the provider
19
     *
20
     * @var string
21
     */
22
    protected $homepage = 'https://github.com/WhichBrowser/Parser';
23
24
    /**
25
     * Composer package name
26
     *
27
     * @var string
28
     */
29
    protected $packageName = 'whichbrowser/parser';
30
31
    protected $detectionCapabilities = [
32
33
        'browser' => [
34
            'name'    => true,
35
            'version' => true,
36
        ],
37
38
        'renderingEngine' => [
39
            'name'    => true,
40
            'version' => true,
41
        ],
42
43
        'operatingSystem' => [
44
            'name'    => true,
45
            'version' => true,
46
        ],
47
48
        'device' => [
49
            'model'    => true,
50
            'brand'    => true,
51
            'type'     => true,
52
            'isMobile' => true,
53
            'isTouch'  => false,
54
        ],
55
56
        'bot' => [
57
            'isBot' => true,
58
            'name'  => true,
59
            'type'  => false,
60
        ],
61
    ];
62
63
    /**
64
     * Used for unitTests mocking
65
     *
66
     * @var WhichBrowserParser
67
     */
68
    private $parser;
69
70 15
    public function __construct()
71
    {
72 15
        if (! class_exists('WhichBrowser\Parser', true)) {
73 1
            throw new Exception\PackageNotLoadedException('You need to install ' . $this->getHomepage() . ' to use this provider');
74
        }
75 14
    }
76
77 1
    public function getName()
78
    {
79 1
        return 'WhichBrowser';
80
    }
81
82 2
    public function getHomepage()
83
    {
84 2
        return 'https://github.com/WhichBrowser/Parser';
85
    }
86
87
    /**
88
     *
89
     * @param  array              $headers
90
     * @return WhichBrowserParser
91
     */
92 8
    public function getParser(array $headers)
93
    {
94 8
        if ($this->parser !== null) {
95 7
            return $this->parser;
96
        }
97
98 1
        return new WhichBrowserParser($headers);
99
    }
100
101
    /**
102
     *
103
     * @param Model\Bot                   $bot
104
     * @param \WhichBrowser\Model\Browser $browserRaw
105
     */
106 1
    private function hydrateBot(Model\Bot $bot, \WhichBrowser\Model\Browser $browserRaw)
107
    {
108 1
        $bot->setIsBot(true);
109
110 1
        if ($this->isRealResult($browserRaw->getName()) === true) {
111 1
            $bot->setName($browserRaw->getName());
112
        }
113 1
    }
114
115
    /**
116
     *
117
     * @param Model\Browser               $browser
118
     * @param \WhichBrowser\Model\Browser $browserRaw
119
     */
120 5
    private function hydrateBrowser(Model\Browser $browser, \WhichBrowser\Model\Browser $browserRaw)
121
    {
122 5 View Code Duplication
        if ($this->isRealResult($browserRaw->getName()) === 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...
123 1
            $browser->setName($browserRaw->getName());
124
125 1
            if ($this->isRealResult($browserRaw->getVersion()) === true) {
126 1
                $browser->getVersion()->setComplete($browserRaw->getVersion());
127
            }
128
129 1
            return;
130
        }
131
132 4
        if (isset($browserRaw->using) && $browserRaw->using instanceof \WhichBrowser\Model\Using) {
133
            /* @var $usingRaw \WhichBrowser\Model\Using */
134 1
            $usingRaw = $browserRaw->using;
135
136 1 View Code Duplication
            if ($this->isRealResult($usingRaw->getName()) === 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...
137 1
                $browser->setName($usingRaw->getName());
138
139 1
                if ($this->isRealResult($usingRaw->getVersion()) === true) {
140 1
                    $browser->getVersion()->setComplete($usingRaw->getVersion());
141
                }
142
            }
143
        }
144 4
    }
145
146
    /**
147
     *
148
     * @param Model\RenderingEngine      $engine
149
     * @param \WhichBrowser\Model\Engine $engineRaw
150
     */
151 5 View Code Duplication
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, \WhichBrowser\Model\Engine $engineRaw)
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...
152
    {
153 5
        if ($this->isRealResult($engineRaw->getName()) === true) {
154 1
            $engine->setName($engineRaw->getName());
155
        }
156
157 5
        if ($this->isRealResult($engineRaw->getVersion()) === true) {
158 1
            $engine->getVersion()->setComplete($engineRaw->getVersion());
159
        }
160 5
    }
161
162
    /**
163
     *
164
     * @param Model\OperatingSystem  $os
165
     * @param \WhichBrowser\Model\Os $osRaw
166
     */
167 5 View Code Duplication
    private function hydrateOperatingSystem(Model\OperatingSystem $os, \WhichBrowser\Model\Os $osRaw)
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...
168
    {
169 5
        if ($this->isRealResult($osRaw->getName()) === true) {
170 1
            $os->setName($osRaw->getName());
171
        }
172
173 5
        if ($this->isRealResult($osRaw->getVersion()) === true) {
174 1
            $os->getVersion()->setComplete($osRaw->getVersion());
175
        }
176 5
    }
177
178
    /**
179
     *
180
     * @param Model\Device               $device
181
     * @param \WhichBrowser\Model\Device $deviceRaw
182
     * @param WhichBrowserParser         $parser
183
     */
184 5
    private function hydrateDevice(Model\Device $device, \WhichBrowser\Model\Device $deviceRaw, WhichBrowserParser $parser)
185
    {
186 5
        if ($this->isRealResult($deviceRaw->getModel()) === true) {
187 1
            $device->setModel($deviceRaw->getModel());
188
        }
189
190 5
        if ($this->isRealResult($deviceRaw->getManufacturer()) === true) {
191 1
            $device->setBrand($deviceRaw->getManufacturer());
192
        }
193
194 5
        if ($this->isRealResult($parser->getType()) === true) {
195 1
            $device->setType($parser->getType());
196
        }
197
198 5
        if ($parser->isType('mobile', 'tablet', 'ereader', 'media', 'watch', 'camera', 'gaming:portable') === true) {
199 1
            $device->setIsMobile(true);
200
        }
201 5
    }
202
203 7
    public function parse($userAgent, array $headers = [])
204
    {
205 7
        $headers['User-Agent'] = $userAgent;
206
207 7
        $parser = $this->getParser($headers);
208
209
        /*
210
         * No result found?
211
         */
212 7
        if ($parser->isDetected() !== true) {
213 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
214
        }
215
216
        /*
217
         * Hydrate the model
218
         */
219 6
        $result = new Model\UserAgent();
220 6
        $result->setProviderResultRaw($parser->toArray());
221
222
        /*
223
         * Bot detection
224
         */
225 6
        if ($parser->getType() === 'bot') {
226 1
            $this->hydrateBot($result->getBot(), $parser->browser);
227
228 1
            return $result;
229
        }
230
231
        /*
232
         * hydrate the result
233
         */
234 5
        $this->hydrateBrowser($result->getBrowser(), $parser->browser);
235 5
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $parser->engine);
236 5
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $parser->os);
237 5
        $this->hydrateDevice($result->getDevice(), $parser->device, $parser);
238
239 5
        return $result;
240
    }
241
}
242