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 ( 015291...4b7930 )
by François
04:19
created

Response::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
/**
3
 *  Copyright (C) 2017 François Kooman <[email protected]>.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace fkooman\OAuth\Client\Http;
20
21
use RuntimeException;
22
23
class Response
24
{
25
    /** @var int */
26
    private $statusCode;
27
28
    /** @var string */
29
    private $responseBody;
30
31
    /** @var array */
32
    private $responseHeaders;
33
34
    /**
35
     * @param int    $statusCode
36
     * @param string $responseBody
37
     */
38
    public function __construct($statusCode, $responseBody, array $responseHeaders = [])
39
    {
40
        $this->statusCode = $statusCode;
41
        $this->responseBody = $responseBody;
42
        $this->responseHeaders = $responseHeaders;
43
    }
44
45
    public function __toString()
46
    {
47
        $fmtHdrs = '';
48
        foreach ($this->responseHeaders as $k => $v) {
49
            $fmtHdrs .= sprintf('%s: %s', $k, $v).PHP_EOL;
50
        }
51
52
        return implode(
53
            PHP_EOL,
54
            [
55
                $this->statusCode,
56
                '',
57
                $fmtHdrs,
58
                '',
59
                $this->responseBody,
60
            ]
61
        );
62
    }
63
64
    /**
65
     * @return int
66
     */
67
    public function getStatusCode()
68
    {
69
        return $this->statusCode;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getBody()
76
    {
77
        return $this->responseBody;
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public function getHeaders()
84
    {
85
        return $this->responseHeaders;
86
    }
87
88
    /**
89
     * @return string|null
90
     */
91
    public function getHeader($key)
92
    {
93
        foreach ($this->responseHeaders as $k => $v) {
94
            if (strtoupper($key) === strtoupper($k)) {
95
                return $v;
96
            }
97
        }
98
99
        return null;
100
    }
101
102
    /**
103
     * @return mixed
104
     */
105
    public function json()
106
    {
107
        $decodedJson = json_decode($this->responseBody, true);
108
        if (is_null($decodedJson) && JSON_ERROR_NONE !== json_last_error()) {
109
            // XXX better exception!!!
110
            throw new RuntimeException('unable to decode JSON');
111
        }
112
113
        return $decodedJson;
114
    }
115
116
    /**
117
     * @return bool
118
     */
119
    public function isOkay()
120
    {
121
        return 200 <= $this->statusCode && 300 > $this->statusCode;
122
    }
123
}
124