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 ( cd256e...747ec6 )
by Ema
03:19
created

HttpApi::handleErrors()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 16
cts 16
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 17
nc 12
nop 1
crap 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pnz\MattermostClient\Api;
6
7
use Http\Client\HttpClient;
8
use Http\Message\MessageFactory;
9
use Pnz\MattermostClient\Exception\Domain as DomainExceptions;
10
use Pnz\MattermostClient\Exception\GenericApiException;
11
use Pnz\MattermostClient\Hydrator\Hydrator;
12
use Pnz\MattermostClient\Hydrator\ModelHydrator;
13
use Pnz\MattermostClient\Model\Error;
14
use Psr\Http\Message\ResponseInterface;
15
16
abstract class HttpApi
17
{
18
    /**
19
     * @var HttpClient
20
     */
21
    protected $httpClient;
22
23
    /**
24
     * @var Hydrator
25
     */
26
    protected $hydrator;
27
28
    /**
29
     * @var MessageFactory
30
     */
31
    protected $messageFactory;
32
33
    /**
34
     * @param HttpClient     $httpClient
35
     * @param MessageFactory $messageFactory
36
     * @param Hydrator       $hydrator
37
     */
38 467
    public function __construct(
39
        HttpClient $httpClient,
40
        MessageFactory $messageFactory,
41
        Hydrator $hydrator = null
42
    ) {
43 467
        $this->httpClient = $httpClient;
44 467
        $this->messageFactory = $messageFactory;
45 467
        $this->hydrator = $hydrator ?: new ModelHydrator();
46 467
    }
47
48
    /**
49
     * Send a GET request with query parameters.
50
     *
51
     * @param string $path           Request path
52
     * @param array  $params         GET parameters
53
     * @param array  $requestHeaders Request Headers
54
     *
55
     * @return ResponseInterface
56
     */
57 162
    protected function httpGet(string $path, array $params = [], array $requestHeaders = []): ResponseInterface
58
    {
59 162
        if (count($params) > 0) {
60 6
            $path .= '?'.http_build_query($params);
61
        }
62
63 162
        return $this->httpClient->sendRequest(
64 162
            $this->messageFactory->createRequest('GET', $path, $requestHeaders)
65
        );
66
    }
67
68
    /**
69
     * Send a POST request with JSON-encoded parameters.
70
     *
71
     * @param string $path           Request path
72
     * @param array  $params         POST parameters to be JSON encoded
73
     * @param array  $pathParams     Params added to the path, as query parameters
74
     * @param array  $requestHeaders Request headers
75
     *
76
     * @return ResponseInterface
77
     */
78 96
    protected function httpPost(string $path, array $params = [], array $pathParams = [], array $requestHeaders = []): ResponseInterface
79
    {
80 96
        $body = $this->createJsonBody($params);
81 96
        $path .= $this->buildPathParams($pathParams);
82
83 96
        return $this->httpPostRaw($path, $body, $requestHeaders);
84
    }
85
86
    /**
87
     * Send a POST request with raw data.
88
     *
89
     * @param string       $path           Request path
90
     * @param array|string $body           Request body
91
     * @param array        $requestHeaders Request headers
92
     *
93
     * @return ResponseInterface
94
     */
95 96
    protected function httpPostRaw(string $path, $body, array $requestHeaders = []): ResponseInterface
96
    {
97 96
        return $this->httpClient->sendRequest(
98 96
            $this->messageFactory->createRequest('POST', $path, $requestHeaders, $body)
0 ignored issues
show
Bug introduced by
It seems like $body defined by parameter $body on line 95 can also be of type array; however, Http\Message\RequestFactory::createRequest() does only seem to accept resource|string|object<P...e\StreamInterface>|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
99
        );
100
    }
101
102
    /**
103
     * Send a PUT request with JSON-encoded parameters.
104
     *
105
     * @param string $path           Request path
106
     * @param array  $params         POST parameters to be JSON encoded
107
     * @param array  $requestHeaders Request headers
108
     *
109
     * @return ResponseInterface
110
     */
111 88
    protected function httpPut(string $path, array $params = [], array $requestHeaders = []): ResponseInterface
112
    {
113 88
        return $this->httpClient->sendRequest(
114 88
            $this->messageFactory->createRequest('PUT', $path, $requestHeaders, $this->createJsonBody($params))
115
        );
116
    }
117
118
    /**
119
     * Send a DELETE request with JSON-encoded parameters.
120
     *
121
     * @param string $path           Request path
122
     * @param array  $params         POST parameters to be JSON encoded
123
     * @param array  $requestHeaders Request headers
124
     *
125
     * @return ResponseInterface
126
     */
127 49
    protected function httpDelete(string $path, array $params = [], array $pathParams = [], array $requestHeaders = []): ResponseInterface
128
    {
129 49
        $path .= $this->buildPathParams($pathParams);
130
131 49
        return $this->httpClient->sendRequest(
132 49
            $this->messageFactory->createRequest('DELETE', $path, $requestHeaders, $this->createJsonBody($params))
133
        );
134
    }
135
136
    /**
137
     * Handle responses from the endpoint: handle errors and hydrations.
138
     *
139
     * @param ResponseInterface $response The request response
140
     * @param string            $class    The Class to hydrate the response
141
     *
142
     * @return mixed Hydration return data
143
     */
144 371
    protected function handleResponse(ResponseInterface $response, $class)
145
    {
146 371
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
147 322
            $this->handleErrors($response);
148
        }
149
150 49
        return $this->hydrator->hydrate($response, $class);
151
    }
152
153
    /**
154
     * Handle HTTP errors.
155
     *
156
     * Call is controlled by the specific API methods.
157
     *
158
     * @param ResponseInterface $response
159
     *
160
     * @throws GenericApiException
161
     */
162 357
    protected function handleErrors(ResponseInterface $response)
163
    {
164 357
        $error = null;
165
        // We only hydrate the Error response if the hydrator is a Model one
166 357
        if ($this->hydrator instanceof ModelHydrator) {
167 7
            $error = $this->hydrator->hydrate($response, Error::class);
168
        }
169
170 357
        switch ($response->getStatusCode()) {
171 357
            case 400:
172 51
                throw new DomainExceptions\ValidationException($response, $error);
173 306
            case 401:
174 51
                throw new DomainExceptions\MissingAccessTokenException($response, $error);
175 255
            case 403:
176 51
                throw new DomainExceptions\PermissionDeniedException($response, $error);
177 204
            case 404:
178 51
                throw new DomainExceptions\NotFoundException($response, $error);
179 153
            case 501:
180 51
                throw new DomainExceptions\DisabledFeatureException($response, $error);
181
            default:
182 102
                throw new GenericApiException($response, $error);
183
        }
184
    }
185
186
    /**
187
     * Create a JSON encoded version of an array of parameters.
188
     *
189
     * @param array $params Request parameters
190
     *
191
     * @return null|string
192
     */
193 233
    private function createJsonBody(array $params)
194
    {
195 233
        return (count($params) === 0) ? null : json_encode($params, empty($params) ? JSON_FORCE_OBJECT : 0);
196
    }
197
198
    /**
199
     * Builds the Query string given the parameters, null if no parameters are provided.
200
     *
201
     * @param array $params
202
     *
203
     * @return string
204
     */
205 145
    private function buildPathParams(array $params)
206
    {
207 145
        if (count($params) > 0) {
208 1
            return '?'.http_build_query($params);
209
        }
210 144
    }
211
}
212