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 (#111)
by Martin
06:28
created

Mimmi20BrowserDetector::hydrateBrowser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use BrowserDetector\Detector;
5
use Psr\Log\NullLogger;
6
use Psr6NullCache\Adapter\MemoryCacheItemPool;
7
use UaResult\Browser\BrowserInterface;
8
use UaResult\Device\DeviceInterface;
9
use UaResult\Engine\EngineInterface;
10
use UaResult\Os\OsInterface;
11
use UaResult\Result\Result;
12
use UserAgentParser\Exception\NoResultFoundException;
13
use UserAgentParser\Exception\PackageNotLoadedException;
14
use UserAgentParser\Model;
15
16
/**
17
 * Abstraction for mimmi20/browser-detector
18
 *
19
 * @author Martin Keckeis <[email protected]>
20
 * @license MIT
21
 * @see https://github.com/donatj/PhpUserAgent
22
 */
23
class Mimmi20BrowserDetector extends AbstractProvider
24
{
25
    /**
26
     * Name of the provider
27
     *
28
     * @var string
29
     */
30
    protected $name = 'Mimmi20BrowserDetector';
31
32
    /**
33
     * Homepage of the provider
34
     *
35
     * @var string
36
     */
37
    protected $homepage = 'https://github.com/mimmi20/BrowserDetector';
38
39
    /**
40
     * Composer package name
41
     *
42
     * @var string
43
     */
44
    protected $packageName = 'mimmi20/browser-detector';
45
46
    protected $detectionCapabilities = [
47
48
        'browser' => [
49
            'name'    => true,
50
            'version' => true,
51
        ],
52
53
        'renderingEngine' => [
54
            'name'    => true,
55
            'version' => true,
56
        ],
57
58
        'operatingSystem' => [
59
            'name'    => true,
60
            'version' => true,
61
        ],
62
63
        'device' => [
64
            'model'    => true,
65
            'brand'    => true,
66
            'type'     => true,
67
            'isMobile' => true,
68
            'isTouch'  => true,
69
        ],
70
71
        'bot' => [
72
            'isBot' => true,
73
            'name'  => true,
74
            'type'  => true,
75
        ],
76
    ];
77
78
    protected $defaultValues = [
79
80
        'general' => [
81
            '/^unknown$/i',
82
        ],
83
    ];
84
85
    /**
86
     *
87
     * @var Detector
88
     */
89
    private $parser;
90
91
    /**
92
     *
93
     * @param  Detector                  $parser
94
     * @throws PackageNotLoadedException
95
     */
96 15
    public function __construct(Detector $parser = null)
97
    {
98 15
        if ($parser === null) {
99 8
            $this->checkIfInstalled();
100
        }
101
102 15
        $this->parser = $parser;
103 15
    }
104
105
    /**
106
     *
107
     * @return Detector
108
     */
109 8
    public function getParser()
110
    {
111 8
        if ($this->parser !== null) {
112 8
            return $this->parser;
113
        }
114
115 1
        $cache  = new MemoryCacheItemPool();
116 1
        $logger = new NullLogger();
117
118 1
        $this->parser = new Detector($cache, $logger);
119
120 1
        return $this->parser;
121
    }
122
123
    /**
124
     *
125
     * @param Result $result
126
     *
127
     * @return bool
128
     */
129 7
    private function hasResult(Result $result)
130
    {
131 7
        if ($this->isRealResult($result->getBrowser()
132 7
            ->getType()
133 7
            ->getType())) {
134 6
            return true;
135
        }
136
137 1
        return false;
138
    }
139
140
    /**
141
     *
142
     * @param Model\Bot        $bot
143
     * @param BrowserInterface $browserRaw
144
     */
145 2
    private function hydrateBot(Model\Bot $bot, BrowserInterface $browserRaw)
146
    {
147 2
        $bot->setIsBot(true);
148 2
        $bot->setName($this->getRealResult($browserRaw->getName()));
149 2
        $bot->setType($this->getRealResult($browserRaw->getType()->getType()));
150 2
    }
151
152
    /**
153
     *
154
     * @param Model\Browser    $browser
155
     * @param BrowserInterface $browserRaw
156
     */
157 4
    private function hydrateBrowser(Model\Browser $browser, BrowserInterface $browserRaw)
158
    {
159 4
        $browser->setName($this->getRealResult($browserRaw->getName()));
160 4
        $browser->getVersion()->setComplete($this->getRealResult($browserRaw->getVersion()
161 4
            ->getVersion()));
162 4
    }
163
164
    /**
165
     *
166
     * @param Model\RenderingEngine $engine
167
     * @param EngineInterface       $engineRaw
168
     */
169 4
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, EngineInterface $engineRaw)
170
    {
171 4
        $engine->setName($this->getRealResult($engineRaw->getName()));
172 4
        $engine->getVersion()->setComplete($this->getRealResult($engineRaw->getVersion()->getVersion()));
173 4
    }
174
175
    /**
176
     *
177
     * @param Model\OperatingSystem $os
178
     * @param OsInterface           $osRaw
179
     */
180 4
    private function hydrateOperatingSystem(Model\OperatingSystem $os, OsInterface $osRaw)
181
    {
182 4
        $os->setName($this->getRealResult($osRaw->getName()));
183 4
        $os->getVersion()->setComplete($this->getRealResult($osRaw->getVersion()->getVersion()));
184 4
    }
185
186
    /**
187
     *
188
     * @param Model\UserAgent $device
189
     * @param DeviceInterface $deviceRaw
190
     */
191 4
    private function hydrateDevice(Model\Device $device, DeviceInterface $deviceRaw)
192
    {
193 4
        $device->setModel($this->getRealResult($deviceRaw->getDeviceName()));
194 4
        $device->setBrand($this->getRealResult($deviceRaw->getBrand()->getBrandName()));
195 4
        $device->setType($this->getRealResult($deviceRaw->getType()->getName()));
196
197 4
        $device->setIsMobile($deviceRaw->getType()->isMobile());
198 4
        if ($deviceRaw->getPointingMethod() === 'touchscreen') {
199 1
            $device->setIsTouch(true);
200
        }
201 4
    }
202
203 7
    public function parse($userAgent, array $headers = [])
204
    {
205 7
        $headers['HTTP_USER_AGENT'] = $userAgent;
206
207 7
        $detector = $this->getParser();
208
209 7
        $parser = $detector->getBrowser($headers);
210
211
        /*
212
         * No result found?
213
         */
214 7
        if ($this->hasResult($parser) !== true) {
215 1
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
216
        }
217
218
        /*
219
         * Hydrate the model
220
         */
221 6
        $result = new Model\UserAgent($this->getName(), $this->getVersion());
222 6
        $result->setProviderResultRaw($parser->toArray(true));
223
224
        /*
225
         * Bot detection
226
         */
227 6
        if ($parser->getBrowser()->getType()->isBot() === true) {
228 2
            $this->hydrateBot($result->getBot(), $parser->getBrowser());
229
230 2
            return $result;
231
        }
232
233
        /*
234
         * hydrate the result
235
         */
236 4
        $this->hydrateBrowser($result->getBrowser(), $parser->getBrowser());
237 4
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $parser->getEngine());
238 4
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $parser->getOs());
239 4
        $this->hydrateDevice($result->getDevice(), $parser->getDevice());
240
241 4
        return $result;
242
    }
243
}
244