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.

Woothee::hydrateBot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
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
        $this->checkIfInstalled();
100 17
    }
101
102
    /**
103
     *
104
     * @return Classifier
105
     */
106 10
    public function getParser()
107
    {
108 10
        if ($this->parser !== null) {
109 9
            return $this->parser;
110
        }
111
112 1
        $this->parser = new Classifier();
113
114 1
        return $this->parser;
115
    }
116
117
    /**
118
     *
119
     * @param array $resultRaw
120
     *
121
     * @return bool
122
     */
123 9
    private function hasResult(array $resultRaw)
124
    {
125 9
        if (isset($resultRaw['category']) && $this->isRealResult($resultRaw['category'], 'device', 'type')) {
126 5
            return true;
127
        }
128
129 4
        if (isset($resultRaw['name']) && $this->isRealResult($resultRaw['name'])) {
130 1
            return true;
131
        }
132
133 3
        return false;
134
    }
135
136
    /**
137
     *
138
     * @param  array   $resultRaw
139
     * @return boolean
140
     */
141 6
    private function isBot(array $resultRaw)
142
    {
143 6
        if (isset($resultRaw['category']) && $resultRaw['category'] === DataSet::DATASET_CATEGORY_CRAWLER) {
144 3
            return true;
145
        }
146
147 3
        return false;
148
    }
149
150
    /**
151
     *
152
     * @param Model\Bot $bot
153
     * @param array     $resultRaw
154
     */
155 3
    private function hydrateBot(Model\Bot $bot, array $resultRaw)
156
    {
157 3
        $bot->setIsBot(true);
158
159 3
        if (isset($resultRaw['name'])) {
160 3
            $bot->setName($this->getRealResult($resultRaw['name'], 'bot', 'name'));
161
        }
162 3
    }
163
164
    /**
165
     *
166
     * @param Model\Browser $browser
167
     * @param array         $resultRaw
168
     */
169 3
    private function hydrateBrowser(Model\Browser $browser, array $resultRaw)
170
    {
171 3
        if (isset($resultRaw['name'])) {
172 2
            $browser->setName($this->getRealResult($resultRaw['name']));
173
        }
174
175 3
        if (isset($resultRaw['version'])) {
176 1
            $browser->getVersion()->setComplete($this->getRealResult($resultRaw['version']));
177
        }
178 3
    }
179
180
    /**
181
     *
182
     * @param Model\Device $device
183
     * @param array        $resultRaw
184
     */
185 3
    private function hydrateDevice(Model\Device $device, array $resultRaw)
186
    {
187 3
        if (isset($resultRaw['category'])) {
188 2
            $device->setType($this->getRealResult($resultRaw['category'], 'device', 'type'));
189
        }
190 3
    }
191
192 9
    public function parse($userAgent, array $headers = [])
193
    {
194 9
        $parser = $this->getParser();
195
196 9
        $resultRaw = $parser->parse($userAgent);
197
198
        /*
199
         * No result found?
200
         */
201 9
        if ($this->hasResult($resultRaw) !== true) {
202 3
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
203
        }
204
205
        /*
206
         * Hydrate the model
207
         */
208 6
        $result = new Model\UserAgent($this->getName(), $this->getVersion());
209 6
        $result->setProviderResultRaw($resultRaw);
210
211
        /*
212
         * Bot detection
213
         */
214 6
        if ($this->isBot($resultRaw) === true) {
215 3
            $this->hydrateBot($result->getBot(), $resultRaw);
216
217 3
            return $result;
218
        }
219
220
        /*
221
         * hydrate the result
222
         */
223 3
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
224
        // renderingEngine not available
225
        // operatingSystem filled OS is mixed! Examples: iPod, iPhone, Android...
226 3
        $this->hydrateDevice($result->getDevice(), $resultRaw);
227
228 3
        return $result;
229
    }
230
}
231