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 ( 192bb3...47c5f3 )
by Martin
06:04
created

Woothee::setParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
        if (! class_exists('Woothee\Classifier', true)) {
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
     * @return Classifier
69
     */
70 6
    public function getParser()
71
    {
72 6
        if ($this->parser !== null) {
73 5
            return $this->parser;
74
        }
75
76 1
        $this->parser = new Classifier();
77
78 1
        return $this->parser;
79
    }
80
81
    /**
82
     *
83
     * @param array $resultRaw
84
     *
85
     * @return bool
86
     */
87 5
    private function hasResult(array $resultRaw)
88
    {
89 5
        foreach ($resultRaw as $value) {
90 4
            if ($this->isRealResult($value) === true) {
91 4
                return true;
92
            }
93 1
        }
94
95 1
        return false;
96
    }
97
98
    /**
99
     *
100
     * @param  array   $resultRaw
101
     * @return boolean
102
     */
103 4
    private function isBot(array $resultRaw)
104
    {
105 4
        if (isset($resultRaw['category']) && $resultRaw['category'] === DataSet::DATASET_CATEGORY_CRAWLER) {
106 1
            return true;
107
        }
108
109 3
        return false;
110
    }
111
112
    /**
113
     *
114
     * @param Model\Bot $bot
115
     * @param array     $resultRaw
116
     */
117 1
    private function hydrateBot(Model\Bot $bot, array $resultRaw)
118
    {
119 1
        $bot->setIsBot(true);
120
121 1
        if (isset($resultRaw['name']) && $this->isRealResult($resultRaw['name']) === true) {
122 1
            $bot->setName($resultRaw['name']);
123
        }
124 1
    }
125
126
    /**
127
     *
128
     * @param Model\Browser $browser
129
     * @param array         $resultRaw
130
     */
131 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...
132
    {
133 3
        if (isset($resultRaw['name']) && $this->isRealResult($resultRaw['name']) === true) {
134 1
            $browser->setName($resultRaw['name']);
135
        }
136
137 3
        if (isset($resultRaw['version']) && $this->isRealResult($resultRaw['version']) === true) {
138 1
            $browser->getVersion()->setComplete($resultRaw['version']);
139
        }
140 3
    }
141
142
    /**
143
     *
144
     * @param Model\Device $device
145
     * @param array        $resultRaw
146
     */
147 3
    private function hydrateDevice(Model\Device $device, array $resultRaw)
148
    {
149 3
        if (isset($resultRaw['category']) && $this->isRealResult($resultRaw['category']) === true) {
150 2
            $device->setType($resultRaw['category']);
151
        }
152 3
    }
153
154 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...
155
    {
156 5
        $parser = $this->getParser();
157
158 5
        $resultRaw = $parser->parse($userAgent);
159
160
        /*
161
         * No result found?
162
         */
163 5
        if ($this->hasResult($resultRaw) !== true) {
164 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
165
        }
166
167
        /*
168
         * Hydrate the model
169
         */
170 4
        $result = new Model\UserAgent();
171 4
        $result->setProviderResultRaw($resultRaw);
172
173
        /*
174
         * Bot detection
175
         */
176 4
        if ($this->isBot($resultRaw) === true) {
177 1
            $this->hydrateBot($result->getBot(), $resultRaw);
178
179 1
            return $result;
180
        }
181
182
        /*
183
         * hydrate the result
184
         */
185 3
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
186
        // renderingEngine not available
187
        // operatingSystem filled OS is mixed! Examples: iPod, iPhone, Android...
188 3
        $this->hydrateDevice($result->getDevice(), $resultRaw);
189
190 3
        return $result;
191
    }
192
}
193