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 ( 89fb33...cd256e )
by Ema
02:19
created

HttpApi::httpPostRaw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
crap 1
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 401
    public function __construct(
39
        HttpClient $httpClient,
40
        MessageFactory $messageFactory,
41
        Hydrator $hydrator = null
42
    ) {
43 401
        $this->httpClient = $httpClient;
44 401
        $this->messageFactory = $messageFactory;
45 401
        $this->hydrator = $hydrator ?: new ModelHydrator();
46 401
    }
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 138
    protected function httpGet(string $path, array $params = [], array $requestHeaders = []): ResponseInterface
58
    {
59 138
        if (count($params) > 0) {
60 6
            $path .= '?'.http_build_query($params);
61
        }
62
63 138
        return $this->httpClient->sendRequest(
64 138
            $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 88
    protected function httpPost(string $path, array $params = [], array $pathParams = [], array $requestHeaders = []): ResponseInterface
79
    {
80 88
        $body = $this->createJsonBody($params);
81 88
        $path .= $this->buildPathParams($pathParams);
82
83 88
        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 88
    protected function httpPostRaw(string $path, $body, array $requestHeaders = []): ResponseInterface
96
    {
97 88
        return $this->httpClient->sendRequest(
98 88
            $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 64
    protected function httpPut(string $path, array $params = [], array $requestHeaders = []): ResponseInterface
112
    {
113 64
        return $this->httpClient->sendRequest(
114 64
            $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 339
    protected function handleResponse(ResponseInterface $response, $class)
145
    {
146 339
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
147 294
            $this->handleErrors($response);
148
        }
149
150 45
        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 308
    protected function handleErrors(ResponseInterface $response)
163
    {
164 308
        $error = null;
165
        // We only hydrate the Error response if the hydrator is a Model one
166 308
        if ($this->hydrator instanceof ModelHydrator) {
167
            $error = $this->hydrator->hydrate($response, Error::class);
168
        }
169
170 308
        switch ($response->getStatusCode()) {
171 308
            case 400:
172 44
                throw new DomainExceptions\ValidationException($response, $error);
173 264
            case 401:
174 44
                throw new DomainExceptions\MissingAccessTokenException($response, $error);
175 220
            case 403:
176 44
                throw new DomainExceptions\PermissionDeniedException($response, $error);
177 176
            case 404:
178 44
                throw new DomainExceptions\NotFoundException($response, $error);
179 132
            case 501:
180 44
                throw new DomainExceptions\DisabledFeatureException($response, $error);
181
            default:
182 88
                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 201
    private function createJsonBody(array $params)
194
    {
195 201
        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 137
    private function buildPathParams(array $params)
206
    {
207 137
        if (count($params) > 0) {
208 1
            return '?'.http_build_query($params);
209
        }
210 136
    }
211
}
212