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 ( 32e807...794dfa )
by Martin
06:01
created

Endorphin::hydrateBot()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use EndorphinStudio\Detector as EndorphinDetector;
5
use UserAgentParser\Exception\NoResultFoundException;
6
use UserAgentParser\Exception\PackageNotLoadedException;
7
use UserAgentParser\Model;
8
9
/**
10
 * Abstraction for piwik/device-detector
11
 *
12
 * @author Martin Keckeis <[email protected]>
13
 * @license MIT
14
 * @see https://github.com/EndorphinDetector-studio/browser-detector
15
 */
16
class Endorphin extends AbstractProvider
17
{
18
    /**
19
     * Name of the provider
20
     *
21
     * @var string
22
     */
23
    protected $name = 'Endorphin';
24
25
    /**
26
     * Homepage of the provider
27
     *
28
     * @var string
29
     */
30
    protected $homepage = 'https://github.com/endorphin-studio/browser-detector';
31
32
    /**
33
     * Composer package name
34
     *
35
     * @var string
36
     */
37
    protected $packageName = 'endorphin-studio/browser-detector';
38
39
    protected $detectionCapabilities = [
40
41
        'browser' => [
42
            'name'    => true,
43
            'version' => true,
44
        ],
45
46
        'renderingEngine' => [
47
            'name'    => false,
48
            'version' => false,
49
        ],
50
51
        'operatingSystem' => [
52
            'name'    => true,
53
            'version' => true,
54
        ],
55
56
        'device' => [
57
            'model'    => false,
58
            'brand'    => false,
59
            'type'     => true,
60
            'isMobile' => false,
61
            'isTouch'  => false,
62
        ],
63
64
        'bot' => [
65
            'isBot' => true,
66
            'name'  => true,
67
            'type'  => true,
68
        ],
69
    ];
70
71
    protected $defaultValues = [
72
73
        'general' => [
74
            '/^N\\\\A$/i',
75
        ],
76
77
        'device' => [
78
79
            'model' => [
80
                '/^Desktop/i',
81
            ],
82
        ],
83
    ];
84
85
    /**
86
     * Used for unitTests mocking
87
     *
88
     * @var EndorphinDetector\Detector
89
     */
90
    private $parser;
91
92
    /**
93
     *
94
     * @throws PackageNotLoadedException
95
     */
96 14
    public function __construct()
97
    {
98 14
        if (! file_exists('vendor/' . $this->getPackageName() . '/composer.json')) {
99 1
            throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider');
100
        }
101 13
    }
102
103
    /**
104
     *
105
     * @param  string                           $userAgent
106
     * @return EndorphinDetector\DetectorResult
107
     */
108 6
    public function getParser($userAgent)
109
    {
110 6
        if ($this->parser !== null) {
111 5
            return $this->parser;
112
        }
113
114 1
        return EndorphinDetector\Detector::analyse($userAgent);
115
    }
116
117
    /**
118
     *
119
     * @param EndorphinDetector\DetectorResult $resultRaw
120
     *
121
     * @return bool
122
     */
123 5
    private function hasResult(EndorphinDetector\DetectorResult $resultRaw)
124
    {
125 5
        if ($this->isRealResult($resultRaw->OS->getName()) === true) {
126 1
            return true;
127
        }
128
129 4
        if ($this->isRealResult($resultRaw->Browser->getName()) === true) {
130 1
            return true;
131
        }
132
133 3
        if ($this->isRealResult($resultRaw->Device->getName(), 'device', 'model') === true) {
134 1
            return true;
135
        }
136
137 2
        if ($this->isRealResult($resultRaw->Robot->getType()) === true) {
138 1
            return true;
139
        }
140
141 1
        return false;
142
    }
143
144
    /**
145
     *
146
     * @param Model\Bot               $bot
147
     * @param EndorphinDetector\Robot $resultRaw
148
     */
149 1
    private function hydrateBot(Model\Bot $bot, EndorphinDetector\Robot $resultRaw)
150
    {
151 1
        $bot->setIsBot(true);
152 1
        $bot->setName($this->getRealResult($resultRaw->getName()));
153 1
        $bot->setType($this->getRealResult($resultRaw->getType()));
154 1
    }
155
156
    /**
157
     *
158
     * @param Model\Browser             $browser
159
     * @param EndorphinDetector\Browser $resultRaw
160
     */
161 3
    private function hydrateBrowser(Model\Browser $browser, EndorphinDetector\Browser $resultRaw)
162
    {
163 3
        $browser->setName($this->getRealResult($resultRaw->getName()));
164 3
        $browser->getVersion()->setComplete($this->getRealResult($resultRaw->getVersion()));
165 3
    }
166
167
    /**
168
     *
169
     * @param Model\OperatingSystem $os
170
     * @param EndorphinDetector\OS  $resultRaw
171
     */
172 3
    private function hydrateOperatingSystem(Model\OperatingSystem $os, EndorphinDetector\OS $resultRaw)
173
    {
174 3
        $os->setName($this->getRealResult($resultRaw->getName()));
175 3
        $os->getVersion()->setComplete($this->getRealResult($resultRaw->getVersion()));
176 3
    }
177
178
    /**
179
     *
180
     * @param Model\Device             $device
181
     * @param EndorphinDetector\Device $resultRaw
182
     */
183 3
    private function hydrateDevice(Model\Device $device, EndorphinDetector\Device $resultRaw)
184
    {
185
        // $device->setModel($this->getRealResult($resultRaw->ModelName));
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
186 3
        $device->setType($this->getRealResult($resultRaw->getType()));
187 3
    }
188
189 5
    public function parse($userAgent, array $headers = [])
190
    {
191 5
        $resultRaw = $this->getParser($userAgent);
192
193
        /*
194
         * No result found?
195
         */
196 5
        if ($this->hasResult($resultRaw) !== true) {
197 1
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
198
        }
199
200
        /*
201
         * Hydrate the model
202
         */
203 4
        $result = new Model\UserAgent();
204 4
        $result->setProviderResultRaw($resultRaw);
205
206
        /*
207
         * Bot detection
208
         */
209 4
        if ($this->isRealResult($resultRaw->Robot->getType()) === true) {
210 1
            $this->hydrateBot($result->getBot(), $resultRaw->Robot);
211
212 1
            return $result;
213
        }
214
215
        /*
216
         * hydrate the result
217
         */
218 3
        if ($resultRaw->Browser instanceof EndorphinDetector\Browser) {
219 3
            $this->hydrateBrowser($result->getBrowser(), $resultRaw->Browser);
220
        }
221 3
        if ($resultRaw->OS instanceof EndorphinDetector\OS) {
222 3
            $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->OS);
223
        }
224 3
        if ($resultRaw->Device instanceof EndorphinDetector\Device) {
225 3
            $this->hydrateDevice($result->getDevice(), $resultRaw->Device);
226
        }
227
228 3
        return $result;
229
    }
230
}
231