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 ( 06960b...53f99c )
by Martin
05:32
created

Woothee::isMobile()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 22
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

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