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 (#59)
by Martin
04:01
created

Woothee::isBot()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

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