Completed
Push — master ( 9403bf...9c8c23 )
by Vladimir
02:29
created

Entities::transform()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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