1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AcquiroPay\Services; |
6
|
|
|
|
7
|
|
|
use stdClass; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use AcquiroPay\Helpers\Str; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use Illuminate\Support\Collection; |
12
|
|
|
use AcquiroPay\Models\Entities\Site; |
13
|
|
|
use AcquiroPay\Models\Entities\Entity; |
14
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
15
|
|
|
use AcquiroPay\Models\Entities\Country; |
16
|
|
|
use AcquiroPay\Models\Entities\Product; |
17
|
|
|
use AcquiroPay\Models\Entities\Currency; |
18
|
|
|
use AcquiroPay\Models\Entities\Merchant; |
19
|
|
|
use function GuzzleHttp\json_decode as decode; |
20
|
|
|
use AcquiroPay\Contracts\Mapper as MapperContract; |
21
|
|
|
use AcquiroPay\Contracts\Entities as EntitiesContract; |
22
|
|
|
|
23
|
|
|
class Entities implements EntitiesContract |
24
|
|
|
{ |
25
|
|
|
protected $client; |
26
|
|
|
protected $mapper; |
27
|
|
|
|
28
|
|
|
public function __construct(GuzzleClient $client, MapperContract $mapper) |
29
|
|
|
{ |
30
|
|
|
$this->client = $client; |
31
|
|
|
$this->mapper = $mapper; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get countries. |
36
|
|
|
* |
37
|
|
|
* @param array $parameters |
38
|
|
|
* @return Collection |
39
|
|
|
*/ |
40
|
|
|
public function getCountries(array $parameters = []): Collection |
41
|
|
|
{ |
42
|
|
|
return $this->getEntityCollection(Country::class, $parameters); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get currencies. |
47
|
|
|
* |
48
|
|
|
* @param array $parameters |
49
|
|
|
* @return Collection |
50
|
|
|
*/ |
51
|
|
|
public function getCurrencies(array $parameters = []): Collection |
52
|
|
|
{ |
53
|
|
|
return $this->getEntityCollection(Currency::class, $parameters); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get products. |
58
|
|
|
* |
59
|
|
|
* @param array $parameters |
60
|
|
|
* @return Collection |
61
|
|
|
*/ |
62
|
|
|
public function getProducts(array $parameters = []): Collection |
63
|
|
|
{ |
64
|
|
|
return $this->getEntityCollection(Product::class, $parameters); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get country by id. |
69
|
|
|
* |
70
|
|
|
* @param int $id |
71
|
|
|
* @param array $parameters |
72
|
|
|
* @return Country|null |
73
|
|
|
*/ |
74
|
|
|
public function getCountry(int $id, array $parameters = []): ?Country |
75
|
|
|
{ |
76
|
|
|
return $this->getEntity(new Country(), $id, $parameters); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get currency by id. |
81
|
|
|
* |
82
|
|
|
* @param int $id |
83
|
|
|
* @param array $parameters |
84
|
|
|
* @return Currency|null |
85
|
|
|
*/ |
86
|
|
|
public function getCurrency(int $id, array $parameters = []): ?Currency |
87
|
|
|
{ |
88
|
|
|
return $this->getEntity(new Currency(), $id, $parameters); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get product by id. |
93
|
|
|
* |
94
|
|
|
* @param int $id |
95
|
|
|
* @param array $parameters |
96
|
|
|
* @return Product|null |
97
|
|
|
*/ |
98
|
|
|
public function getProduct(int $id, array $parameters = []): ?Product |
99
|
|
|
{ |
100
|
|
|
return $this->getEntity(new Product(), $id, $parameters); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get site by product id. |
105
|
|
|
* |
106
|
|
|
* @param int $productId |
107
|
|
|
* @param array $parameters |
108
|
|
|
* @return Site|null |
109
|
|
|
*/ |
110
|
|
|
public function getSite(int $productId, array $parameters = []): ?Site |
111
|
|
|
{ |
112
|
|
|
$url = $this->resource(Product::class).'/'.$productId.'/site'; |
113
|
|
|
$resp = $this->makeGetRequest($url, $parameters); |
114
|
|
|
|
115
|
|
|
return $this->response2Entity($resp, new Site()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get merchant by product id. |
120
|
|
|
* |
121
|
|
|
* @param int $productId |
122
|
|
|
* @param array $parameters |
123
|
|
|
* @return Merchant|null |
124
|
|
|
*/ |
125
|
|
|
public function getMerchant(int $productId, array $parameters = []): ?Merchant |
126
|
|
|
{ |
127
|
|
|
$url = $this->resource(Merchant::class).'/'.$productId.'/merchant'; |
128
|
|
|
$resp = $this->makeGetRequest($url, $parameters); |
129
|
|
|
|
130
|
|
|
return $this->response2Entity($resp, new Merchant()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get entities list. |
135
|
|
|
* |
136
|
|
|
* @param string $entityName |
137
|
|
|
* @param array $params |
138
|
|
|
* @return stdClass |
139
|
|
|
*/ |
140
|
|
|
public function getList(string $entityName, array $params = []): stdClass |
141
|
|
|
{ |
142
|
|
|
$url = $this->resource($entityName); |
143
|
|
|
|
144
|
|
|
return $this->makeGetRequest($url, $params); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get entity. |
149
|
|
|
* |
150
|
|
|
* @param string $entityName |
151
|
|
|
* @param $id |
152
|
|
|
* @param array $params |
153
|
|
|
* @return stdClass |
154
|
|
|
*/ |
155
|
|
|
public function get(string $entityName, $id, array $params = []): stdClass |
156
|
|
|
{ |
157
|
|
|
$url = $this->resource($entityName).'/'.$id; |
158
|
|
|
|
159
|
|
|
return $this->makeGetRequest($url, $params); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get entity as collection. |
164
|
|
|
* |
165
|
|
|
* @param string $className |
166
|
|
|
* @param array $params |
167
|
|
|
* @return Collection |
168
|
|
|
*/ |
169
|
|
|
protected function getEntityCollection(string $className, array $params): Collection |
170
|
|
|
{ |
171
|
|
|
$resp = $this->getList($className, $params); |
172
|
|
|
if (isset($resp->result, $resp->result->data)) { |
173
|
|
|
return collect($this->mapper->mapArray($resp->result->data, [], $className)); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return collect(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
protected function getEntity(Entity $entity, $id, array $params): ?Entity |
180
|
|
|
{ |
181
|
|
|
$resp = $this->get(get_class($entity), $id, $params); |
182
|
|
|
|
183
|
|
|
return $this->response2Entity($resp, $entity); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
protected function makeGetRequest(string $url, array $params): stdClass |
187
|
|
|
{ |
188
|
|
|
return $this->makeRequest('get', $url, ['query' => $params]); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param string $method |
193
|
|
|
* @param string $url |
194
|
|
|
* @param array $options |
195
|
|
|
* @return mixed |
196
|
|
|
* @throws RuntimeException |
197
|
|
|
* @throws InvalidArgumentException |
198
|
|
|
*/ |
199
|
|
|
protected function makeRequest(string $method, string $url, array $options) |
200
|
|
|
{ |
201
|
|
|
$response = $this->client->request($method, 'services/entities/'.$url, $options); |
202
|
|
|
$response = (string) $response->getBody()->getContents(); |
203
|
|
|
$response = decode($response); |
204
|
|
|
|
205
|
|
|
return $response ?? new stdClass(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
protected function resource(string $className): string |
209
|
|
|
{ |
210
|
|
|
$value = Str::snake(Str::classBaseName($className), '-'); |
211
|
|
|
|
212
|
|
|
return Str::lower($value); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
protected function response2Entity($resp, Entity $entity): ?Entity |
216
|
|
|
{ |
217
|
|
|
if (isset($resp->result)) { |
218
|
|
|
$this->mapper->map($resp->result, $entity); |
219
|
|
|
|
220
|
|
|
return $entity; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return null; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|