Completed
Push — master ( b30d6c...e07405 )
by Mauro
32s
created

MercadoLibreClient::itemRelist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 18
loc 18
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 2
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
     * @param $filters
143
     *
144
     * @return array|\JMS\Serializer\scalar|object
145
     */
146 View Code Duplication
    public function userPackages($user_id, $filters = [])
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...
147
    {
148
        // TODO: Tests
149
        $response = $this->getGuzzleClient()
150
            ->get(
151
                '/users/' . $user_id . '/classifieds_promotion_packs',
152
                $this->setQuery($filters)
153
            );
154
155
        return $this->serializer->deserialize(
156
            $response->getBody()->getContents(),
157
            "array<" . Package::class . ">",
158
            'json'
159
        );
160
    }
161
162
    /**
163
     * User show me resource
164
     *
165
     * @return array|\JMS\Serializer\scalar|object
166
     */
167 3
    public function userShowMe()
168
    {
169 3
        return $this->userShow('me');
170
    }
171
172
    /**
173
     * Category list resource
174
     *
175
     * @param $site_id string
176
     *
177
     * @return array|\JMS\Serializer\scalar|object
178
     */
179 2
    public function categoryList($site_id)
180
    {
181 2
        $response = $this->getGuzzleClient()
182 2
            ->get('/sites/' . $site_id . '/categories', $this->setQuery());
183
184 1
        return $this->serializer->deserialize(
185 1
            $response->getBody()->getContents(),
186 1
            "array<" . Category::class . ">",
187
            'json'
188 1
        );
189
    }
190
191
    /**
192
     * Category Predict resource
193
     *
194
     * @param $site_id string
195
     * @param $title   string
196
     *
197
     * @return array|\JMS\Serializer\scalar|object
198
     */
199 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...
200
    {
201 4
        $response = $this->getGuzzleClient()
202 4
            ->get(
203 4
                '/sites/' . $site_id . '/category_predictor/predict',
204 4
                $this->setQuery(['title' => $title])
205 4
            );
206 1
        return $this->serializer->deserialize(
207 1
            $response->getBody()->getContents(),
208 1
            CategoryPrediction::class,
209
            'json'
210 1
        );
211
    }
212
213
    /**
214
     * Item List resource
215
     *
216
     * @param $user_id string
217
     *
218
     * @return array|\JMS\Serializer\scalar|object
219
     */
220 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...
221
    {
222 5
        $response = $this->getGuzzleClient()
223 5
            ->get('/users/' . $user_id . '/items/search', $this->setQuery());
224
225 1
        return $this->serializer->deserialize(
226 1
            $response->getBody()->getContents(),
227 1
            ItemList::class,
228
            'json'
229 1
        );
230
    }
231
232
    /**
233
     * Item Show resource
234
     *
235
     * @param $item_id
236
     *
237
     * @return array|\JMS\Serializer\scalar|object
238
     */
239 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...
240
    {
241 2
        $response = $this->getGuzzleClient()
242 2
            ->get('/items/' . $item_id, $this->setQuery());
243
244 1
        return $this->serializer->deserialize(
245 1
            $response->getBody()->getContents(),
246 1
            Item::class,
247
            'json'
248 1
        );
249
    }
250
251
    /**
252
     * Item create resource
253
     *
254
     * @param Item $item
255
     *
256
     * @return array|\JMS\Serializer\scalar|object
257
     */
258 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...
259
    {
260 6
        $response = $this->getGuzzleClient()
261 6
            ->post(
262 6
                '/items',
263 6
                array_merge($this->setQuery(), $this->setBody($item))
264 6
            );
265
266 1
        return $this->serializer->deserialize(
267 1
            $response->getBody()->getContents(),
268 1
            Item::class,
269
            'json'
270 1
        );
271
    }
272
273
    /**
274
     * Item create resource
275
     *
276
     * @param string $item_id
277
     *
278
     * @return array|\JMS\Serializer\scalar|object
279
     */
280 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...
281
    {
282
        // TODO: Tests
283
        $response = $this->getGuzzleClient()
284
            ->put(
285
                '/items/' . $item_id,
286
                array_merge($this->setQuery(), $this->setBodyArray($fields))
287
            );
288
289
        return $this->serializer->deserialize(
290
            $response->getBody()->getContents(),
291
            Item::class,
292
            'json'
293
        );
294
    }
295
296
    /**
297
     * Item delete resource
298
     *
299
     * @param $item_id
300
     *
301
     * @return array|\JMS\Serializer\scalar|object
302
     */
303
    public function itemDelete($item_id)
304
    {
305
        return $this->itemUpdate($item_id, ['deleted' => true]);
306
    }
307
308
    /**
309
     * Item close resource
310
     *
311
     * @param $item_id
312
     *
313
     * @return array|\JMS\Serializer\scalar|object
314
     */
315
    public function itemClose($item_id)
316
    {
317
        return $this->itemUpdate($item_id, ['closed' => true]);
318
    }
319
320
    /**
321
     * Item Relist
322
     *
323
     * @param string $item_id
324
     * @param array $fields
325
     *
326
     * @return array|\JMS\Serializer\scalar|object
327
     */
328 View Code Duplication
    public function itemRelist($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...
329
    {
330
        // TODO: Tests
331
        $response = $this->getGuzzleClient()
332
            ->post(
333
                '/items/' . $item_id . '/relist',
334
                array_merge(
335
                    $this->setQuery(),
336
                    $this->setBodyArray($fields)
337
                )
338
            );
339
340
        return $this->serializer->deserialize(
341
            $response->getBody()->getContents(),
342
            Item::class,
343
            'json'
344
        );
345
    }
346
347
    /**
348
     * Item update listing type
349
     *
350
     * @param string $item_id
351
     * @param string $listing_type
352
     *
353
     * @return array|\JMS\Serializer\scalar|object
354
     */
355 View Code Duplication
    public function itemUpdateListingType($item_id, $listing_type)
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...
356
    {
357
        // TODO: Tests
358
        $response = $this->getGuzzleClient()
359
            ->post(
360
                '/items/' . $item_id . '/listing_type',
361
                array_merge(
362
                    $this->setQuery(),
363
                    $this->setBodyArray(['id' => $listing_type])
364
                )
365
            );
366
367
        return $this->serializer->deserialize(
368
            $response->getBody()->getContents(),
369
            Item::class,
370
            'json'
371
        );
372
    }
373
374
    /**
375
     * Set query
376
     *
377
     * @param array $query
378
     *
379
     * @return array
380
     */
381 28
    private function setQuery(array $query = [])
382
    {
383 28
        $defaults = [];
384 28
        if (!empty($this->getAccessToken())) {
385 14
            $defaults['access_token'] = $this->getAccessToken();
386 14
        }
387 28
        return ['query' => array_merge($defaults, $query)];
388
    }
389
390
    /**
391
     * Set Json
392
     *
393
     * @param object $object
394
     *
395
     * @return array
396
     */
397 6
    private function setBody($object)
398
    {
399 6
        $json = $this->serializer->serialize($object, 'json');
400 6
        return ['body' => $json];
401
    }
402
403
    /**
404
     * Set Body Array
405
     *
406
     * @param array $fields
407
     *
408
     * @return array
409
     */
410
    private function setBodyArray($fields = [])
411
    {
412
        return ['body' => json_encode($fields)];
413
    }
414
}
415