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

YzalisUAParser::getParser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use UAParser\Result as UAResult;
5
use UAParser\Result\Result as UAParserResult;
6
use UserAgentParser\Exception;
7
use UserAgentParser\Model;
8
9
class YzalisUAParser 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' => false,
33
            'isTouch'  => false,
34
        ],
35
36
        'bot' => [
37
            'isBot' => false,
38
            'name'  => false,
39
            'type'  => false,
40
        ],
41
    ];
42
43
    protected $defaultValues = [
44
        'Other',
45
    ];
46
47
    private $parser;
48
49 12
    public function __construct()
50
    {
51 12 View Code Duplication
        if (! class_exists('UAParser\UAParser', 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...
52 1
            throw new Exception\PackageNotLoaded('You need to install ' . $this->getComposerPackageName() . ' to use this provider');
53
        }
54 11
    }
55
56 1
    public function getName()
57
    {
58 1
        return 'YzalisUAParser';
59
    }
60
61 3
    public function getComposerPackageName()
62
    {
63 3
        return 'yzalis/ua-parser';
64
    }
65
66
    /**
67
     *
68
     * @param \UAParser\UAParser $parser
69
     */
70 7
    public function setParser(\UAParser\UAParser $parser = null)
71
    {
72 7
        $this->parser = $parser;
73 7
    }
74
75
    /**
76
     *
77
     * @return \UAParser\UAParser
78
     */
79 7
    public function getParser()
80
    {
81 7
        if ($this->parser !== null) {
82 7
            return $this->parser;
83
        }
84
85 1
        $this->parser = new \UAParser\UAParser();
86
87 1
        return $this->parser;
88
    }
89
90
    /**
91
     *
92
     * @param UAParserResult $resultRaw
93
     *
94
     * @return bool
95
     */
96 6
    private function hasResult(UAParserResult $resultRaw)
97
    {
98
        /* @var $browserRaw \UAParser\Result\BrowserResult */
99 6
        $browserRaw = $resultRaw->getBrowser();
100
101 6
        if ($browserRaw !== null && $this->isRealResult($browserRaw->getFamily()) === true) {
102 1
            return true;
103
        }
104
105
        /* @var $osRaw \UAParser\Result\OperatingSystemResult */
106 5
        $osRaw = $resultRaw->getOperatingSystem();
107
108 5
        if ($osRaw !== null && $this->isRealResult($osRaw->getFamily()) === true) {
109 1
            return true;
110
        }
111
112
        /* @var $deviceRaw \UAParser\Result\DeviceResult */
113 4
        $deviceRaw = $resultRaw->getDevice();
114
115 4
        if ($deviceRaw !== null && $this->isRealResult($deviceRaw->getConstructor()) === true) {
116 1
            return true;
117
        }
118
119 3
        if ($deviceRaw !== null && $this->isRealResult($deviceRaw->getModel()) === true) {
120 1
            return true;
121
        }
122
123 2
        return false;
124
    }
125
126
    /**
127
     *
128
     * @param Model\Browser          $browser
129
     * @param UAResult\BrowserResult $browserRaw
130
     */
131 4 View Code Duplication
    private function hydrateBrowser(Model\Browser $browser, UAResult\BrowserResult $browserRaw)
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...
132
    {
133 4
        if ($this->isRealResult($browserRaw->getFamily()) === true) {
134 1
            $browser->setName($browserRaw->getFamily());
135
        }
136
137 4
        if ($this->isRealResult($browserRaw->getVersionString()) === true) {
138 1
            $browser->getVersion()->setComplete($browserRaw->getVersionString());
139
        }
140 4
    }
141
142
    /**
143
     *
144
     * @param Model\RenderingEngine          $engine
145
     * @param UAResult\RenderingEngineResult $renderingEngineRaw
146
     */
147 4 View Code Duplication
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, UAResult\RenderingEngineResult $renderingEngineRaw)
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...
148
    {
149 4
        if ($this->isRealResult($renderingEngineRaw->getFamily()) === true) {
150 1
            $engine->setName($renderingEngineRaw->getFamily());
151
        }
152
153 4
        if ($this->isRealResult($renderingEngineRaw->getVersion()) === true) {
154 1
            $engine->getVersion()->setComplete($renderingEngineRaw->getVersion());
155
        }
156 4
    }
157
158
    /**
159
     *
160
     * @param Model\OperatingSystem          $os
161
     * @param UAResult\OperatingSystemResult $osRaw
162
     */
163 4
    private function hydrateOperatingSystem(Model\OperatingSystem $os, UAResult\OperatingSystemResult $osRaw)
164
    {
165 4
        if ($this->isRealResult($osRaw->getFamily()) === true) {
166 1
            $os->setName($osRaw->getFamily());
167
        }
168
169 4
        if ($this->isRealResult($osRaw->getMajor()) === true) {
170 1
            $os->getVersion()->setMajor($osRaw->getMajor());
171
172 1
            if ($this->isRealResult($osRaw->getMinor()) === true) {
173 1
                $os->getVersion()->setMinor($osRaw->getMinor());
174
            }
175
176 1
            if ($this->isRealResult($osRaw->getPatch()) === true) {
177 1
                $os->getVersion()->setPatch($osRaw->getPatch());
178
            }
179
        }
180 4
    }
181
182
    /**
183
     *
184
     * @param Model\UserAgent       $device
185
     * @param UAResult\DeviceResult $deviceRaw
186
     */
187 4
    private function hydrateDevice(Model\Device $device, UAResult\DeviceResult $deviceRaw)
188
    {
189 4
        if ($this->isRealResult($deviceRaw->getModel()) === true) {
190 2
            $device->setModel($deviceRaw->getModel());
191
        }
192
193 4
        if ($this->isRealResult($deviceRaw->getConstructor()) === true) {
194 1
            $device->setBrand($deviceRaw->getConstructor());
195
        }
196
197
        // removed desktop type, since it's a default value and not really detected
198 4
        if ($this->isRealResult($deviceRaw->getType()) === true && $deviceRaw->getType() !== 'desktop') {
199 2
            $device->setType($deviceRaw->getType());
200
        }
201 4
    }
202
203 6
    public function parse($userAgent, array $headers = [])
204
    {
205 6
        $parser = $this->getParser();
206
207
        /* @var $resultRaw \UAParser\Result\Result */
208 6
        $resultRaw = $parser->parse($userAgent);
209
210
        /*
211
         * No result found?
212
         */
213 6
        if ($this->hasResult($resultRaw) !== true) {
214 2
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
215
        }
216
217
        /* @var $emailRaw \UAParser\Result\EmailClientResult */
218
        // currently not used...any idea to implement it?
219
220
        /*
221
         * Hydrate the model
222
         */
223 4
        $result = new Model\UserAgent();
224 4
        $result->setProviderResultRaw($resultRaw);
225
226
        /*
227
         * Bot detection is currently not possible
228
         */
229
230
        /*
231
         * hydrate the result
232
         */
233 4
        $this->hydrateBrowser($result->getBrowser(), $resultRaw->getBrowser());
234 4
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw->getRenderingEngine());
235 4
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->getOperatingSystem());
236 4
        $this->hydrateDevice($result->getDevice(), $resultRaw->getDevice());
237
238 4
        return $result;
239
    }
240
}
241