Completed
Push — master ( 269c07...d308f6 )
by Vladimir
02:30
created

Entities::resource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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 Illuminate\Support\Collection;
11
use AcquiroPay\Models\Entities\Site;
12
use AcquiroPay\Models\Entities\Entity;
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 function GuzzleHttp\json_decode as decode;
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
    protected $resources = [
28
        Country::class => 'countries',
29
        Currency::class => 'currencies',
30
        Merchant::class => 'merchants',
31
        Product::class => 'products',
32
        Site::class => 'sites',
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
        return $this->getEntityCollection(Country::class, $parameters);
50
    }
51
52
    /**
53
     * Get currencies.
54
     *
55
     * @param array $parameters
56
     * @return Collection
57
     */
58
    public function getCurrencies(array $parameters = []): Collection
59
    {
60
        return $this->getEntityCollection(Currency::class, $parameters);
61
    }
62
63
    /**
64
     * Get products.
65
     *
66
     * @param array $parameters
67
     * @return Collection
68
     */
69
    public function getProducts(array $parameters = []): Collection
70
    {
71
        return $this->getEntityCollection(Product::class, $parameters);
72
    }
73
74
    /**
75
     * Get country by id.
76
     *
77
     * @param int $id
78
     * @param array $parameters
79
     * @return Country|null
80
     */
81
    public function getCountry(int $id, array $parameters = []): ?Country
82
    {
83
        return $this->getEntity(new Country(), $id, $parameters);
84
    }
85
86
    /**
87
     * Get currency by id.
88
     *
89
     * @param int $id
90
     * @param array $parameters
91
     * @return Currency|null
92
     */
93
    public function getCurrency(int $id, array $parameters = []): ?Currency
94
    {
95
        return $this->getEntity(new Currency(), $id, $parameters);
96
    }
97
98
    /**
99
     * Get product by id.
100
     *
101
     * @param int $id
102
     * @param array $parameters
103
     * @return Product|null
104
     */
105
    public function getProduct(int $id, array $parameters = []): ?Product
106
    {
107
        return $this->getEntity(new Product(), $id, $parameters);
108
    }
109
110
    /**
111
     * Get site by product id.
112
     *
113
     * @param int $productId
114
     * @param array $parameters
115
     * @return Site|null
116
     */
117
    public function getSite(int $productId, array $parameters = []): ?Site
118
    {
119
        $url = $this->resources[Product::class].'/'.$productId.'/site';
120
        $resp = $this->makeGetRequest($url, $parameters);
121
122
        return $this->response2Entity($resp, new Site());
123
    }
124
125
    /**
126
     * Get merchant by product id.
127
     *
128
     * @param int $productId
129
     * @param array $parameters
130
     * @return Merchant|null
131
     */
132
    public function getMerchant(int $productId, array $parameters = []): ?Merchant
133
    {
134
        $url = $this->resources[Merchant::class].'/'.$productId.'/merchant';
135
        $resp = $this->makeGetRequest($url, $parameters);
136
137
        return $this->response2Entity($resp, new Merchant());
138
    }
139
140
    /**
141
     * Get entities list.
142
     *
143
     * @param string $entityName
144
     * @param array $params
145
     * @return stdClass
146
     */
147
    public function getList(string $entityName, array $params = []): stdClass
148
    {
149
        $url = $this->resources[$entityName];
150
151
        return $this->makeGetRequest($url, $params);
152
    }
153
154
    /**
155
     * Get entity.
156
     *
157
     * @param string $entity
158
     * @param $id
159
     * @param array $params
160
     * @return stdClass
161
     */
162
    public function get(string $entity, $id, array $params = []): stdClass
163
    {
164
        $url = $this->resources[$entity].'/'.$id;
165
166
        return $this->makeGetRequest($url, $params);
167
    }
168
169
    /**
170
     * Get entity as collection.
171
     *
172
     * @param string $className
173
     * @param array $params
174
     * @return Collection
175
     */
176
    protected function getEntityCollection(string $className, array $params): Collection
177
    {
178
        $resp = $this->getList($className, $params);
179
        if (isset($resp->result, $resp->result->data)) {
180
            return collect($this->mapper->mapArray($resp->result->data, [], $className));
181
        }
182
183
        return collect();
184
    }
185
186
    protected function getEntity(Entity $entity, $id, array $params): ?Entity
187
    {
188
        $resp = $this->get(get_class($entity), $id, $params);
189
190
        return $this->response2Entity($resp, $entity);
191
    }
192
193
    protected function makeGetRequest(string $url, array $params): stdClass
194
    {
195
        return $this->makeRequest('get', $url, ['query' => $params]);
196
    }
197
198
    /**
199
     * @param string $method
200
     * @param string $url
201
     * @param array $options
202
     * @return mixed
203
     * @throws RuntimeException
204
     * @throws InvalidArgumentException
205
     */
206
    protected function makeRequest(string $method, string $url, array $options)
207
    {
208
        $response = $this->client->request($method, 'services/entities/'.$url, $options);
209
        $response = (string) $response->getBody()->getContents();
210
        $response = decode($response);
211
212
        return $response ?? new stdClass();
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