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.

HttpApi   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 44
c 2
b 0
f 0
dl 0
loc 170
ccs 50
cts 50
cp 1
rs 10
wmc 22

10 Methods

Rating   Name   Duplication   Size   Complexity  
A buildPathParams() 0 7 2
A httpGet() 0 6 1
A httpPost() 0 6 1
A httpPostRaw() 0 4 1
A handleResponse() 0 8 3
A httpPut() 0 4 1
A createJsonBody() 0 3 3
A httpDelete() 0 6 1
B handleErrors() 0 21 7
A __construct() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pnz\MattermostClient\Api;
6
7
use Http\Client\HttpClient;
8
use Http\Message\RequestFactory;
9
use Pnz\JsonException\Json;
10
use Pnz\MattermostClient\Exception\ApiException;
11
use Pnz\MattermostClient\Exception\Domain as DomainExceptions;
12
use Pnz\MattermostClient\Hydrator\Hydrator;
13
use Pnz\MattermostClient\Hydrator\ModelHydrator;
14
use Pnz\MattermostClient\Model\Error;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\StreamInterface;
17
18
abstract class HttpApi
19
{
20
    /**
21
     * @var RequestFactory
22
     */
23
    protected $requestFactory;
24
    /**
25
     * @var HttpClient
26
     */
27
    private $httpClient;
28
29
    /**
30
     * @var Hydrator
31
     */
32
    private $hydrator;
33
34 496
    public function __construct(
35
        HttpClient $httpClient,
36
        RequestFactory $messageFactory,
37
        Hydrator $hydrator = null
38
    ) {
39 496
        $this->httpClient = $httpClient;
40 496
        $this->requestFactory = $messageFactory;
41 496
        $this->hydrator = $hydrator ?: new ModelHydrator();
42 496
    }
43
44
    /**
45
     * Send a GET request with query parameters.
46
     *
47
     * @param string $path           Request path
48
     * @param array  $params         GET parameters
49
     * @param array  $requestHeaders Request Headers
50
     */
51 178
    protected function httpGet(string $path, array $params = [], array $requestHeaders = []): ResponseInterface
52
    {
53 178
        $path .= self::buildPathParams($params);
54
55 178
        return $this->httpClient->sendRequest(
56 178
            $this->requestFactory->createRequest('GET', $path, $requestHeaders)
57
        );
58
    }
59
60
    /**
61
     * Send a POST request with JSON-encoded parameters.
62
     *
63
     * @param string $path           Request path
64
     * @param array  $params         POST parameters to be JSON encoded
65
     * @param array  $pathParams     Params added to the path, as query parameters
66
     * @param array  $requestHeaders Request headers
67
     */
68 104
    protected function httpPost(string $path, array $params = [], array $pathParams = [], array $requestHeaders = []): ResponseInterface
69
    {
70 104
        $body = $this->createJsonBody($params);
71 104
        $path .= self::buildPathParams($pathParams);
72
73 104
        return $this->httpPostRaw($path, $body, $requestHeaders);
74
    }
75
76
    /**
77
     * Send a POST request with raw data.
78
     *
79
     * @param string                      $path           Request path
80
     * @param StreamInterface|string|null $body           Request body
81
     * @param array                       $requestHeaders Request headers
82
     */
83 104
    protected function httpPostRaw(string $path, $body, array $requestHeaders = []): ResponseInterface
84
    {
85 104
        return $this->httpClient->sendRequest(
86 104
            $this->requestFactory->createRequest('POST', $path, $requestHeaders, $body)
87
        );
88
    }
89
90
    /**
91
     * Send a PUT request with JSON-encoded parameters.
92
     *
93
     * @param string $path           Request path
94
     * @param array  $params         POST parameters to be JSON encoded
95
     * @param array  $requestHeaders Request headers
96
     */
97 88
    protected function httpPut(string $path, array $params = [], array $requestHeaders = []): ResponseInterface
98
    {
99 88
        return $this->httpClient->sendRequest(
100 88
            $this->requestFactory->createRequest('PUT', $path, $requestHeaders, $this->createJsonBody($params))
101
        );
102
    }
103
104
    /**
105
     * Send a DELETE request with JSON-encoded parameters.
106
     *
107
     * @param string $path           Request path
108
     * @param array  $params         POST parameters to be JSON encoded
109
     * @param array  $pathParams     URL parameters, used as query string
110
     * @param array  $requestHeaders Request headers
111
     */
112 49
    protected function httpDelete(string $path, array $params = [], array $pathParams = [], array $requestHeaders = []): ResponseInterface
113
    {
114 49
        $path .= self::buildPathParams($pathParams);
115
116 49
        return $this->httpClient->sendRequest(
117 49
            $this->requestFactory->createRequest('DELETE', $path, $requestHeaders, $this->createJsonBody($params))
118
        );
119
    }
120
121
    /**
122
     * Handle responses from the endpoint: handle errors and hydrations.
123
     *
124
     * @param ResponseInterface $response The request response
125
     * @param string            $class    The Class to hydrate the response
126
     *
127
     * @return mixed Hydration return data
128
     */
129 396
    protected function handleResponse(ResponseInterface $response, $class)
130
    {
131 396
        $returnCode = $response->getStatusCode();
132 396
        if (200 !== $returnCode && 201 !== $returnCode) {
133 343
            $this->handleErrors($response);
134
        }
135
136 53
        return $this->hydrator->hydrate($response, $class);
137
    }
138
139
    /**
140
     * Handle HTTP errors.
141
     *
142
     * Call is controlled by the specific API methods.
143
     *
144
     * @throws ApiException
145
     */
146 378
    protected function handleErrors(ResponseInterface $response): void
147
    {
148 378
        $error = null;
149
        // We only hydrate the Error response if the hydrator is a Model one
150 378
        if ($this->hydrator instanceof ModelHydrator) {
151 7
            $error = $this->hydrator->hydrate($response, Error::class);
152
        }
153
154 378
        switch ($response->getStatusCode()) {
155 378
            case 400:
156 54
                throw new DomainExceptions\ValidationException($response, $error);
157 324
            case 401:
158 54
                throw new DomainExceptions\MissingAccessTokenException($response, $error);
159 270
            case 403:
160 54
                throw new DomainExceptions\PermissionDeniedException($response, $error);
161 216
            case 404:
162 54
                throw new DomainExceptions\NotFoundException($response, $error);
163 162
            case 501:
164 54
                throw new DomainExceptions\DisabledFeatureException($response, $error);
165
            default:
166 108
                throw new ApiException($response, $error);
167
        }
168
    }
169
170
    /**
171
     * Create a JSON encoded version of an array of parameters.
172
     */
173 241
    private function createJsonBody(array $params): ?string
174
    {
175 241
        return (0 === \count($params)) ? null : Json::encode($params, empty($params) ? JSON_FORCE_OBJECT : 0);
176
    }
177
178
    /**
179
     * Returns the Query string for the given parameters.
180
     */
181 331
    private static function buildPathParams(array $params): string
182
    {
183 331
        if (\count($params) > 0) {
184 7
            return '?'.http_build_query($params);
185
        }
186
187 324
        return '';
188
    }
189
}
190