1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Shapin\Stripe\Api; |
11
|
|
|
|
12
|
|
|
use Shapin\Stripe\ErrorHandler; |
13
|
|
|
use Shapin\Stripe\HttpQueryBuilder; |
14
|
|
|
use Shapin\Stripe\Hydrator\Hydrator; |
15
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
16
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface; |
17
|
|
|
|
18
|
|
|
abstract class HttpApi |
19
|
|
|
{ |
20
|
|
|
protected $httpClient; |
21
|
|
|
protected $hydrator; |
22
|
|
|
protected $httpQueryBuilder; |
23
|
|
|
protected $errorHandler; |
24
|
|
|
|
25
|
37 |
|
public function __construct(HttpClientInterface $httpClient, Hydrator $hydrator, HttpQueryBuilder $httpQueryBuilder = null, ErrorHandler $errorHandler = null) |
26
|
|
|
{ |
27
|
37 |
|
$this->httpClient = $httpClient; |
28
|
37 |
|
$this->hydrator = $hydrator; |
29
|
37 |
|
$this->httpQueryBuilder = $httpQueryBuilder ?: new HttpQueryBuilder(); |
30
|
37 |
|
$this->errorHandler = $errorHandler ?: new ErrorHandler(); |
31
|
37 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Send a GET request with query parameters. |
35
|
|
|
*/ |
36
|
32 |
View Code Duplication |
protected function httpGet(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
|
|
|
|
37
|
|
|
{ |
38
|
32 |
|
$response = $this->httpClient->request('GET', $path, [ |
39
|
32 |
|
'query' => $params, |
40
|
32 |
|
'headers' => $requestHeaders, |
41
|
|
|
]); |
42
|
|
|
|
43
|
32 |
|
if (200 !== $response->getStatusCode()) { |
44
|
|
|
$this->errorHandler->handle($response); |
45
|
|
|
} |
46
|
|
|
|
47
|
32 |
|
return $response; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Send a POST request with JSON-encoded parameters. |
52
|
|
|
*/ |
53
|
4 |
|
protected function httpPost(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
54
|
|
|
{ |
55
|
4 |
|
$requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; |
56
|
|
|
|
57
|
4 |
|
$response = $this->httpClient->request('POST', $path, [ |
58
|
4 |
|
'body' => $this->httpQueryBuilder->build($params), |
59
|
4 |
|
'headers' => $requestHeaders, |
60
|
|
|
]); |
61
|
|
|
|
62
|
4 |
|
if (200 !== $response->getStatusCode()) { |
63
|
|
|
$this->errorHandler->handle($response); |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
return $response; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Send a PUT request with JSON-encoded parameters. |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
protected function httpPut(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$response = $this->httpClient->request('PUT', $path, [ |
75
|
|
|
'body' => $params, |
76
|
|
|
'headers' => $requestHeaders, |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
if (200 !== $response->getStatusCode()) { |
80
|
|
|
$this->errorHandler->handle($response); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $response; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Send a DELETE request with JSON-encoded parameters. |
88
|
|
|
*/ |
89
|
1 |
View Code Duplication |
protected function httpDelete(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
|
|
|
|
90
|
|
|
{ |
91
|
1 |
|
$response = $this->httpClient->request('DELETE', $path, [ |
92
|
1 |
|
'body' => $params, |
93
|
1 |
|
'headers' => $requestHeaders, |
94
|
|
|
]); |
95
|
|
|
|
96
|
1 |
|
if (200 !== $response->getStatusCode()) { |
97
|
|
|
$this->errorHandler->handle($response); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
return $response; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.