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

WhichBrowser::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 3
Ratio 50 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 3
loc 6
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use UserAgentParser\Exception;
5
use UserAgentParser\Model;
6
use WhichBrowser\Parser as WhichBrowserParser;
7
8
class WhichBrowser extends AbstractProvider
9
{
10
    protected $detectionCapabilities = [
11
12
        'browser' => [
13
            'name'    => true,
14
            'version' => true,
15
        ],
16
17
        'renderingEngine' => [
18
            'name'    => true,
19
            'version' => true,
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'  => false,
33
        ],
34
35
        'bot' => [
36
            'isBot' => true,
37
            'name'  => true,
38
            'type'  => false,
39
        ],
40
    ];
41
42
    /**
43
     * Used for unitTests mocking
44
     *
45
     * @var WhichBrowserParser
46
     */
47
    private $parser;
48
49 13
    public function __construct()
50
    {
51 13 View Code Duplication
        if (! class_exists('WhichBrowser\Parser', 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 12
    }
55
56 1
    public function getName()
57
    {
58 1
        return 'WhichBrowser';
59
    }
60
61 3
    public function getComposerPackageName()
62
    {
63 3
        return 'whichbrowser/parser';
64
    }
65
66
    /**
67
     *
68
     * @param  array              $headers
69
     * @return WhichBrowserParser
70
     */
71 8
    public function getParser(array $headers)
72
    {
73 8
        if ($this->parser !== null) {
74 7
            return $this->parser;
75
        }
76
77 1
        return new WhichBrowserParser($headers);
78
    }
79
80
    /**
81
     *
82
     * @param Model\Bot                   $bot
83
     * @param \WhichBrowser\Model\Browser $browserRaw
84
     */
85 1
    private function hydrateBot(Model\Bot $bot, \WhichBrowser\Model\Browser $browserRaw)
86
    {
87 1
        $bot->setIsBot(true);
88
89 1
        if ($this->isRealResult($browserRaw->getName()) === true) {
90 1
            $bot->setName($browserRaw->getName());
91
        }
92 1
    }
93
94
    /**
95
     *
96
     * @param Model\Browser               $browser
97
     * @param \WhichBrowser\Model\Browser $browserRaw
98
     */
99 5
    private function hydrateBrowser(Model\Browser $browser, \WhichBrowser\Model\Browser $browserRaw)
100
    {
101 5 View Code Duplication
        if ($this->isRealResult($browserRaw->getName()) === 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...
102 1
            $browser->setName($browserRaw->getName());
103
104 1
            if ($this->isRealResult($browserRaw->getVersion()) === true) {
105 1
                $browser->getVersion()->setComplete($browserRaw->getVersion());
106
            }
107
108 1
            return;
109
        }
110
111 4
        if (isset($browserRaw->using) && $browserRaw->using instanceof \WhichBrowser\Model\Using) {
112
            /* @var $usingRaw \WhichBrowser\Model\Using */
113 1
            $usingRaw = $browserRaw->using;
114
115 1 View Code Duplication
            if ($this->isRealResult($usingRaw->getName()) === 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...
116 1
                $browser->setName($usingRaw->getName());
117
118 1
                if ($this->isRealResult($usingRaw->getVersion()) === true) {
119 1
                    $browser->getVersion()->setComplete($usingRaw->getVersion());
120
                }
121
            }
122
        }
123 4
    }
124
125
    /**
126
     *
127
     * @param Model\RenderingEngine      $engine
128
     * @param \WhichBrowser\Model\Engine $engineRaw
129
     */
130 5 View Code Duplication
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, \WhichBrowser\Model\Engine $engineRaw)
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...
131
    {
132 5
        if ($this->isRealResult($engineRaw->getName()) === true) {
133 1
            $engine->setName($engineRaw->getName());
134
        }
135
136 5
        if ($this->isRealResult($engineRaw->getVersion()) === true) {
137 1
            $engine->getVersion()->setComplete($engineRaw->getVersion());
138
        }
139 5
    }
140
141
    /**
142
     *
143
     * @param Model\OperatingSystem  $os
144
     * @param \WhichBrowser\Model\Os $osRaw
145
     */
146 5 View Code Duplication
    private function hydrateOperatingSystem(Model\OperatingSystem $os, \WhichBrowser\Model\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...
147
    {
148 5
        if ($this->isRealResult($osRaw->getName()) === true) {
149 1
            $os->setName($osRaw->getName());
150
        }
151
152 5
        if ($this->isRealResult($osRaw->getVersion()) === true) {
153 1
            $os->getVersion()->setComplete($osRaw->getVersion());
154
        }
155 5
    }
156
157
    /**
158
     *
159
     * @param Model\Device               $device
160
     * @param \WhichBrowser\Model\Device $deviceRaw
161
     * @param WhichBrowserParser         $parser
162
     */
163 5
    private function hydrateDevice(Model\Device $device, \WhichBrowser\Model\Device $deviceRaw, WhichBrowserParser $parser)
164
    {
165 5
        if ($this->isRealResult($deviceRaw->getModel()) === true) {
166 1
            $device->setModel($deviceRaw->getModel());
167
        }
168
169 5
        if ($this->isRealResult($deviceRaw->getManufacturer()) === true) {
170 1
            $device->setBrand($deviceRaw->getManufacturer());
171
        }
172
173 5
        if ($this->isRealResult($parser->getType()) === true) {
174 1
            $device->setType($parser->getType());
175
        }
176
177 5
        if ($parser->isType('mobile', 'tablet', 'ereader', 'media', 'watch', 'camera', 'gaming:portable') === true) {
178 1
            $device->setIsMobile(true);
179
        }
180 5
    }
181
182 7
    public function parse($userAgent, array $headers = [])
183
    {
184 7
        $headers['User-Agent'] = $userAgent;
185
186 7
        $parser = $this->getParser($headers);
187
188
        /*
189
         * No result found?
190
         */
191 7
        if ($parser->isDetected() !== true) {
192 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
193
        }
194
195
        /*
196
         * Hydrate the model
197
         */
198 6
        $result = new Model\UserAgent();
199 6
        $result->setProviderResultRaw($parser->toArray());
200
201
        /*
202
         * Bot detection
203
         */
204 6
        if ($parser->getType() === 'bot') {
205 1
            $this->hydrateBot($result->getBot(), $parser->browser);
206
207 1
            return $result;
208
        }
209
210
        /*
211
         * hydrate the result
212
         */
213 5
        $this->hydrateBrowser($result->getBrowser(), $parser->browser);
214 5
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $parser->engine);
215 5
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $parser->os);
216 5
        $this->hydrateDevice($result->getDevice(), $parser->device, $parser);
217
218 5
        return $result;
219
    }
220
}
221