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.

ApiProblem::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 5
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 8/03/16
5
 * Time: 22:29.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace NilPortugues\Api\Problem;
12
13
use Exception;
14
use NilPortugues\Assert\Assert;
15
16
/**
17
 * Object describing an API-Problem payload.
18
 */
19
class ApiProblem
20
{
21
    const RFC2616 = 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html';
22
23
    /**
24
     * Title of the error.
25
     *
26
     * @var string
27
     */
28
    protected $title;
29
    /**
30
     * URL describing the problem type; defaults to HTTP status codes.
31
     *
32
     * @var string
33
     */
34
    protected $type = self::RFC2616;
35
36
    /**
37
     * Description of the specific problem.
38
     *
39
     * @var string
40
     */
41
    protected $detail = '';
42
43
    /**
44
     * HTTP status for the error.
45
     *
46
     * @var int
47
     */
48
    protected $status;
49
50
    /**
51
     * @var array
52
     */
53
    protected $additionalDetails = [];
54
55
    /**
56
     * Status titles for common problems.
57
     *
58
     * @var array
59
     */
60
    protected static $problemStatus = [
61
        // CLIENT ERROR
62
        400 => 'Bad Request',
63
        401 => 'Unauthorized',
64
        402 => 'Payment Required',
65
        403 => 'Forbidden',
66
        404 => 'Not Found',
67
        405 => 'Method Not Allowed',
68
        406 => 'Not Acceptable',
69
        407 => 'Proxy Authentication Required',
70
        408 => 'Request Time-out',
71
        409 => 'Conflict',
72
        410 => 'Gone',
73
        411 => 'Length Required',
74
        412 => 'Precondition Failed',
75
        413 => 'Request Entity Too Large',
76
        414 => 'Request-URI Too Large',
77
        415 => 'Unsupported Media Type',
78
        416 => 'Requested range not satisfiable',
79
        417 => 'Expectation Failed',
80
        418 => 'I\'m a teapot',
81
        422 => 'Unprocessable Entity',
82
        423 => 'Locked',
83
        424 => 'Failed Dependency',
84
        425 => 'Unordered Collection',
85
        426 => 'Upgrade Required',
86
        428 => 'Precondition Required',
87
        429 => 'Too Many Requests',
88
        431 => 'Request Header Fields Too Large',
89
        // SERVER ERROR
90
        500 => 'Internal Server Error',
91
        501 => 'Not Implemented',
92
        502 => 'Bad Gateway',
93
        503 => 'Service Unavailable',
94
        504 => 'Gateway Time-out',
95
        505 => 'HTTP Version not supported',
96
        506 => 'Variant Also Negotiates',
97
        507 => 'Insufficient Storage',
98
        508 => 'Loop Detected',
99
        511 => 'Network Authentication Required',
100
    ];
101
102
    /**
103
     * ApiProblem constructor.
104
     *
105
     * @param        $status
106
     * @param        $detail
107
     * @param string $title
108
     * @param string $type
109
     * @param array  $additionalDetails
110
     */
111
    public function __construct($status, $detail, $title = '', $type = '', array $additionalDetails = [])
112
    {
113
        $this->setTitle($title);
114
        $this->setType($type);
115
        $this->setDetail($detail);
116
        $this->setStatus($status);
117
        $this->setAdditionalDetails($additionalDetails);
118
    }
119
120
    /**
121
     * @param Exception $exception
122
     * @param string    $title
123
     * @param string    $type
124
     * @param array     $additionalDetails
125
     *
126
     * @return ApiProblem
127
     */
128
    public static function fromException(Exception $exception, $title = '', $type = '', array $additionalDetails = [])
129
    {
130
        $eCode = $exception->getCode();
131
        $code = (!empty($eCode) && is_int($eCode)) ? $eCode : 500;
132
133
        if (0 === strlen($title) && 0 === strlen($type) && array_key_exists($code, self::$problemStatus)) {
134
            $title = (self::$problemStatus[$code]);
135
            $type = self::RFC2616;
136
        }
137
138
        $detail = $exception->getMessage();
139
        if (empty($detail)) {
140
            $className = explode('\\', get_class($exception));
141
            $detail = array_pop($className);
142
        }
143
144
        return new self($code, $detail, $title, $type, $additionalDetails);
145
    }
146
147
    /**
148
     * Returns value for `additionalDetails`.
149
     *
150
     * @return array
151
     */
152
    public function additionalDetails()
153
    {
154
        return $this->additionalDetails;
155
    }
156
157
    /**
158
     * Sets the value for `additionalDetails` property.
159
     *
160
     * @param array $additionalDetails
161
     */
162
    protected function setAdditionalDetails(array &$additionalDetails)
163
    {
164
        Assert::hasKeyFormat($additionalDetails, function ($key) {
165
            Assert::isAlpha($key, 'Key requires [A-Za-z] characters only.');
166
        });
167
168
        $this->additionalDetails = $additionalDetails;
169
    }
170
171
    /**
172
     * Returns value for `title`.
173
     *
174
     * @return string
175
     */
176
    public function title()
177
    {
178
        return $this->title;
179
    }
180
181
    /**
182
     * Sets the value for `title` property.
183
     *
184
     * @param string $title
185
     */
186
    protected function setTitle($title)
187
    {
188
        $this->title = $title;
189
    }
190
191
    /**
192
     * Returns value for `type`.
193
     *
194
     * @return string
195
     */
196
    public function type()
197
    {
198
        return $this->type;
199
    }
200
201
    /**
202
     * Sets the value for `type` property.
203
     *
204
     * @param string $type
205
     */
206
    protected function setType($type)
207
    {
208
        $this->type = $type;
209
    }
210
211
    /**
212
     * Returns value for `detail`.
213
     *
214
     * @return string
215
     */
216
    public function detail()
217
    {
218
        return $this->detail;
219
    }
220
221
    /**
222
     * Sets the value for `detail` property.
223
     *
224
     * @param string $detail
225
     */
226
    protected function setDetail($detail)
227
    {
228
        Assert::notEmpty($detail, 'Detail field cannot be an empty string');
229
230
        $this->detail = $detail;
231
    }
232
233
    /**
234
     * Returns value for `status`.
235
     *
236
     * @return int
237
     */
238
    public function status()
239
    {
240
        return $this->status;
241
    }
242
243
    /**
244
     * Sets the value for `status` property.
245
     *
246
     * @param int $status
247
     */
248
    protected function setStatus($status)
249
    {
250
        Assert::isInteger($status, 'Status must be an integer value');
251
        Assert::contains(array_keys(self::$problemStatus), $status, false, 'Status must be a valid HTTP 4xx or 5xx value.');
252
253
        $this->status = $status;
254
    }
255
}
256