1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LauLamanApps\eCurring; |
6
|
|
|
|
7
|
|
|
use LauLamanApps\eCurring\Http\ClientInterface; |
8
|
|
|
use LauLamanApps\eCurring\Http\Endpoint\MapperInterface; |
9
|
|
|
use LauLamanApps\eCurring\Http\Resource\UpdateParserInterface; |
10
|
|
|
use LauLamanApps\eCurring\Http\Resource\CreateParserInterface; |
11
|
|
|
use LauLamanApps\eCurring\Resource\Curser\Pagination; |
12
|
|
|
use LauLamanApps\eCurring\Resource\Customer; |
13
|
|
|
use LauLamanApps\eCurring\Resource\CustomerCollection; |
14
|
|
|
use LauLamanApps\eCurring\Resource\Factory\CustomerFactoryInterface; |
15
|
|
|
use LauLamanApps\eCurring\Resource\Factory\SubscriptionFactoryInterface; |
16
|
|
|
use LauLamanApps\eCurring\Resource\Factory\SubscriptionPlanFactoryInterface; |
17
|
|
|
use LauLamanApps\eCurring\Resource\Factory\TransactionFactoryInterface; |
18
|
|
|
use LauLamanApps\eCurring\Resource\Subscription; |
19
|
|
|
use LauLamanApps\eCurring\Resource\SubscriptionCollection; |
20
|
|
|
use LauLamanApps\eCurring\Resource\SubscriptionPlan; |
21
|
|
|
use LauLamanApps\eCurring\Resource\SubscriptionPlanCollection; |
22
|
|
|
use LauLamanApps\eCurring\Resource\TransactionCollection; |
23
|
|
|
use LauLamanApps\eCurring\Resource\Transaction; |
24
|
|
|
use Ramsey\Uuid\UuidInterface; |
25
|
|
|
|
26
|
|
|
final class eCurringClient implements eCurringClientInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var ClientInterface |
30
|
|
|
*/ |
31
|
|
|
private $httpClient; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var SubscriptionPlanFactoryInterface |
35
|
|
|
*/ |
36
|
|
|
private $subscriptionPlanFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var TransactionFactoryInterface |
40
|
|
|
*/ |
41
|
|
|
private $transactionFactory; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var SubscriptionFactoryInterface |
45
|
|
|
*/ |
46
|
|
|
private $subscriptionFactory; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var CustomerFactoryInterface |
50
|
|
|
*/ |
51
|
|
|
private $customerFactory; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var CreateParserInterface |
55
|
|
|
*/ |
56
|
|
|
private $createParser; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var UpdateParserInterface |
60
|
|
|
*/ |
61
|
|
|
private $updateParser; |
62
|
|
|
|
63
|
|
|
public function __construct( |
64
|
|
|
ClientInterface $httpClient, |
65
|
|
|
CustomerFactoryInterface $customerFactory, |
66
|
|
|
SubscriptionFactoryInterface $subscriptionFactory, |
67
|
|
|
SubscriptionPlanFactoryInterface $subscriptionPlanFactory, |
68
|
|
|
TransactionFactoryInterface $transactionFactory, |
69
|
|
|
CreateParserInterface $createParser, |
70
|
|
|
UpdateParserInterface $updateParser |
71
|
|
|
) { |
72
|
|
|
$this->httpClient = $httpClient; |
73
|
|
|
$this->customerFactory = $customerFactory; |
74
|
|
|
$this->subscriptionFactory = $subscriptionFactory; |
75
|
|
|
$this->subscriptionPlanFactory = $subscriptionPlanFactory; |
76
|
|
|
$this->transactionFactory = $transactionFactory; |
77
|
|
|
$this->createParser = $createParser; |
78
|
|
|
$this->updateParser = $updateParser; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getCustomers(?Pagination $pagination = null): CustomerCollection |
82
|
|
|
{ |
83
|
|
|
$json = $this->httpClient->getJson( |
84
|
|
|
$this->httpClient->getEndpoint(MapperInterface::GET_CUSTOMERS, [], $pagination) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return $this->customerFactory->fromArray( |
88
|
|
|
$this, |
89
|
|
|
$this->decodeJsonToArray($json), |
90
|
|
|
$pagination ?? new Pagination(10) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getCustomer(string $id): Customer |
95
|
|
|
{ |
96
|
|
|
$json = $this->httpClient->getJson( |
97
|
|
|
$this->httpClient->getEndpoint(MapperInterface::GET_CUSTOMER, [$id]) |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
return $this->customerFactory->fromData($this, $this->decodeJsonToArray($json)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function createCustomer(Customer $customer): Customer |
104
|
|
|
{ |
105
|
|
|
$data = $this->createParser->parse($customer); |
106
|
|
|
|
107
|
|
|
$json = $this->httpClient->getJson( |
108
|
|
|
$this->httpClient->postEndpoint(MapperInterface::POST_CUSTOMER, $data [$customer->getId()]) |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
return $this->customerFactory->fromData($this, $this->decodeJsonToArray($json)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function updateCustomer(Customer $customer): Customer |
115
|
|
|
{ |
116
|
|
|
$data = $this->updateParser->parse($customer); |
117
|
|
|
|
118
|
|
|
$json = $this->httpClient->getJson( |
119
|
|
|
$this->httpClient->patchEndpoint(MapperInterface::PATCH_CUSTOMER, $data [$customer->getId()]) |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
return $this->customerFactory->fromData($this, $this->decodeJsonToArray($json)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getSubscriptionPlans(?Pagination $pagination = null): SubscriptionPlanCollection |
126
|
|
|
{ |
127
|
|
|
$json = $this->httpClient->getJson( |
128
|
|
|
$this->httpClient->getEndpoint(MapperInterface::GET_SUBSCRIPTION_PLANS, [], $pagination) |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
return $this->subscriptionPlanFactory->fromArray( |
132
|
|
|
$this, |
133
|
|
|
$this->decodeJsonToArray($json), |
134
|
|
|
$pagination ?? new Pagination(10) |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getSubscriptionPlan(string $id): SubscriptionPlan |
139
|
|
|
{ |
140
|
|
|
$json = $this->httpClient->getJson( |
141
|
|
|
$this->httpClient->getEndpoint(MapperInterface::GET_SUBSCRIPTION_PLAN, [$id]) |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
return $this->subscriptionPlanFactory->fromData($this, $this->decodeJsonToArray($json)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function getSubscriptions(?Pagination $page = null): SubscriptionCollection |
148
|
|
|
{ |
149
|
|
|
$json = $this->httpClient->getJson( |
150
|
|
|
$this->httpClient->getEndpoint(MapperInterface::GET_SUBSCRIPTIONS) |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
return $this->subscriptionFactory->fromArray( |
154
|
|
|
$this, |
155
|
|
|
$this->decodeJsonToArray($json), |
156
|
|
|
$page ?? new Pagination(10) |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function getSubscription(string $id): Subscription |
161
|
|
|
{ |
162
|
|
|
$json = $this->httpClient->getJson( |
163
|
|
|
$this->httpClient->getEndpoint(MapperInterface::GET_SUBSCRIPTION, [$id]) |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
return $this->subscriptionFactory->fromData($this, $this->decodeJsonToArray($json)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function getSubscriptionTransactions(Subscription $subscription, ?Pagination $pagination = null): TransactionCollection |
170
|
|
|
{ |
171
|
|
|
$json = $this->httpClient->getJson( |
172
|
|
|
$this->httpClient->getEndpoint(MapperInterface::GET_SUBSCRIPTION_TRANSACTIONS, [$subscription->getId()]) |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
return $this->transactionFactory->fromSubscriptionArray( |
176
|
|
|
$this, |
177
|
|
|
$this->decodeJsonToArray($json), |
178
|
|
|
$subscription, |
179
|
|
|
$pagination ?? new Pagination(10) |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function createSubscription(Subscription $subscription): Subscription |
184
|
|
|
{ |
185
|
|
|
$data = $this->createParser->parse($subscription); |
186
|
|
|
|
187
|
|
|
$json = $this->httpClient->getJson( |
188
|
|
|
$this->httpClient->postEndpoint(MapperInterface::POST_SUBSCRIPTION, $data [$subscription->getId()]) |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
return $this->subscriptionFactory->fromData($this, $this->decodeJsonToArray($json)); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function updateSubscription(Subscription $subscription): Subscription |
195
|
|
|
{ |
196
|
|
|
$data = $this->updateParser->parse($subscription); |
197
|
|
|
|
198
|
|
|
$json = $this->httpClient->getJson( |
199
|
|
|
$this->httpClient->patchEndpoint(MapperInterface::PATCH_SUBSCRIPTION, $data [$subscription->getId()]) |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
return $this->subscriptionFactory->fromData($this, $this->decodeJsonToArray($json)); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function getTransaction(UuidInterface $id): Transaction |
206
|
|
|
{ |
207
|
|
|
$json = $this->httpClient->getJson( |
208
|
|
|
$this->httpClient->getEndpoint(MapperInterface::GET_SUBSCRIPTION_PLAN, [$id]) |
209
|
|
|
); |
210
|
|
|
|
211
|
|
|
return $this->transactionFactory->fromData($this->decodeJsonToArray($json)); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function createTransaction(Transaction $transaction): Transaction |
215
|
|
|
{ |
216
|
|
|
$data = $this->createParser->parse($transaction); |
217
|
|
|
|
218
|
|
|
$json = $this->httpClient->getJson( |
219
|
|
|
$this->httpClient->postEndpoint(MapperInterface::POST_TRANSACTION, $data [$transaction->getId()]) |
220
|
|
|
); |
221
|
|
|
|
222
|
|
|
return $this->transactionFactory->fromData($this, $this->decodeJsonToArray($json)); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function deleteTransaction(Transaction $transaction): void |
226
|
|
|
{ |
227
|
|
|
$this->httpClient->deleteEndpoint(MapperInterface::DELETE_TRANSACTION, [$transaction->getId()]); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
private function decodeJsonToArray(string $json): array |
231
|
|
|
{ |
232
|
|
|
return json_decode($json, true); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.