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
Push — master ( 06960b...53f99c )
by Martin
05:32
created

YzalisUAParser::hydrateDevice()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 8.8571
cc 6
eloc 9
nc 16
nop 2
crap 6
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 $defaultValues = [
12
        'Other',
13
    ];
14
15
    private $parser;
16
17 1
    public function getName()
18
    {
19 1
        return 'YzalisUAParser';
20
    }
21
22 2
    public function getComposerPackageName()
23
    {
24 2
        return 'yzalis/ua-parser';
25
    }
26
27
    /**
28
     *
29
     * @param \UAParser\UAParser $parser
30
     */
31 7
    public function setParser(\UAParser\UAParser $parser = null)
32
    {
33 7
        $this->parser = $parser;
34 7
    }
35
36
    /**
37
     *
38
     * @return \UAParser\UAParser
39
     */
40 7
    public function getParser()
41
    {
42 7
        if ($this->parser !== null) {
43 7
            return $this->parser;
44
        }
45
46 1
        $this->parser = new \UAParser\UAParser();
47
48 1
        return $this->parser;
49
    }
50
51
    /**
52
     *
53
     * @param UAParserResult $resultRaw
54
     *
55
     * @return bool
56
     */
57 6
    private function hasResult(UAParserResult $resultRaw)
58
    {
59
        /* @var $browserRaw \UAParser\Result\BrowserResult */
60 6
        $browserRaw = $resultRaw->getBrowser();
61
62 6
        if ($browserRaw !== null && $this->isRealResult($browserRaw->getFamily()) === true) {
63 1
            return true;
64
        }
65
66
        /* @var $osRaw \UAParser\Result\OperatingSystemResult */
67 5
        $osRaw = $resultRaw->getOperatingSystem();
68
69 5
        if ($osRaw !== null && $this->isRealResult($osRaw->getFamily()) === true) {
70 1
            return true;
71
        }
72
73
        /* @var $deviceRaw \UAParser\Result\DeviceResult */
74 4
        $deviceRaw = $resultRaw->getDevice();
75
76 4
        if ($deviceRaw !== null && $this->isRealResult($deviceRaw->getConstructor()) === true) {
77 1
            return true;
78
        }
79
80 3
        if ($deviceRaw !== null && $this->isRealResult($deviceRaw->getModel()) === true) {
81 1
            return true;
82
        }
83
84 2
        return false;
85
    }
86
87
    /**
88
     *
89
     * @param Model\Browser          $browser
90
     * @param UAResult\BrowserResult $browserRaw
91
     */
92 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...
93
    {
94 4
        if ($this->isRealResult($browserRaw->getFamily()) === true) {
95 1
            $browser->setName($browserRaw->getFamily());
96
        }
97
98 4
        if ($this->isRealResult($browserRaw->getVersionString()) === true) {
99 1
            $browser->getVersion()->setComplete($browserRaw->getVersionString());
100
        }
101 4
    }
102
103
    /**
104
     *
105
     * @param Model\RenderingEngine          $engine
106
     * @param UAResult\RenderingEngineResult $renderingEngineRaw
107
     */
108 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...
109
    {
110 4
        if ($this->isRealResult($renderingEngineRaw->getFamily()) === true) {
111 1
            $engine->setName($renderingEngineRaw->getFamily());
112
        }
113
114 4
        if ($this->isRealResult($renderingEngineRaw->getVersion()) === true) {
115 1
            $engine->getVersion()->setComplete($renderingEngineRaw->getVersion());
116
        }
117 4
    }
118
119
    /**
120
     *
121
     * @param Model\OperatingSystem          $os
122
     * @param UAResult\OperatingSystemResult $resultRaw
0 ignored issues
show
Bug introduced by
There is no parameter named $resultRaw. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
123
     */
124 4
    private function hydrateOperatingSystem(Model\OperatingSystem $os, UAResult\OperatingSystemResult $osRaw)
125
    {
126 4
        if ($this->isRealResult($osRaw->getFamily()) === true) {
127 1
            $os->setName($osRaw->getFamily());
128
        }
129
130 4
        if ($this->isRealResult($osRaw->getMajor()) === true) {
131 1
            $os->getVersion()->setMajor($osRaw->getMajor());
132
133 1
            if ($this->isRealResult($osRaw->getMinor()) === true) {
134 1
                $os->getVersion()->setMinor($osRaw->getMinor());
135
            }
136
137 1
            if ($this->isRealResult($osRaw->getPatch()) === true) {
138 1
                $os->getVersion()->setPatch($osRaw->getPatch());
139
            }
140
        }
141 4
    }
142
143
    /**
144
     *
145
     * @param Model\UserAgent       $device
146
     * @param UAResult\DeviceResult $deviceRaw
147
     */
148 4
    private function hydrateDevice(Model\Device $device, UAResult\DeviceResult $deviceRaw)
149
    {
150 4
        if ($this->isRealResult($deviceRaw->getModel()) === true) {
151 2
            $device->setModel($deviceRaw->getModel());
152
        }
153
154 4
        if ($this->isRealResult($deviceRaw->getConstructor()) === true) {
155 1
            $device->setBrand($deviceRaw->getConstructor());
156
        }
157
158
        // removed desktop type, since it's a default value and not really detected
159 4
        if ($this->isRealResult($deviceRaw->getType()) === true && $deviceRaw->getType() !== 'desktop') {
160 2
            $device->setType($deviceRaw->getType());
161
        }
162
163 4
        if ($this->isMobile($deviceRaw) === true) {
164 2
            $device->setIsMobile(true);
165
        }
166 4
    }
167
168
    /**
169
     *
170
     * @param  UAResult\DeviceResult $deviceRaw
171
     * @return bool
172
     */
173 4
    private function isMobile(UAResult\DeviceResult $deviceRaw)
174
    {
175 4
        if ($deviceRaw->getType() === 'mobile') {
176 1
            return true;
177
        }
178
179 3
        if ($deviceRaw->getType() === 'tablet') {
180 1
            return true;
181
        }
182
183 2
        return false;
184
    }
185
186 6
    public function parse($userAgent, array $headers = [])
187
    {
188 6
        $parser = $this->getParser();
189
190
        /* @var $resultRaw \UAParser\Result\Result */
191 6
        $resultRaw = $parser->parse($userAgent);
192
193
        /*
194
         * No result found?
195
         */
196 6
        if ($this->hasResult($resultRaw) !== true) {
197 2
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
198
        }
199
200
        /* @var $emailRaw \UAParser\Result\EmailClientResult */
201
        // currently not used...any idea to implement it?
202
203
        /*
204
         * Hydrate the model
205
         */
206 4
        $result = new Model\UserAgent();
207 4
        $result->setProviderResultRaw($resultRaw);
208
209
        /*
210
         * Bot detection is currently not possible
211
         */
212
213 4
        $this->hydrateBrowser($result->getBrowser(), $resultRaw->getBrowser());
214 4
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw->getRenderingEngine());
215 4
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->getOperatingSystem());
216 4
        $this->hydrateDevice($result->getDevice(), $resultRaw->getDevice());
217
218 4
        return $result;
219
    }
220
}
221