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 ( 134146...83d859 )
by Niels
11:03
created

Client::createOrderRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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