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

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