2
2
3
3
namespace Stefna \Mailchimp ;
4
4
5
- use Http \Client \HttpClient ;
6
- use Http \Discovery \MessageFactoryDiscovery ;
7
- use Http \Message \MessageFactory ;
8
5
use InvalidArgumentException ;
6
+ use Psr \Http \Client \ClientInterface ;
7
+ use Psr \Http \Message \RequestFactoryInterface ;
9
8
use Psr \Http \Message \RequestInterface ;
10
9
use Psr \Http \Message \ResponseInterface ;
10
+ use Psr \Http \Message \StreamFactoryInterface ;
11
+ use Psr \Http \Message \UriFactoryInterface ;
12
+ use Psr \Http \Message \UriInterface ;
11
13
use Psr \Log \LoggerInterface ;
12
14
use RuntimeException ;
13
15
use Stefna \Mailchimp \Api \Campaigns \Campaigns as CampaignsApi ;
18
20
class Client
19
21
{
20
22
private const DEFAULT_ENDPOINT = 'https://<dc>.api.mailchimp.com/3.0 ' ;
21
-
22
23
protected ?LoggerInterface $ logger = null ;
23
24
protected ?ResponseInterface $ lastResponse ;
24
25
protected ?RequestInterface $ lastRequest ;
25
- protected MessageFactory $ messageFactory ;
26
- protected HttpClient $ httpClient ;
26
+ protected RequestFactoryInterface $ messageFactory ;
27
+ protected ClientInterface $ httpClient ;
28
+ protected UriFactoryInterface $ uriFactory ;
29
+ protected StreamFactoryInterface $ streamFactory ;
27
30
protected string $ apiKey ;
28
31
protected string $ apiEndpoint = '' ;
29
32
30
33
public function __construct (
31
- HttpClient $ httpClient ,
34
+ ClientInterface $ httpClient ,
32
35
string $ apiKey ,
36
+ RequestFactoryInterface $ messageFactory ,
37
+ UriFactoryInterface $ uriFactory ,
38
+ StreamFactoryInterface $ streamFactory ,
33
39
?string $ apiEndpoint = null ,
34
- ?MessageFactory $ messageFactory = null
35
40
) {
36
41
$ this ->httpClient = $ httpClient ;
37
42
$ this ->apiKey = $ apiKey ;
43
+ $ this ->messageFactory = $ messageFactory ;
44
+ $ this ->uriFactory = $ uriFactory ;
45
+ $ this ->streamFactory = $ streamFactory ;
38
46
$ this ->apiEndpoint = $ apiEndpoint ?: $ this ->createApiEndpoint ($ apiKey );
39
- $ this ->messageFactory = $ messageFactory ?: MessageFactoryDiscovery::find ();
40
47
}
41
48
42
49
public function lists (): ListsApi
@@ -64,7 +71,7 @@ public function setLogger(LoggerInterface $logger): void
64
71
$ this ->logger = $ logger ;
65
72
}
66
73
67
- public function getHttpClient (): HttpClient
74
+ public function getHttpClient (): ClientInterface
68
75
{
69
76
return $ this ->httpClient ;
70
77
}
@@ -76,7 +83,7 @@ public function getHttpClient(): HttpClient
76
83
public function get (string $ path , array $ args = []): array
77
84
{
78
85
/** @var array<string, mixed> */
79
- return $ this ->request ($ this ->messageFactory -> createRequest (
86
+ return $ this ->request ($ this ->createRequest (
80
87
'GET ' ,
81
88
$ this ->createUrl ($ path , $ args ),
82
89
$ this ->getDefaultHeaders ()
@@ -88,7 +95,7 @@ public function get(string $path, array $args = []): array
88
95
*/
89
96
public function delete (string $ path , array $ args = []): bool
90
97
{
91
- return $ this ->request ($ this ->messageFactory -> createRequest (
98
+ return $ this ->request ($ this ->createRequest (
92
99
'DELETE ' ,
93
100
$ this ->createUrl ($ path , $ args ),
94
101
$ this ->getDefaultHeaders ()
@@ -102,7 +109,7 @@ public function delete(string $path, array $args = []): bool
102
109
public function post (string $ path , array $ data = [])
103
110
{
104
111
/** @var array<string, mixed> */
105
- return $ this ->request ($ this ->messageFactory -> createRequest (
112
+ return $ this ->request ($ this ->createRequest (
106
113
'POST ' ,
107
114
$ this ->createUrl ($ path ),
108
115
$ this ->getDefaultHeaders (),
@@ -116,7 +123,7 @@ public function post(string $path, array $data = [])
116
123
*/
117
124
public function put (string $ path , array $ data = []): array
118
125
{
119
- $ ret = $ this ->request ($ this ->messageFactory -> createRequest (
126
+ $ ret = $ this ->request ($ this ->createRequest (
120
127
'PUT ' ,
121
128
$ this ->createUrl ($ path ),
122
129
$ this ->getDefaultHeaders (),
@@ -136,7 +143,7 @@ public function put(string $path, array $data = []): array
136
143
*/
137
144
public function patch (string $ path , array $ data = [])
138
145
{
139
- $ ret = $ this ->request ($ this ->messageFactory -> createRequest (
146
+ $ ret = $ this ->request ($ this ->createRequest (
140
147
'PATCH ' ,
141
148
$ this ->createUrl ($ path ),
142
149
$ this ->getDefaultHeaders (),
@@ -298,11 +305,30 @@ protected function formatError($data): string
298
305
/**
299
306
* @param array<string,string> $queryParams
300
307
*/
301
- protected function createUrl (string $ path , array $ queryParams = []): string
308
+ protected function createUrl (string $ path , array $ queryParams = []): UriInterface
302
309
{
303
- $ ret = $ this ->apiEndpoint . '/ ' . $ path ;
310
+ $ url = $ this ->apiEndpoint . '/ ' . $ path ;
304
311
if ($ queryParams ) {
305
- $ ret .= '? ' . http_build_query ($ queryParams );
312
+ $ url .= '? ' . http_build_query ($ queryParams );
313
+ }
314
+ return $ this ->uriFactory ->createUri ($ url );
315
+ }
316
+
317
+ /**
318
+ * @param array<string, string|string[]> $headers
319
+ */
320
+ protected function createRequest (
321
+ string $ method ,
322
+ UriInterface $ uri ,
323
+ array $ headers = [],
324
+ ?string $ body = null
325
+ ): RequestInterface {
326
+ $ ret = $ this ->messageFactory ->createRequest ($ method , $ uri );
327
+ foreach ($ headers as $ key => $ value ) {
328
+ $ ret = $ ret ->withHeader ($ key , $ value );
329
+ }
330
+ if ($ body ) {
331
+ $ ret = $ ret ->withBody ($ this ->streamFactory ->createStream ($ body ));
306
332
}
307
333
return $ ret ;
308
334
}
0 commit comments