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\User; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class MercadoLibreClient |
20
|
|
|
* |
21
|
|
|
* @package Zephia\MercadoLibre\Client |
22
|
|
|
* @author Mauro Moreno <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class MercadoLibreClient |
25
|
|
|
{ |
26
|
|
|
// Mercado Libre API URI. |
27
|
|
|
const BASE_URI = 'https://api.mercadolibre.com'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Guzzle Client |
31
|
|
|
* |
32
|
|
|
* @var GuzzleClient |
33
|
|
|
*/ |
34
|
|
|
private $guzzleClient; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Access Token from MercadoLibre OAuth |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $access_token; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Serializer |
45
|
|
|
* |
46
|
|
|
* @var SerializerInterface |
47
|
|
|
*/ |
48
|
|
|
private $serializer; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* MercadoLibreClient constructor. |
52
|
|
|
* |
53
|
|
|
* @param array $config |
54
|
|
|
* @param SerializerInterface $serializer |
55
|
|
|
*/ |
56
|
13 |
|
public function __construct( |
57
|
|
|
array $config = [], |
58
|
|
|
SerializerInterface $serializer |
59
|
|
|
) { |
60
|
13 |
|
$defaults = ['base_uri' => self::BASE_URI]; |
61
|
13 |
|
$config = array_merge($defaults, $config); |
62
|
|
|
|
63
|
13 |
|
$this->guzzleClient = new GuzzleClient($config); |
64
|
13 |
|
$this->serializer = $serializer; |
65
|
13 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get Guzzle client |
69
|
|
|
* |
70
|
|
|
* @return GuzzleClient |
71
|
|
|
*/ |
72
|
12 |
|
public function getGuzzleClient() |
73
|
|
|
{ |
74
|
12 |
|
return $this->guzzleClient; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $access_token |
79
|
|
|
* |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
7 |
|
public function setAccessToken($access_token) |
83
|
|
|
{ |
84
|
7 |
|
$this->access_token = $access_token; |
85
|
7 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
12 |
|
public function getAccessToken() |
92
|
|
|
{ |
93
|
12 |
|
return $this->access_token; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Show User resource |
98
|
|
|
* |
99
|
|
|
* @param $customer_id |
100
|
|
|
* |
101
|
|
|
* @return array|\JMS\Serializer\scalar|object |
102
|
|
|
*/ |
103
|
6 |
View Code Duplication |
public function userShow($customer_id) |
|
|
|
|
104
|
|
|
{ |
105
|
6 |
|
$response = $this->getGuzzleClient() |
106
|
6 |
|
->get('/users/' . $customer_id, $this->setQuery()); |
107
|
|
|
|
108
|
2 |
|
return $this->serializer->deserialize( |
109
|
2 |
|
$response->getBody()->getContents(), |
110
|
2 |
|
User::class, 'json' |
111
|
2 |
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Show User me resource |
116
|
|
|
* |
117
|
|
|
* @return array|\JMS\Serializer\scalar|object |
118
|
|
|
*/ |
119
|
3 |
View Code Duplication |
public function userShowMe() |
|
|
|
|
120
|
|
|
{ |
121
|
3 |
|
$response = $this->getGuzzleClient() |
122
|
3 |
|
->get('/users/me', $this->setQuery()); |
123
|
|
|
|
124
|
1 |
|
return $this->serializer->deserialize( |
125
|
1 |
|
$response->getBody()->getContents(), |
126
|
1 |
|
User::class, 'json' |
127
|
1 |
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* List Categories resource |
132
|
|
|
* |
133
|
|
|
* @param $site_id string |
134
|
|
|
* |
135
|
|
|
* @return array|\JMS\Serializer\scalar|object |
136
|
|
|
*/ |
137
|
2 |
|
public function categoryList($site_id) |
138
|
|
|
{ |
139
|
2 |
|
$response = $this->getGuzzleClient() |
140
|
2 |
|
->get('/sites/' . $site_id . '/categories', $this->setQuery()); |
141
|
|
|
|
142
|
1 |
|
return $this->serializer->deserialize( |
143
|
1 |
|
$response->getBody()->getContents(), |
144
|
1 |
|
"array<" . Category::class . ">", |
145
|
|
|
'json' |
146
|
1 |
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set query |
151
|
|
|
* |
152
|
|
|
* @param array $query |
153
|
|
|
* |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
11 |
|
private function setQuery(array $query = []) |
157
|
|
|
{ |
158
|
11 |
|
$defaults = []; |
159
|
11 |
|
if (!empty($this->getAccessToken())) { |
160
|
6 |
|
$defaults['access_token'] = $this->getAccessToken(); |
161
|
6 |
|
} |
162
|
11 |
|
return ['query' => array_merge($defaults, $query)]; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.