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 (#63)
by Martin
03:22
created

Zsxsoft   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 30
c 2
b 0
f 0
lcom 1
cbo 9
dl 0
loc 207
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getParser() 0 10 2
B hasResult() 0 20 9
B hydrateBrowser() 0 10 5
B hydrateOperatingSystem() 0 10 5
B hydrateDevice() 0 10 5
B parse() 0 37 2
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use UserAgent;
5
use UserAgentParser\Exception\NoResultFoundException;
6
use UserAgentParser\Exception\PackageNotLoadedException;
7
use UserAgentParser\Model;
8
9
class Zsxsoft extends AbstractProvider
10
{
11
    /**
12
     * Name of the provider
13
     *
14
     * @var string
15
     */
16
    protected $name = 'Zsxsoft';
17
18
    /**
19
     * Homepage of the provider
20
     *
21
     * @var string
22
     */
23
    protected $homepage = 'https://github.com/zsxsoft/php-useragent';
24
25
    /**
26
     * Composer package name
27
     *
28
     * @var string
29
     */
30
    protected $packageName = 'zsxsoft/php-useragent';
31
32
    protected $detectionCapabilities = [
33
34
        'browser' => [
35
            'name'    => true,
36
            'version' => true,
37
        ],
38
39
        'renderingEngine' => [
40
            'name'    => false,
41
            'version' => false,
42
        ],
43
44
        'operatingSystem' => [
45
            'name'    => true,
46
            'version' => true,
47
        ],
48
49
        'device' => [
50
            'model'    => true,
51
            'brand'    => true,
52
            'type'     => false,
53
            'isMobile' => false,
54
            'isTouch'  => false,
55
        ],
56
57
        'bot' => [
58
            'isBot' => false,
59
            'name'  => false,
60
            'type'  => false,
61
        ],
62
    ];
63
64
    protected $defaultValues = [
65
66
        'general' => [
67
            '/^Unknown$/i',
68
        ],
69
    ];
70
71
    private $parser;
72
73
    /**
74
     * @param  UserAgent                 $parser
75
     * @throws PackageNotLoadedException
76
     */
77
    public function __construct(UserAgent $parser = null)
78
    {
79
        if (! file_exists('vendor/' . $this->getPackageName() . '/composer.json')) {
80
            throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider');
81
        }
82
83
        $this->parser = $parser;
84
    }
85
86
    /**
87
     *
88
     * @return UserAgent
89
     */
90
    public function getParser()
91
    {
92
        if ($this->parser !== null) {
93
            return $this->parser;
94
        }
95
96
        $this->parser = new UserAgent();
97
98
        return $this->parser;
99
    }
100
101
    /**
102
     *
103
     * @param array $browser
104
     * @param array $os
105
     * @param array $device
106
     *
107
     * @return bool
108
     */
109
    private function hasResult(array $browser, array $os, array $device)
110
    {
111
        if (isset($browser['name']) && $this->isRealResult($browser['name'])) {
112
            return true;
113
        }
114
115
        if (isset($os['name']) && $this->isRealResult($os['name'])) {
116
            return true;
117
        }
118
119
        if (isset($device['brand']) && $this->isRealResult($device['brand'])) {
120
            return true;
121
        }
122
123
        if (isset($device['model']) && $this->isRealResult($device['model'])) {
124
            return true;
125
        }
126
127
        return false;
128
    }
129
130
    /**
131
     *
132
     * @param Model\Browser $browser
133
     * @param array         $browserRaw
134
     */
135
    private function hydrateBrowser(Model\Browser $browser, array $browserRaw)
136
    {
137
        if (isset($browserRaw['name']) && $this->isRealResult($browserRaw['name']) === true) {
138
            $browser->setName($browserRaw['name']);
139
        }
140
141
        if (isset($browserRaw['version']) && $this->isRealResult($browserRaw['version']) === true) {
142
            $browser->getVersion()->setComplete($browserRaw['version']);
143
        }
144
    }
145
146
    /**
147
     *
148
     * @param Model\OperatingSystem $os
149
     * @param array                 $osRaw
150
     */
151
    private function hydrateOperatingSystem(Model\OperatingSystem $os, array $osRaw)
152
    {
153
        if (isset($osRaw['name']) && $this->isRealResult($osRaw['name']) === true) {
154
            $os->setName($osRaw['name']);
155
        }
156
157
        if (isset($osRaw['version']) && $this->isRealResult($osRaw['version']) === true) {
158
            $os->getVersion()->setComplete($osRaw['version']);
159
        }
160
    }
161
162
    /**
163
     *
164
     * @param Model\Device $device
165
     * @param array        $deviceRaw
166
     */
167
    private function hydrateDevice(Model\Device $device, array $deviceRaw)
168
    {
169
        if (isset($deviceRaw['model']) && $this->isRealResult($deviceRaw['model']) === true) {
170
            $device->setModel($deviceRaw['model']);
171
        }
172
173
        if (isset($deviceRaw['brand']) && $this->isRealResult($deviceRaw['brand']) === true) {
174
            $device->setBrand($deviceRaw['brand']);
175
        }
176
    }
177
178
    public function parse($userAgent, array $headers = [])
179
    {
180
        $parser = $this->getParser();
181
        $parser->analyze($userAgent);
182
183
        $browser  = $parser->browser;
184
        $os       = $parser->os;
185
        $device   = $parser->device;
186
        $platform = $parser->platform;
187
188
        /*
189
         * No result found?
190
         */
191
        if ($this->hasResult($browser, $os, $device) !== true) {
192
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
193
        }
194
195
        /*
196
         * Hydrate the model
197
         */
198
        $result = new Model\UserAgent();
199
        $result->setProviderResultRaw([
200
            'browser'  => $browser,
201
            'os'       => $os,
202
            'device'   => $device,
203
            'platform' => $platform,
204
        ]);
205
206
        /*
207
         * hydrate the result
208
         */
209
        $this->hydrateBrowser($result->getBrowser(), $browser);
210
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $os);
211
        $this->hydrateDevice($result->getDevice(), $device);
212
213
        return $result;
214
    }
215
}
216