Completed
Push — master ( d308f6...9403bf )
by
unknown
08:42 queued 31s
created

Entities   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 8
dl 0
loc 216
rs 10
c 0
b 0
f 0

17 Methods

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