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 (#44)
by Martin
05:39
created

AbstractProvider::getPackages()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0313

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 7
cts 8
cp 0.875
rs 9.2
cc 4
eloc 8
nc 3
nop 0
crap 4.0313
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use DateTime;
5
use DateTimeZone;
6
use UserAgentParser\Exception;
7
use UserAgentParser\Model;
8
9
abstract class AbstractProvider
10
{
11
    /**
12
     * Name of the provider
13
     *
14
     * @var string
15
     */
16
    protected $name;
17
18
    /**
19
     * Homepage of the provider
20
     *
21
     * @var string
22
     */
23
    protected $homepage;
24
25
    /**
26
     * Composer package name
27
     *
28
     * @var string
29
     */
30
    protected $packageName;
31
32
    /**
33
     * Version string for caching
34
     *
35
     * @var string
36
     */
37
    private $version;
38
39
    /**
40
     * Last update date 
41
     * 
42
     * @var DateTime
43
     */
44
    private $updateDate;
45
46
    /**
47
     * Per default the provider cannot detect anything
48
     * Activate them in $detectionCapabilities
49
     *
50
     * @var array
51
     */
52
    protected $allDetectionCapabilities = [
53
        'browser' => [
54
            'name'    => false,
55
            'version' => false,
56
        ],
57
58
        'renderingEngine' => [
59
            'name'    => false,
60
            'version' => false,
61
        ],
62
63
        'operatingSystem' => [
64
            'name'    => false,
65
            'version' => false,
66
        ],
67
68
        'device' => [
69
            'model'    => false,
70
            'brand'    => false,
71
            'type'     => false,
72
            'isMobile' => false,
73
            'isTouch'  => false,
74
        ],
75
76
        'bot' => [
77
            'isBot' => false,
78
            'name'  => false,
79
            'type'  => false,
80
        ],
81
    ];
82
83
    /**
84
     * Set this in each Provider implementation
85
     *
86
     * @var array
87
     */
88
    protected $detectionCapabilities = [];
89
90
    protected $defaultValues = [];
91
92
    /**
93
     * Return the name of the provider
94
     *
95
     * @return string
96
     */
97 1
    public function getName()
98
    {
99 1
        return $this->name;
100
    }
101
102
    /**
103
     * Get the homepage
104
     *
105
     * @return string
106
     */
107 1
    public function getHomepage()
108
    {
109 1
        return $this->homepage;
110
    }
111
112
    /**
113
     * Get the package name
114
     *
115
     * @return string null
116
     */
117 5
    public function getPackageName()
118
    {
119 5
        return $this->packageName;
120
    }
121
122
    /**
123
     * Return the version of the provider
124
     *
125
     * @return string null
126
     */
127 2
    public function getVersion()
128
    {
129 2
        if ($this->version !== null) {
130 1
            return $this->version;
131
        }
132
133 2
        if ($this->getPackageName() === null) {
134 1
            return;
135
        }
136
137 2
        $packages = $this->getPackages();
138
139 2
        if ($packages === null) {
140 1
            return;
141
        }
142
143 2
        foreach ($packages as $package) {
144 2
            if ($package->name === $this->getPackageName()) {
145 1
                $this->version = $package->version;
146
147 1
                break;
148
            }
149 2
        }
150
151 2
        return $this->version;
152
    }
153
154
    /**
155
     * Get the last change date of the provider
156
     * 
157
     * @return DateTime null
158
     */
159 2
    public function getUpdateDate()
160
    {
161 2
        if ($this->updateDate !== null) {
162 1
            return $this->updateDate;
163
        }
164
165 2
        if ($this->getPackageName() === null) {
166 1
            return;
167
        }
168
169 2
        $packages = $this->getPackages();
170
171 2
        if ($packages === null) {
172 1
            return;
173
        }
174
175 2
        foreach ($packages as $package) {
176 2
            if ($package->name === $this->getPackageName()) {
177 1
                $updateDate = new DateTime($package->time, new DateTimeZone('UTC'));
178
179 1
                $this->updateDate = $updateDate;
180
181 1
                break;
182
            }
183 2
        }
184
185 2
        return $this->updateDate;
186
    }
187
188
    /**
189
     *
190
     * @return array null
191
     */
192 4
    private function getPackages()
193
    {
194 4
        if (! file_exists('composer.lock')) {
195 2
            return;
196
        }
197
198 4
        $content = file_get_contents('composer.lock');
199 4
        if ($content === false || $content === '') {
200
            return;
201
        }
202
203 4
        $content = json_decode($content);
204
205 4
        return array_merge($content->packages, $content->{'packages-dev'});
206
    }
207
208
    /**
209
     * What kind of capabilities this provider can detect
210
     *
211
     * @return array
212
     */
213 1
    public function getDetectionCapabilities()
214
    {
215 1
        return array_merge($this->allDetectionCapabilities, $this->detectionCapabilities);
216
    }
217
218
    /**
219
     *
220
     * @param  mixed   $value
221
     * @param  array   $additionalDefaultValues
222
     * @return boolean
223
     */
224 2
    protected function isRealResult($value, array $additionalDefaultValues = [])
225
    {
226 2
        if ($value === '' || $value === null) {
227 1
            return false;
228
        }
229
230 2
        $value = (string) $value;
231
232 2
        $defaultValues = array_merge($this->defaultValues, $additionalDefaultValues);
233
234 2
        if (in_array($value, $defaultValues, true) === true) {
235 1
            return false;
236
        }
237
238 2
        return true;
239
    }
240
241
    /**
242
     * Parse the given user agent and return a result if possible
243
     *
244
     * @param string $userAgent
245
     * @param array  $headers
246
     *
247
     * @throws Exception\NoResultFoundException
248
     *
249
     * @return Model\UserAgent
250
     */
251
    abstract public function parse($userAgent, array $headers = []);
252
}
253