Completed
Push — master ( 048651...40ad8f )
by Mauro
30s
created

MercadoLibreClient::itemUpdate()   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 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\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)
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...
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)
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...
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)
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...
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)
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...
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)
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...
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
     * Item create resource
250
     *
251
     * @param string $item_id
252
     *
253
     * @return array|\JMS\Serializer\scalar|object
254
     */
255 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...
256
    {
257
        // TODO: Tests
258
        $response = $this->getGuzzleClient()
259
            ->put(
260
                '/items/' . $item_id,
261
                array_merge($this->setQuery(), $this->setBody($fields))
262
            );
263
264
        return $this->serializer->deserialize(
265
            $response->getBody()->getContents(),
266
            Item::class,
267
            'json'
268
        );
269
    }
270
271
    /**
272
     * Set query
273
     *
274
     * @param array $query
275
     *
276
     * @return array
277
     */
278 28
    private function setQuery(array $query = [])
279
    {
280 28
        $defaults = [];
281 28
        if (!empty($this->getAccessToken())) {
282 14
            $defaults['access_token'] = $this->getAccessToken();
283 14
        }
284 28
        return ['query' => array_merge($defaults, $query)];
285
    }
286
287
    /**
288
     * Set Json
289
     *
290
     * @param object $object
291
     *
292
     * @return array
293
     */
294 6
    private function setBody($object)
295
    {
296 6
        $json = $this->serializer->serialize($object, 'json');
297 6
        return ['body' => $json];
298
    }
299
}
300