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

PiwikDeviceDetector::getComposerPackageName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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