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 ( 96f191...a0fbd6 )
by Martin
06:51
created

Woothee::hydrateBrowser()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 4
nop 2
crap 3
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use UserAgentParser\Exception\NoResultFoundException;
5
use UserAgentParser\Exception\PackageNotLoadedException;
6
use UserAgentParser\Model;
7
use Woothee\Classifier;
8
use Woothee\DataSet;
9
10
/**
11
 * Abstraction for woothee/woothee
12
 *
13
 * @author Martin Keckeis <[email protected]>
14
 * @license MIT
15
 * @see https://github.com/woothee/woothee-php
16
 */
17
class Woothee extends AbstractProvider
18
{
19
    /**
20
     * Name of the provider
21
     *
22
     * @var string
23
     */
24
    protected $name = 'Woothee';
25
26
    /**
27
     * Homepage of the provider
28
     *
29
     * @var string
30
     */
31
    protected $homepage = 'https://github.com/woothee/woothee-php';
32
33
    /**
34
     * Composer package name
35
     *
36
     * @var string
37
     */
38
    protected $packageName = 'woothee/woothee';
39
40
    protected $detectionCapabilities = [
41
42
        'browser' => [
43
            'name'    => true,
44
            'version' => true,
45
        ],
46
47
        'renderingEngine' => [
48
            'name'    => false,
49
            'version' => false,
50
        ],
51
52
        'operatingSystem' => [
53
            'name'    => false,
54
            'version' => false,
55
        ],
56
57
        'device' => [
58
            'model'    => false,
59
            'brand'    => false,
60
            'type'     => true,
61
            'isMobile' => false,
62
            'isTouch'  => false,
63
        ],
64
65
        'bot' => [
66
            'isBot' => true,
67
            'name'  => true,
68
            'type'  => false,
69
        ],
70
    ];
71
72
    protected $defaultValues = [
73
74
        'general' => [
75
            '/^UNKNOWN$/i',
76
        ],
77
78
        'device' => [
79
            'type' => [
80
                '/^misc$/i',
81
            ],
82
        ],
83
84
        'bot' => [
85
            'name' => [
86
                '/^misc crawler$/i',
87
            ],
88
        ],
89
    ];
90
91
    private $parser;
92
93
    /**
94
     *
95
     * @throws PackageNotLoadedException
96
     */
97 17
    public function __construct()
98
    {
99 17
        if (! file_exists('vendor/' . $this->getPackageName() . '/composer.json')) {
100 1
            throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider');
101
        }
102 16
    }
103
104
    /**
105
     *
106
     * @return Classifier
107
     */
108 9
    public function getParser()
109
    {
110 9
        if ($this->parser !== null) {
111 8
            return $this->parser;
112
        }
113
114 1
        $this->parser = new Classifier();
115
116 1
        return $this->parser;
117
    }
118
119
    /**
120
     *
121
     * @param array $resultRaw
122
     *
123
     * @return bool
124
     */
125 8
    private function hasResult(array $resultRaw)
126
    {
127 8
        if (isset($resultRaw['category']) && $this->isRealResult($resultRaw['category'], 'device', 'type')) {
128 4
            return true;
129
        }
130
131 4
        if (isset($resultRaw['name']) && $this->isRealResult($resultRaw['name'])) {
132 1
            return true;
133
        }
134
135 3
        return false;
136
    }
137
138
    /**
139
     *
140
     * @param  array   $resultRaw
141
     * @return boolean
142
     */
143 5
    private function isBot(array $resultRaw)
144
    {
145 5
        if (isset($resultRaw['category']) && $resultRaw['category'] === DataSet::DATASET_CATEGORY_CRAWLER) {
146 2
            return true;
147
        }
148
149 3
        return false;
150
    }
151
152
    /**
153
     *
154
     * @param Model\Bot $bot
155
     * @param array     $resultRaw
156
     */
157 2
    private function hydrateBot(Model\Bot $bot, array $resultRaw)
158
    {
159 2
        $bot->setIsBot(true);
160
161 2
        if (isset($resultRaw['name'])) {
162 2
            $bot->setName($this->getRealResult($resultRaw['name'], 'bot', 'name'));
163
        }
164 2
    }
165
166
    /**
167
     *
168
     * @param Model\Browser $browser
169
     * @param array         $resultRaw
170
     */
171 3
    private function hydrateBrowser(Model\Browser $browser, array $resultRaw)
172
    {
173 3
        if (isset($resultRaw['name'])) {
174 2
            $browser->setName($this->getRealResult($resultRaw['name']));
175
        }
176
177 3
        if (isset($resultRaw['version'])) {
178 1
            $browser->getVersion()->setComplete($this->getRealResult($resultRaw['version']));
179
        }
180 3
    }
181
182
    /**
183
     *
184
     * @param Model\Device $device
185
     * @param array        $resultRaw
186
     */
187 3
    private function hydrateDevice(Model\Device $device, array $resultRaw)
188
    {
189 3
        if (isset($resultRaw['category'])) {
190 2
            $device->setType($this->getRealResult($resultRaw['category'], 'device', 'type'));
191
        }
192 3
    }
193
194 8
    public function parse($userAgent, array $headers = [])
195
    {
196 8
        $parser = $this->getParser();
197
198 8
        $resultRaw = $parser->parse($userAgent);
199
200
        /*
201
         * No result found?
202
         */
203 8
        if ($this->hasResult($resultRaw) !== true) {
204 3
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
205
        }
206
207
        /*
208
         * Hydrate the model
209
         */
210 5
        $result = new Model\UserAgent();
211 5
        $result->setProviderResultRaw($resultRaw);
212
213
        /*
214
         * Bot detection
215
         */
216 5
        if ($this->isBot($resultRaw) === true) {
217 2
            $this->hydrateBot($result->getBot(), $resultRaw);
218
219 2
            return $result;
220
        }
221
222
        /*
223
         * hydrate the result
224
         */
225 3
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
226
        // renderingEngine not available
227
        // operatingSystem filled OS is mixed! Examples: iPod, iPhone, Android...
228 3
        $this->hydrateDevice($result->getDevice(), $resultRaw);
229
230 3
        return $result;
231
    }
232
}
233