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
10:53 queued 03:22
created

PiwikDeviceDetector::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 3
Ratio 50 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 3
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use DeviceDetector\DeviceDetector;
5
use UserAgentParser\Exception;
6
use UserAgentParser\Model;
7
8
class PiwikDeviceDetector extends AbstractProvider
9
{
10
    protected $detectionCapabilities = [
11
12
        'browser' => [
13
            'name'    => true,
14
            'version' => true,
15
        ],
16
17
        'renderingEngine' => [
18
            'name'    => true,
19
            'version' => false,
20
        ],
21
22
        'operatingSystem' => [
23
            'name'    => true,
24
            'version' => true,
25
        ],
26
27
        'device' => [
28
            'model'    => true,
29
            'brand'    => true,
30
            'type'     => true,
31
            'isMobile' => true,
32
            'isTouch'  => true,
33
        ],
34
35
        'bot' => [
36
            'isBot' => true,
37
            'name'  => true,
38
            'type'  => true,
39
        ],
40
    ];
41
42
    protected $defaultValues = [
43
        DeviceDetector::UNKNOWN,
44
    ];
45
46
    /**
47
     *
48
     * @var DeviceDetector
49
     */
50
    private $parser;
51
52 1
    public function __construct()
53
    {
54 1 View Code Duplication
        if (! class_exists('DeviceDetector\Cache\StaticCache', 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...
55
            throw new Exception\PackageNotLoaded('You need to install ' . $this->getComposerPackageName() . ' to use this provider');
56
        }
57 2
    }
58
59 2
    public function getName()
60
    {
61
        return 'PiwikDeviceDetector';
62
    }
63
64
    public function getComposerPackageName()
65
    {
66 6
        return 'piwik/device-detector';
67
    }
68 6
69 6
    /**
70
     *
71
     * @param DeviceDetector $parser
72
     */
73
    public function setParser(DeviceDetector $parser = null)
74
    {
75 6
        $this->parser = $parser;
76
    }
77 6
78 6
    /**
79
     *
80
     * @return DeviceDetector
81 1
     */
82
    public function getParser()
83 1
    {
84
        if ($this->parser !== null) {
85
            return $this->parser;
86
        }
87
88
        $this->parser = new DeviceDetector();
89
90
        return $this->parser;
91
    }
92 4
93
    /**
94
     *
95 4
     * @param DeviceDetector $dd
96 4
     *
97
     * @return array
98
     */
99 4
    private function getResultRaw(DeviceDetector $dd)
100 4
    {
101
        $raw = [
102 4
            'client'          => $dd->getClient(),
103
            'operatingSystem' => $dd->getOs(),
104 4
105 4
            'device' => [
106 4
                'brand'     => $dd->getBrand(),
107
                'brandName' => $dd->getBrandName(),
108 4
109
                'model' => $dd->getModel(),
110
111 4
                'device'     => $dd->getDevice(),
112
                'deviceName' => $dd->getDeviceName(),
113
            ],
114 4
115 4
            'bot' => $dd->getBot(),
116 4
117 4
            'extra' => [
118 4
                'isBot' => $dd->isBot(),
119 4
120
                // client
121
                'isBrowser'     => $dd->isBrowser(),
122 4
                'isFeedReader'  => $dd->isFeedReader(),
123 4
                'isMobileApp'   => $dd->isMobileApp(),
124 4
                'isPIM'         => $dd->isPIM(),
125 4
                'isLibrary'     => $dd->isLibrary(),
126 4
                'isMediaPlayer' => $dd->isMediaPlayer(),
127 4
128 4
                // deviceType
129 4
                'isCamera'              => $dd->isCamera(),
130 4
                'isCarBrowser'          => $dd->isCarBrowser(),
131 4
                'isConsole'             => $dd->isConsole(),
132
                'isFeaturePhone'        => $dd->isFeaturePhone(),
133
                'isPhablet'             => $dd->isPhablet(),
134 4
                'isPortableMediaPlayer' => $dd->isPortableMediaPlayer(),
135 4
                'isSmartDisplay'        => $dd->isSmartDisplay(),
136 4
                'isSmartphone'          => $dd->isSmartphone(),
137 4
                'isTablet'              => $dd->isTablet(),
138 4
                'isTV'                  => $dd->isTV(),
139
140 4
                // other special
141
                'isDesktop'      => $dd->isDesktop(),
142
                'isMobile'       => $dd->isMobile(),
143
                'isTouchEnabled' => $dd->isTouchEnabled(),
144
            ],
145
        ];
146
147
        return $raw;
148
    }
149 5
150
    /**
151 5
     *
152 1
     * @param DeviceDetector $dd
153
     *
154
     * @return bool
155 4
     */
156 4
    private function hasResult(DeviceDetector $dd)
157 1
    {
158
        if ($dd->isBot() === true) {
159
            return true;
160 3
        }
161 3
162 1
        $client = $dd->getClient();
163
        if (isset($client['name']) && $this->isRealResult($client['name'])) {
164
            return true;
165 2
        }
166 1
167
        $os = $dd->getOs();
168
        if (isset($os['name']) && $this->isRealResult($os['name'])) {
169 1
            return true;
170
        }
171
172
        if ($dd->getDevice() !== null) {
173
            return true;
174
        }
175
176
        return false;
177 1
    }
178
179 1
    /**
180
     *
181 1
     * @param Model\Bot     $bot
182 1
     * @param array|boolean $botRaw
183
     */
184 1 View Code Duplication
    private function hydrateBot(Model\Bot $bot, $botRaw)
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...
185 1
    {
186
        $bot->setIsBot(true);
187 1
188
        if (isset($botRaw['name']) && $this->isRealResult($botRaw['name'])) {
189
            $bot->setName($botRaw['name']);
190
        }
191
        if (isset($botRaw['category']) && $this->isRealResult($botRaw['category'])) {
192
            $bot->setType($botRaw['category']);
193
        }
194 3
    }
195
196 3
    /**
197 1
     *
198
     * @param Model\Browser $browser
199
     * @param array|string  $clientRaw
200 3
     */
201 1 View Code Duplication
    private function hydrateBrowser(Model\Browser $browser, $clientRaw)
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...
202
    {
203 3
        if (isset($clientRaw['name']) && $this->isRealResult($clientRaw['name']) === true) {
204
            $browser->setName($clientRaw['name']);
205
        }
206
207
        if (isset($clientRaw['version']) && $this->isRealResult($clientRaw['version']) === true) {
208
            $browser->getVersion()->setComplete($clientRaw['version']);
209
        }
210 3
    }
211
212 3
    /**
213 1
     *
214
     * @param Model\RenderingEngine $engine
215 3
     * @param array|string          $clientRaw
216
     */
217
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, $clientRaw)
218
    {
219
        if (isset($clientRaw['engine']) && $this->isRealResult($clientRaw['engine']) === true) {
220
            $engine->setName($clientRaw['engine']);
221
        }
222 3
    }
223
224 3
    /**
225 1
     *
226
     * @param Model\OperatingSystem $os
227
     * @param array|string          $osRaw
228 3
     */
229 1 View Code Duplication
    private function hydrateOperatingSystem(Model\OperatingSystem $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...
230
    {
231 3
        if (isset($osRaw['name']) && $this->isRealResult($osRaw['name']) === true) {
232
            $os->setName($osRaw['name']);
233
        }
234
235
        if (isset($osRaw['version']) && $this->isRealResult($osRaw['version']) === true) {
236
            $os->getVersion()->setComplete($osRaw['version']);
237
        }
238 3
    }
239
240 3
    /**
241 1
     *
242
     * @param Model\UserAgent $device
243
     * @param DeviceDetector  $dd
244 3
     */
245 1
    private function hydrateDevice(Model\Device $device, DeviceDetector $dd)
246
    {
247
        if ($this->isRealResult($dd->getModel()) === true) {
248 3
            $device->setModel($dd->getModel());
249 1
        }
250
251
        if ($this->isRealResult($dd->getBrandName()) === true) {
252 3
            $device->setBrand($dd->getBrandName());
253 1
        }
254
255
        if ($this->isRealResult($dd->getDeviceName()) === true) {
256 3
            $device->setType($dd->getDeviceName());
257 1
        }
258
259 3
        if ($dd->isMobile() === true) {
260
            $device->setIsMobile(true);
261 5
        }
262
263 5
        if ($dd->isTouchEnabled() === true) {
264
            $device->setIsTouch(true);
265 5
        }
266 5
    }
267
268
    public function parse($userAgent, array $headers = [])
269
    {
270
        $dd = $this->getParser();
271 5
272 1
        $dd->setUserAgent($userAgent);
273
        $dd->parse();
274
275
        /*
276
         * No result found?
277
         */
278 4
        if ($this->hasResult($dd) !== true) {
279 4
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
280
        }
281
282
        /*
283
         * Hydrate the model
284 4
         */
285 1
        $result = new Model\UserAgent();
286
        $result->setProviderResultRaw($this->getResultRaw($dd));
287 1
288
        /*
289
         * Bot detection
290
         */
291
        if ($dd->isBot() === true) {
292
            $this->hydrateBot($result->getBot(), $dd->getBot());
293 3
294 3
            return $result;
295 3
        }
296 3
297
        /*
298 3
         * hydrate the result
299
         */
300
        $this->hydrateBrowser($result->getBrowser(), $dd->getClient());
301
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $dd->getClient());
302
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $dd->getOs());
303
        $this->hydrateDevice($result->getDevice(), $dd);
304
305
        return $result;
306
    }
307
}
308