Completed
Push — master ( b9b966...20dba5 )
by Vladimir
02:14
created

Entities::getProductById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AcquiroPay\Services;
6
7
use stdClass;
8
use RuntimeException;
9
use InvalidArgumentException;
10
use AcquiroPay\Models\Entities\Site;
11
use AcquiroPay\Models\Entities\Agent;
12
use AcquiroPay\Models\Pagination\Meta;
13
use GuzzleHttp\Client as GuzzleClient;
14
use AcquiroPay\Models\Entities\Country;
15
use AcquiroPay\Models\Entities\Product;
16
use AcquiroPay\Models\Entities\Currency;
17
use AcquiroPay\Models\Entities\Merchant;
18
use AcquiroPay\Models\Pagination\Paginator;
19
use AcquiroPay\Contracts\Mapper as MapperContract;
20
use AcquiroPay\Contracts\Entities as EntitiesContract;
21
22
class Entities implements EntitiesContract
23
{
24
    protected $client;
25
    protected $mapper;
26
27
    public function __construct(GuzzleClient $client, MapperContract $mapper)
28
    {
29
        $this->client = $client;
30
        $this->mapper = $mapper;
31
    }
32
33
    /**
34
     * Get countries.
35
     *
36
     * @param array $parameters
37
     * @return Paginator
38
     */
39
    public function getCountries(array $parameters = []): Paginator
40
    {
41
        $response = $this->makeRequest('GET', 'countries', $parameters);
42
43
        return $this->transform(Country::class, $response, true);
44
    }
45
46
    /**
47
     * Get country by id.
48
     *
49
     * @param int $id
50
     * @param array $parameters
51
     * @return Country|null
52
     */
53
    public function getCountry(int $id, array $parameters = []): ?Country
54
    {
55
        $response = $this->makeRequest('GET', 'countries/'.$id, $parameters);
56
57
        return $this->transform(Country::class, $response);
58
    }
59
60
    /**
61
     * Get currencies.
62
     *
63
     * @param array $parameters
64
     * @return Paginator
65
     */
66
    public function getCurrencies(array $parameters = []): Paginator
67
    {
68
        $response = $this->makeRequest('GET', 'currencies', $parameters);
69
70
        return $this->transform(Currency::class, $response, true);
71
    }
72
73
    /**
74
     * Get currency by id.
75
     *
76
     * @param int $id
77
     * @param array $parameters
78
     * @return Currency|null
79
     */
80
    public function getCurrency(int $id, array $parameters = []): ?Currency
81
    {
82
        $response = $this->makeRequest('GET', 'currencies/'.$id, $parameters);
83
84
        return $this->transform(Currency::class, $response);
85
    }
86
87
    /**
88
     * Get agents.
89
     *
90
     * @param array $parameters
91
     * @return Paginator
92
     */
93
    public function getAgents(array $parameters = []): Paginator
94
    {
95
        $response = $this->makeRequest('GET', 'agents', $parameters);
96
97
        return $this->transform(Agent::class, $response, true);
98
    }
99
100
    /**
101
     * Get merchants.
102
     *
103
     * @param array $parameters
104
     * @return Paginator
105
     */
106
    public function getMerchants(array $parameters = []): Paginator
107
    {
108
        $response = $this->makeRequest('GET', 'merchants', $parameters);
109
110
        return $this->transform(Merchant::class, $response, true);
111
    }
112
113
    /**
114
     * Get merchant by id.
115
     *
116
     * @param int $id
117
     * @param array $parameters
118
     * @return Merchant|null
119
     */
120
    public function getMerchant(int $id, array $parameters = []): ?Merchant
121
    {
122
        $response = $this->makeRequest('GET', 'merchants/'.$id, $parameters);
123
124
        return $this->transform(Merchant::class, $response);
125
    }
126
127
    /**
128
     * Get merchant sites.
129
     *
130
     * @param Merchant $merchant
131
     * @param array $parameters
132
     *
133
     * @return Paginator
134
     */
135
    public function getSites(Merchant $merchant, array $parameters = []): Paginator
136
    {
137
        $response = $this->makeRequest('GET', 'merchants/'.$merchant->getId().'/sites', $parameters);
138
139
        return $this->transform(Site::class, $response, true);
140
    }
141
142
    /**
143
     * Get merchant site.
144
     *
145
     * @param Merchant $merchant
146
     * @param int $id
147
     * @param array $parameters
148
     *
149
     * @return Site|null
150
     */
151
    public function getSite(Merchant $merchant, int $id, array $parameters = []): ?Site
152
    {
153
        $response = $this->makeRequest('GET', 'merchants/'.$merchant->getId().'/sites/'.$id, $parameters);
154
155
        return $this->transform(Site::class, $response);
156
    }
157
158
    /**
159
     * Get site by id.
160
     *
161
     * @param int $id
162
     * @param array $parameters
163
     * @return Site|null
164
     */
165
    public function getSiteById(int $id, array $parameters = []): ?Site
166
    {
167
        $response = $this->makeRequest('GET', 'sites/'.$id, $parameters);
168
169
        return $this->transform(Site::class, $response);
170
    }
171
172
    /**
173
     * Get products.
174
     *
175
     * @param Merchant $merchant
176
     * @param Site $site
177
     * @param array $parameters
178
     *
179
     * @return Paginator
180
     */
181
    public function getProducts(Merchant $merchant, Site $site, array $parameters = []): Paginator
182
    {
183
        $response = $this->makeRequest(
184
            'GET',
185
            'merchants/'.$merchant->getId().'/sites/'.$site->getId().'/products',
186
            $parameters
187
        );
188
189
        return $this->transform(Product::class, $response, true);
190
    }
191
192
    /**
193
     * Get merchant site's product.
194
     *
195
     * @param Merchant $merchant
196
     * @param Site $site
197
     * @param int $id
198
     * @param array $parameters
199
     *
200
     * @return Product|null
201
     */
202
    public function getProduct(Merchant $merchant, Site $site, int $id, array $parameters = []): ?Product
203
    {
204
        $response = $this->makeRequest(
205
            'GET',
206
            'merchants/'.$merchant->getId().'/sites/'.$site->getId().'/products/'.$id,
207
            $parameters
208
        );
209
210
        return $this->transform(Product::class, $response);
211
    }
212
213
    /**
214
     * Get product by id.
215
     *
216
     * @param int $id
217
     * @param array $parameters
218
     * @return Product|null
219
     */
220
    public function getProductById(int $id, array $parameters = []): ?Product
221
    {
222
        $response = $this->makeRequest(
223
            'GET',
224
            'products/'.$id,
225
            $parameters
226
        );
227
228
        return $this->transform(Product::class, $response);
229
    }
230
231
    /**
232
     * @param string $method
233
     * @param string $url
234
     * @param array $options
235
     *
236
     * @return mixed
237
     * @throws RuntimeException
238
     * @throws InvalidArgumentException
239
     */
240
    protected function makeRequest(string $method, string $url, array $options)
241
    {
242
        if ($method === 'GET') {
243
            $options = ['query' => $options];
244
        }
245
        $response = $this->client->request($method, 'services/entities/'.$url, $options);
246
        $response = (string) $response->getBody()->getContents();
247
        $response = json_decode($response);
248
249
        return $response ?? new stdClass();
250
    }
251
252
    /**
253
     * Transform data.
254
     *
255
     * @param string $entity
256
     * @param stdClass|array $data
257
     * @param bool $list
258
     * @return mixed
259
     */
260
    private function transform(string $entity, $data, bool $list = false)
261
    {
262
        if (!$list) {
263
            $data = $data->result;
264
265
            return $this->mapper->map($data, new $entity);
266
        }
267
268
        $paginator = new Paginator();
269
        $paginator->setData(collect($this->mapper->mapArray($data->result->data, [], $entity)));
270
        $paginator->setMeta($this->mapper->map($data->result->meta->pagination, new Meta()));
271
272
        return $paginator;
273
    }
274
}
275