1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace CommerceLeague\ActiveCampaignApi\Client; |
7
|
|
|
|
8
|
|
|
use CommerceLeague\ActiveCampaignApi\Routing\UriGeneratorInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class CommonResourceClient |
12
|
|
|
*/ |
13
|
|
|
class CommonResourceClient implements CommonResourceClientInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var HttpClientInterface |
17
|
|
|
*/ |
18
|
|
|
private $httpClient; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var UriGeneratorInterface |
22
|
|
|
*/ |
23
|
|
|
private $uriGenerator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param HttpClientInterface $httpClient |
27
|
|
|
* @param UriGeneratorInterface $uriGenerator |
28
|
|
|
*/ |
29
|
|
|
public function __construct( |
30
|
|
|
HttpClientInterface $httpClient, |
31
|
|
|
UriGeneratorInterface $uriGenerator |
32
|
|
|
) { |
33
|
|
|
$this->httpClient = $httpClient; |
34
|
|
|
$this->uriGenerator = $uriGenerator; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
40
|
|
|
public function getResource(string $uri, array $uriParameters = [], array $queryParameters = []): array |
41
|
|
|
{ |
42
|
|
|
$uri = $this->uriGenerator->generate($uri, $uriParameters, $queryParameters); |
43
|
|
|
|
44
|
|
|
$response = $this->httpClient->sendRequest( |
45
|
|
|
'GET', |
46
|
|
|
$uri, |
47
|
|
|
['Accept' => 'application/json'] |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
return json_decode($response->getBody()->getContents(), true); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
|
|
public function getResources( |
57
|
|
|
string $uri, |
58
|
|
|
array $uriParameters = [], |
59
|
|
|
?int $limit = 20, |
60
|
|
|
?int $offset = 0, |
61
|
|
|
array $queryParameters = [] |
62
|
|
|
): array { |
63
|
|
|
if ($limit !== null) { |
64
|
|
|
$queryParameters['limit'] = $limit; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($offset !== null) { |
68
|
|
|
$queryParameters['offset'] = $offset; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->getResource($uri, $uriParameters, $queryParameters); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
|
|
public function createResource(string $uri, array $uriParameters = [], array $body = []): array |
78
|
|
|
{ |
79
|
|
|
$uri = $this->uriGenerator->generate($uri, $uriParameters); |
80
|
|
|
$response = $this->httpClient->sendRequest( |
81
|
|
|
'POST', |
82
|
|
|
$uri, |
83
|
|
|
['Content-Type' => 'application/json'], |
84
|
|
|
json_encode($body) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return json_decode($response->getBody()->getContents(), true); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
*/ |
93
|
|
|
public function updateResource(string $uri, array $uriParameters = [], array $body = []): array |
94
|
|
|
{ |
95
|
|
|
$uri = $this->uriGenerator->generate($uri, $uriParameters); |
96
|
|
|
$response = $this->httpClient->sendRequest( |
97
|
|
|
'PUT', |
98
|
|
|
$uri, |
99
|
|
|
['Content-Type' => 'application/json'], |
100
|
|
|
json_encode($body) |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
return json_decode($response->getBody()->getContents(), true); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritDoc |
108
|
|
|
*/ |
109
|
|
|
public function upsertResource(string $uri, array $uriParameters = [], array $body = []): array |
110
|
|
|
{ |
111
|
|
|
$uri = $this->uriGenerator->generate($uri, $uriParameters); |
112
|
|
|
$response = $this->httpClient->sendRequest( |
113
|
|
|
'POST', |
114
|
|
|
$uri, |
115
|
|
|
['Content-Type' => 'application/json'], |
116
|
|
|
json_encode($body) |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
return json_decode($response->getBody()->getContents(), true); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritDoc |
124
|
|
|
*/ |
125
|
|
|
public function deleteResource(string $uri, array $uriParameters = []): bool |
126
|
|
|
{ |
127
|
|
|
$uri = $this->uriGenerator->generate($uri, $uriParameters); |
128
|
|
|
$response = $this->httpClient->sendRequest('DELETE', $uri); |
129
|
|
|
|
130
|
|
|
return $response->getStatusCode() === 200; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|