1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Mercado Libre API client package. |
4
|
|
|
* |
5
|
|
|
* (c) Zephia <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zephia\MercadoLibre\Client; |
12
|
|
|
|
13
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
14
|
|
|
use JMS\Serializer\SerializerInterface; |
15
|
|
|
use Zephia\MercadoLibre\Entity\Category; |
16
|
|
|
use Zephia\MercadoLibre\Entity\CategoryPrediction; |
17
|
|
|
use Zephia\MercadoLibre\Entity\Item; |
18
|
|
|
use Zephia\MercadoLibre\Entity\ItemList; |
19
|
|
|
use Zephia\MercadoLibre\Entity\User; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class MercadoLibreClient |
23
|
|
|
* |
24
|
|
|
* @package Zephia\MercadoLibre\Client |
25
|
|
|
* @author Mauro Moreno <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class MercadoLibreClient |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* MercadoLibre API URI. |
31
|
|
|
*/ |
32
|
|
|
const BASE_URI = 'https://api.mercadolibre.com'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* MercadoLibre Authorization URI. |
36
|
|
|
*/ |
37
|
|
|
const AUTH_URI = 'http://auth.mercadolibre.com/authorization'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* MercadoLibre OAuth URI. |
41
|
|
|
*/ |
42
|
|
|
const OAUTH_URI = '/oauth/token'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Guzzle Client |
46
|
|
|
* |
47
|
|
|
* @var GuzzleClient |
48
|
|
|
*/ |
49
|
|
|
private $guzzleClient; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Access Token from MercadoLibre OAuth |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private $access_token; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Serializer |
60
|
|
|
* |
61
|
|
|
* @var SerializerInterface |
62
|
|
|
*/ |
63
|
|
|
private $serializer; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* MercadoLibreClient constructor. |
67
|
|
|
* |
68
|
|
|
* @param array $config |
69
|
|
|
* @param SerializerInterface $serializer |
70
|
|
|
*/ |
71
|
30 |
|
public function __construct( |
72
|
|
|
array $config = [], |
73
|
|
|
SerializerInterface $serializer |
74
|
|
|
) { |
75
|
|
|
$defaults = [ |
76
|
30 |
|
'base_uri' => self::BASE_URI, |
77
|
30 |
|
'base_url' => self::BASE_URI, |
78
|
30 |
|
]; |
79
|
30 |
|
$config = array_merge($defaults, $config); |
80
|
|
|
|
81
|
30 |
|
$this->guzzleClient = new GuzzleClient($config); |
|
|
|
|
82
|
30 |
|
$this->serializer = $serializer; |
83
|
30 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get Guzzle client |
87
|
|
|
* |
88
|
|
|
* @return GuzzleClient |
89
|
|
|
*/ |
90
|
29 |
|
public function getGuzzleClient() |
91
|
|
|
{ |
92
|
29 |
|
return $this->guzzleClient; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set MercadoLibre Access Token |
97
|
|
|
* |
98
|
|
|
* @param string $access_token |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
15 |
|
public function setAccessToken($access_token) |
103
|
|
|
{ |
104
|
15 |
|
$this->access_token = $access_token; |
105
|
15 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get MercadoLibre Access Token |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
29 |
|
public function getAccessToken() |
114
|
|
|
{ |
115
|
29 |
|
return $this->access_token; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* User show resource |
120
|
|
|
* |
121
|
|
|
* @param $customer_id |
122
|
|
|
* |
123
|
|
|
* @return array|\JMS\Serializer\scalar|object |
124
|
|
|
*/ |
125
|
9 |
View Code Duplication |
public function userShow($customer_id) |
|
|
|
|
126
|
|
|
{ |
127
|
9 |
|
$response = $this->getGuzzleClient() |
|
|
|
|
128
|
9 |
|
->get('/users/' . $customer_id, $this->setQuery()); |
129
|
|
|
|
130
|
3 |
|
return $this->serializer->deserialize( |
131
|
3 |
|
$response->getBody()->getContents(), |
132
|
3 |
|
User::class, |
133
|
|
|
'json' |
134
|
3 |
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* User show me resource |
139
|
|
|
* |
140
|
|
|
* @return array|\JMS\Serializer\scalar|object |
141
|
|
|
*/ |
142
|
3 |
|
public function userShowMe() |
143
|
|
|
{ |
144
|
3 |
|
return $this->userShow('me'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Category list resource |
149
|
|
|
* |
150
|
|
|
* @param $site_id string |
151
|
|
|
* |
152
|
|
|
* @return array|\JMS\Serializer\scalar|object |
153
|
|
|
*/ |
154
|
2 |
|
public function categoryList($site_id) |
155
|
|
|
{ |
156
|
2 |
|
$response = $this->getGuzzleClient() |
|
|
|
|
157
|
2 |
|
->get('/sites/' . $site_id . '/categories', $this->setQuery()); |
158
|
|
|
|
159
|
1 |
|
return $this->serializer->deserialize( |
160
|
1 |
|
$response->getBody()->getContents(), |
161
|
1 |
|
"array<" . Category::class . ">", |
162
|
|
|
'json' |
163
|
1 |
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Category Predict resource |
168
|
|
|
* |
169
|
|
|
* @param $site_id string |
170
|
|
|
* @param $title string |
171
|
|
|
* |
172
|
|
|
* @return array|\JMS\Serializer\scalar|object |
173
|
|
|
*/ |
174
|
4 |
View Code Duplication |
public function categoryPredict($site_id, $title) |
|
|
|
|
175
|
|
|
{ |
176
|
4 |
|
$response = $this->getGuzzleClient() |
|
|
|
|
177
|
4 |
|
->get( |
178
|
4 |
|
'/sites/' . $site_id . '/category_predictor/predict', |
179
|
4 |
|
$this->setQuery(['title' => $title]) |
180
|
4 |
|
); |
181
|
1 |
|
return $this->serializer->deserialize( |
182
|
1 |
|
$response->getBody()->getContents(), |
183
|
1 |
|
CategoryPrediction::class, |
184
|
|
|
'json' |
185
|
1 |
|
); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Item List resource |
190
|
|
|
* |
191
|
|
|
* @param $user_id string |
192
|
|
|
* |
193
|
|
|
* @return array|\JMS\Serializer\scalar|object |
194
|
|
|
*/ |
195
|
5 |
View Code Duplication |
public function itemList($user_id) |
|
|
|
|
196
|
|
|
{ |
197
|
5 |
|
$response = $this->getGuzzleClient() |
|
|
|
|
198
|
5 |
|
->get('/users/' . $user_id . '/items/search', $this->setQuery()); |
199
|
|
|
|
200
|
1 |
|
return $this->serializer->deserialize( |
201
|
1 |
|
$response->getBody()->getContents(), |
202
|
1 |
|
ItemList::class, |
203
|
|
|
'json' |
204
|
1 |
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Item Show resource |
209
|
|
|
* |
210
|
|
|
* @param $item_id |
211
|
|
|
* |
212
|
|
|
* @return array|\JMS\Serializer\scalar|object |
213
|
|
|
*/ |
214
|
2 |
View Code Duplication |
public function itemShow($item_id) |
|
|
|
|
215
|
|
|
{ |
216
|
2 |
|
$response = $this->getGuzzleClient() |
|
|
|
|
217
|
2 |
|
->get('/items/' . $item_id, $this->setQuery()); |
218
|
|
|
|
219
|
1 |
|
return $this->serializer->deserialize( |
220
|
1 |
|
$response->getBody()->getContents(), |
221
|
1 |
|
Item::class, |
222
|
|
|
'json' |
223
|
1 |
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Item create resource |
228
|
|
|
* |
229
|
|
|
* @param Item $item |
230
|
|
|
* |
231
|
|
|
* @return array|\JMS\Serializer\scalar|object |
232
|
|
|
*/ |
233
|
6 |
View Code Duplication |
public function itemCreate(Item $item) |
|
|
|
|
234
|
|
|
{ |
235
|
6 |
|
$response = $this->getGuzzleClient() |
|
|
|
|
236
|
6 |
|
->post( |
237
|
6 |
|
'/items', |
238
|
6 |
|
array_merge($this->setQuery(), $this->setBody($item)) |
239
|
6 |
|
); |
240
|
|
|
|
241
|
1 |
|
return $this->serializer->deserialize( |
242
|
1 |
|
$response->getBody()->getContents(), |
243
|
1 |
|
Item::class, |
244
|
|
|
'json' |
245
|
1 |
|
); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Set query |
250
|
|
|
* |
251
|
|
|
* @param array $query |
252
|
|
|
* |
253
|
|
|
* @return array |
254
|
|
|
*/ |
255
|
28 |
|
private function setQuery(array $query = []) |
256
|
|
|
{ |
257
|
28 |
|
$defaults = []; |
258
|
28 |
|
if (!empty($this->getAccessToken())) { |
259
|
14 |
|
$defaults['access_token'] = $this->getAccessToken(); |
260
|
14 |
|
} |
261
|
28 |
|
return ['query' => array_merge($defaults, $query)]; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Set Json |
266
|
|
|
* |
267
|
|
|
* @param object $object |
268
|
|
|
* |
269
|
|
|
* @return array |
270
|
|
|
*/ |
271
|
6 |
|
private function setBody($object) |
272
|
|
|
{ |
273
|
6 |
|
$json = $this->serializer->serialize($object, 'json'); |
274
|
6 |
|
return ['body' => $json]; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.