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 ( c3b60f...35c3e9 )
by Niels
04:43
created

Client::compileParameters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php namespace Ntholenaar\MultiSafepayClient;
2
3
use Http\Client\Common\Plugin;
4
use Http\Client\Common\PluginClient;
5
use Http\Client\HttpClient;
6
use Http\Discovery;
7
use Http\Message\UriFactory;
8
use Ntholenaar\MultiSafepayClient\Exception\InvalidRequestException;
9
use Ntholenaar\MultiSafepayClient\Http\Plugin\Authentication;
10
use Ntholenaar\MultiSafepayClient\Http\Plugin\PrependPathPlugin;
11
use Ntholenaar\MultiSafepayClient\Request\GatewayRequest;
12
use Ntholenaar\MultiSafepayClient\Request\IssuerRequest;
13
use Psr\Http\Message\RequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
16
class Client implements ClientInterface
17
{
18
    /**
19
     * Optional ISO 639-1 language code.
20
     *
21
     * The localization parameter determines the language which is used to
22
     * display gateway information and other messages in the responses.
23
     * The default language is English.
24
     *
25
     * @var string
26
     */
27
    protected $locale;
28
29
    /**
30
     * @var bool
31
     */
32
    protected $testMode = false;
33
34
    /**
35
     * @var HttpClient
36
     */
37
    protected $httpClient;
38
39
    /**
40
     * @var PluginClient
41
     */
42
    protected $pluginClient;
43
44
    /**
45
     * @var UriFactory
46
     */
47
    protected $uriFactory;
48
49
    /**
50
     * @var array
51
     */
52
    protected $plugins = array();
53
54
    /**
55
     * @param HttpClient $httpClient
56
     * @param UriFactory|null $uriFactory
57
     */
58
    public function __construct(HttpClient $httpClient = null, UriFactory $uriFactory = null)
59
    {
60
        if (is_null($httpClient)) {
61
            $httpClient = Discovery\HttpClientDiscovery::find();
62
        }
63
64
        if (is_null($uriFactory)) {
65
            $uriFactory = Discovery\UriFactoryDiscovery::find();
66
        }
67
68
        $this->setHttpClient($httpClient);
69
70
        $this->setUriFactory($uriFactory);
71
    }
72
73
    /**
74
     * Get the API endpoint.
75
     *
76
     * @return \Psr\Http\Message\UriInterface
77
     */
78
    public function getApiEndpoint()
79
    {
80
        if ($this->isTestModeEnabled()) {
81
            return $this->uriFactory->createUri('https://testapi.multisafepay.com');
82
        }
83
84
        return $this->uriFactory->createUri('https://api.multisafepay.com');
85
    }
86
87
    /**
88
     * Get the API path.
89
     *
90
     * @return \Psr\Http\Message\UriInterface
91
     */
92
    public function getApiPath()
93
    {
94
        return $this->uriFactory->createUri('/v1/json/');
95
    }
96
97
    /**
98
     * Get the UriFactory.
99
     *
100
     * @return UriFactory
101
     */
102
    protected function getUriFactory()
103
    {
104
        return $this->uriFactory;
105
    }
106
107
    /**
108
     * Set the UriFactory.
109
     *
110
     * @param UriFactory $uriFactory
111
     * @return $this
112
     */
113
    protected function setUriFactory(UriFactory $uriFactory)
114
    {
115
        $this->uriFactory = $uriFactory;
116
117
        return $this;
118
    }
119
120
    /**
121
     * Get the HttpClient.
122
     *
123
     * @return HttpClient
124
     */
125
    protected function getHttpClient()
126
    {
127
        return $this->httpClient;
128
    }
129
130
    /**
131
     * Set the HttpClient.
132
     *
133
     * @param HttpClient $httpClient
134
     * @return $this
135
     */
136
    protected function setHttpClient(HttpClient $httpClient)
137
    {
138
        $this->httpClient = $httpClient;
139
140
        return $this;
141
    }
142
143
    /**
144
     * Get the locale.
145
     *
146
     * @return string
147
     */
148
    public function getLocale()
149
    {
150
        return $this->locale;
151
    }
152
153
    /**
154
     * Set the locale.
155
     *
156
     * @param $locale
157
     * @return $this
158
     */
159
    public function setLocale($locale)
160
    {
161
        $this->locale = $locale;
162
163
        return $this;
164
    }
165
166
    /**
167
     * Enable or disable the test environment.
168
     *
169
     * @param $testMode
170
     * @return $this
171
     */
172
    public function setTestMode($testMode)
173
    {
174
        $this->testMode = $testMode;
175
176
        return $this;
177
    }
178
179
    /**
180
     * Is the test environment active.
181
     *
182
     * @return bool
183
     */
184
    public function isTestModeEnabled()
185
    {
186
        return $this->testMode;
187
    }
188
189
    /**
190
     * Set the API key.
191
     *
192
     * @param $apiKey
193
     * @return $this
194
     */
195
    public function setApiKey($apiKey)
196
    {
197
        $this->addHttpPlugin(
198
            new Plugin\AuthenticationPlugin(
199
                new Authentication\MultiSafepayAuthentication($apiKey)
200
            )
201
        );
202
203
        return $this;
204
    }
205
206
    /**
207
     * Add an Http plugin.
208
     *
209
     * @param Plugin $plugin
210
     * @return $this
211
     */
212
    public function addHttpPlugin(Plugin $plugin)
213
    {
214
        $this->plugins[] = $plugin;
215
216
        $this->invalidatePluginClient();
217
218
        return $this;
219
    }
220
221
    /**
222
     * Invalidate the PluginClient instance.
223
     *
224
     * @return $this
225
     */
226
    protected function invalidatePluginClient()
227
    {
228
        $this->pluginClient = null;
229
230
        return $this;
231
    }
232
233
    /**
234
     * Get the PluginClient.
235
     *
236
     * @return PluginClient
237
     */
238
    protected function getPluginClient()
239
    {
240
        if ($this->pluginClient !== null) {
241
            return $this->pluginClient;
242
        }
243
244
        $plugins = [
245
            new Plugin\ErrorPlugin(),
246
            new Plugin\AddHostPlugin($this->getApiEndpoint()),
247
            new PrependPathPlugin($this->getApiPath())
248
        ];
249
250
        // @todo set Locale plugin, Which automatically adds the locale query parameter.
251
252
        $this->pluginClient = new PluginClient(
253
            $this->httpClient,
254
            array_merge($plugins, $this->plugins)
255
        );
256
257
        return $this->pluginClient;
258
    }
259
260
    /**
261
     * Execute the Http Request.
262
     *
263
     * @param RequestInterface $request
264
     * @return array|object
265
     */
266
    public function executeRequest(RequestInterface $request)
267
    {
268
        $response = $this->getPluginClient()->sendRequest($request);
269
270
        return $this->parseResponse($response);
271
    }
272
273
    /**
274
     * Parse the response.
275
     *
276
     * @param ResponseInterface $response
277
     * @return array|object
278
     */
279
    protected function parseResponse(ResponseInterface $response)
280
    {
281
        $responseBody = json_decode(
282
            $response->getBody()->getContents()
283
        );
284
285
        $this->validateResponse($responseBody);
286
287
        return $responseBody->data;
288
    }
289
290
    /**
291
     * Validate the response.
292
     *
293
     * @param $response
294
     * @return bool
295
     * @throws InvalidRequestException
296
     */
297
    protected function validateResponse($response)
298
    {
299
        if (!property_exists($response, 'success') || $response->success !== true) {
300
            throw new InvalidRequestException($response->error_info, $response->error_code);
301
        }
302
303
        return true;
304
    }
305
306
    /**
307
     * Create an GatewayRequest.
308
     *
309
     * @return GatewayRequest
310
     */
311
    public function createGatewayRequest()
312
    {
313
        return new GatewayRequest;
314
    }
315
316
    /**
317
     * Create an IssuerRequest.
318
     *
319
     * @return IssuerRequest
320
     */
321
    public function createIssuerRequest()
322
    {
323
        return new IssuerRequest;
324
    }
325
326
    /**
327
     * Create an new request.
328
     *
329
     * @param $resource
330
     * @return \Ntholenaar\MultiSafepayClient\Request\RequestInterface
331
     */
332
    public function createRequest($resource)
333
    {
334
        switch ($resource) {
335
            case 'gateways':
336
                return $this->createGatewayRequest();
337
338
            case 'issuers':
339
                return $this->createIssuerRequest();
340
        }
341
342
        throw new \InvalidArgumentException('Invalid command specified.');
343
    }
344
}