Completed
Push — master ( 82e3be...acf0da )
by
unknown
02:19
created

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