Completed
Pull Request — master (#9)
by Mauro
02:10
created

MercadoLibreClient::userPackages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 15
loc 15
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
crap 2
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\Package;
20
use Zephia\MercadoLibre\Entity\User;
21
22
/**
23
 * Class MercadoLibreClient
24
 *
25
 * @package Zephia\MercadoLibre\Client
26
 * @author  Mauro Moreno <[email protected]>
27
 */
28
class MercadoLibreClient
29
{
30
    /**
31
     * MercadoLibre API URI.
32
     */
33
    const BASE_URI = 'https://api.mercadolibre.com';
34
35
    /**
36
     * MercadoLibre Authorization URI.
37
     */
38
    const AUTH_URI = 'http://auth.mercadolibre.com/authorization';
39
40
    /**
41
     * MercadoLibre OAuth URI.
42
     */
43
    const OAUTH_URI = '/oauth/token';
44
45
    /**
46
     * Guzzle Client
47
     *
48
     * @var GuzzleClient
49
     */
50
    private $guzzleClient;
51
52
    /**
53
     * Access Token from MercadoLibre OAuth
54
     *
55
     * @var string
56
     */
57
    private $access_token;
58
59
    /**
60
     * Serializer
61
     *
62
     * @var SerializerInterface
63
     */
64
    private $serializer;
65
66
    /**
67
     * MercadoLibreClient constructor.
68
     *
69
     * @param array $config
70
     * @param SerializerInterface $serializer
71
     */
72 30
    public function __construct(
73
        array $config = [],
74
        SerializerInterface $serializer
75
    ) {
76
        $defaults = [
77 30
            'base_uri' => self::BASE_URI,
78 30
            'base_url' => self::BASE_URI,
79 30
        ];
80 30
        $config = array_merge($defaults, $config);
81
82 30
        $this->guzzleClient = new GuzzleClient($config);
83 30
        $this->serializer = $serializer;
84 30
    }
85
86
    /**
87
     * Get Guzzle client
88
     *
89
     * @return GuzzleClient
90
     */
91 29
    public function getGuzzleClient()
92
    {
93 29
        return $this->guzzleClient;
94
    }
95
96
    /**
97
     * Set MercadoLibre Access Token
98
     *
99
     * @param string $access_token
100
     *
101
     * @return $this
102
     */
103 15
    public function setAccessToken($access_token)
104
    {
105 15
        $this->access_token = $access_token;
106 15
        return $this;
107
    }
108
109
    /**
110
     * Get MercadoLibre Access Token
111
     *
112
     * @return string
113
     */
114 29
    public function getAccessToken()
115
    {
116 29
        return $this->access_token;
117
    }
118
119
    /**
120
     * User show resource
121
     *
122
     * @param $user_id
123
     *
124
     * @return array|\JMS\Serializer\scalar|object
125
     */
126 9 View Code Duplication
    public function userShow($user_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
127
    {
128 9
        $response = $this->getGuzzleClient()
129 9
            ->get('/users/' . $user_id, $this->setQuery());
130
131 3
        return $this->serializer->deserialize(
132 3
            $response->getBody()->getContents(),
133 3
            User::class,
134
            'json'
135 3
        );
136
    }
137
138
    /**
139
     * Hired packages by user
140
     *
141
     * @param $user_id
142
     *
143
     * @return array|\JMS\Serializer\scalar|object
144
     */
145 View Code Duplication
    public function userPackages($user_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
146
    {
147
        // TODO: Tests
148
        $response = $this->getGuzzleClient()
149
            ->get(
150
                '/users/' . $user_id . '/classifieds_promotion_packs',
151
                $this->setQuery()
152
            );
153
154
        return $this->serializer->deserialize(
155
            $response->getBody()->getContents(),
156
            "array<" . Package::class . ">",
157
            'json'
158
        );
159
    }
160
161
    /**
162
     * User show me resource
163
     *
164
     * @return array|\JMS\Serializer\scalar|object
165
     */
166 3
    public function userShowMe()
167
    {
168 3
        return $this->userShow('me');
169
    }
170
171
    /**
172
     * Category list resource
173
     *
174
     * @param $site_id string
175
     *
176
     * @return array|\JMS\Serializer\scalar|object
177
     */
178 2
    public function categoryList($site_id)
179
    {
180 2
        $response = $this->getGuzzleClient()
181 2
            ->get('/sites/' . $site_id . '/categories', $this->setQuery());
182
183 1
        return $this->serializer->deserialize(
184 1
            $response->getBody()->getContents(),
185 1
            "array<" . Category::class . ">",
186
            'json'
187 1
        );
188
    }
189
190
    /**
191
     * Category Predict resource
192
     *
193
     * @param $site_id string
194
     * @param $title   string
195
     *
196
     * @return array|\JMS\Serializer\scalar|object
197
     */
198 4 View Code Duplication
    public function categoryPredict($site_id, $title)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
199
    {
200 4
        $response = $this->getGuzzleClient()
201 4
            ->get(
202 4
                '/sites/' . $site_id . '/category_predictor/predict',
203 4
                $this->setQuery(['title' => $title])
204 4
            );
205 1
        return $this->serializer->deserialize(
206 1
            $response->getBody()->getContents(),
207 1
            CategoryPrediction::class,
208
            'json'
209 1
        );
210
    }
211
212
    /**
213
     * Item List resource
214
     *
215
     * @param $user_id string
216
     *
217
     * @return array|\JMS\Serializer\scalar|object
218
     */
219 5 View Code Duplication
    public function itemList($user_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
220
    {
221 5
        $response = $this->getGuzzleClient()
222 5
            ->get('/users/' . $user_id . '/items/search', $this->setQuery());
223
224 1
        return $this->serializer->deserialize(
225 1
            $response->getBody()->getContents(),
226 1
            ItemList::class,
227
            'json'
228 1
        );
229
    }
230
231
    /**
232
     * Item Show resource
233
     *
234
     * @param $item_id
235
     *
236
     * @return array|\JMS\Serializer\scalar|object
237
     */
238 2 View Code Duplication
    public function itemShow($item_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
239
    {
240 2
        $response = $this->getGuzzleClient()
241 2
            ->get('/items/' . $item_id, $this->setQuery());
242
243 1
        return $this->serializer->deserialize(
244 1
            $response->getBody()->getContents(),
245 1
            Item::class,
246
            'json'
247 1
        );
248
    }
249
250
    /**
251
     * Item create resource
252
     *
253
     * @param Item $item
254
     *
255
     * @return array|\JMS\Serializer\scalar|object
256
     */
257 6 View Code Duplication
    public function itemCreate(Item $item)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
258
    {
259 6
        $response = $this->getGuzzleClient()
260 6
            ->post(
261 6
                '/items',
262 6
                array_merge($this->setQuery(), $this->setBody($item))
263 6
            );
264
265 1
        return $this->serializer->deserialize(
266 1
            $response->getBody()->getContents(),
267 1
            Item::class,
268
            'json'
269 1
        );
270
    }
271
272
    /**
273
     * Item create resource
274
     *
275
     * @param string $item_id
276
     *
277
     * @return array|\JMS\Serializer\scalar|object
278
     */
279 View Code Duplication
    public function itemUpdate($item_id, $fields)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
280
    {
281
        // TODO: Tests
282
        $response = $this->getGuzzleClient()
283
            ->put(
284
                '/items/' . $item_id,
285
                array_merge($this->setQuery(), $this->setBody($fields))
286
            );
287
288
        return $this->serializer->deserialize(
289
            $response->getBody()->getContents(),
290
            Item::class,
291
            'json'
292
        );
293
    }
294
295
    /**
296
     * Set query
297
     *
298
     * @param array $query
299
     *
300
     * @return array
301
     */
302 28
    private function setQuery(array $query = [])
303
    {
304 28
        $defaults = [];
305 28
        if (!empty($this->getAccessToken())) {
306 14
            $defaults['access_token'] = $this->getAccessToken();
307 14
        }
308 28
        return ['query' => array_merge($defaults, $query)];
309
    }
310
311
    /**
312
     * Set Json
313
     *
314
     * @param object $object
315
     *
316
     * @return array
317
     */
318 6
    private function setBody($object)
319
    {
320 6
        $json = $this->serializer->serialize($object, 'json');
321 6
        return ['body' => $json];
322
    }
323
}
324