Completed
Push — master ( 5c39af...2e0312 )
by Mauro
43s
created

MercadoLibreClient   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 222
Duplicated Lines 20.72 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 46
loc 222
ccs 65
cts 65
cp 1
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A getGuzzleClient() 0 4 1
A setAccessToken() 0 5 1
A getAccessToken() 0 4 1
A userShow() 10 11 1
A userShowMe() 0 4 1
A categoryList() 0 11 1
A itemList() 11 11 1
A itemShow() 11 11 1
A itemCreate() 14 14 1
A setQuery() 0 8 2
A setBody() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 GuzzleHttp\ClientInterface;
15
use JMS\Serializer\SerializerInterface;
16
use Zephia\MercadoLibre\Entity\Category;
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 26
    public function __construct(
72
        array $config = [],
73
        SerializerInterface $serializer
74
    ) {
75
        $defaults = [
76 26
            'base_uri' => self::BASE_URI,
77 26
            'base_url' => self::BASE_URI,
78 26
        ];
79 26
        $config = array_merge($defaults, $config);
80
81 26
        $this->guzzleClient = new GuzzleClient($config);
82 26
        $this->serializer = $serializer;
83 26
    }
84
85
    /**
86
     * Get Guzzle client
87
     *
88
     * @return GuzzleClient
89
     */
90 25
    public function getGuzzleClient()
91
    {
92 25
        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 25
    public function getAccessToken()
114
    {
115 25
        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 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...
167
    {
168 5
        $response = $this->getGuzzleClient()
169 5
            ->get('/users/' . $user_id . '/items/search', $this->setQuery());
170
171 1
        return $this->serializer->deserialize(
172 1
            $response->getBody()->getContents(),
173 1
            ItemList::class,
174
            'json'
175 1
        );
176
    }
177
178
    /**
179
     * Item Show resource
180
     *
181
     * @param $item_id
182
     *
183
     * @return array|\JMS\Serializer\scalar|object
184
     */
185 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...
186
    {
187 2
        $response = $this->getGuzzleClient()
188 2
            ->get('/items/' . $item_id, $this->setQuery());
189
190 1
        return $this->serializer->deserialize(
191 1
            $response->getBody()->getContents(),
192 1
            Item::class,
193
            'json'
194 1
        );
195
    }
196
197
    /**
198
     * Item create resource
199
     *
200
     * @param Item $item
201
     *
202
     * @return array|\JMS\Serializer\scalar|object
203
     */
204 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...
205
    {
206 6
        $response = $this->getGuzzleClient()
207 6
            ->post(
208 6
                '/items',
209 6
                array_merge($this->setQuery(), $this->setBody($item))
210 6
            );
211
212 1
        return $this->serializer->deserialize(
213 1
            $response->getBody()->getContents(),
214 1
            Item::class,
215
            'json'
216 1
        );
217
    }
218
219
    /**
220
     * Set query
221
     *
222
     * @param array $query
223
     *
224
     * @return array
225
     */
226 24
    private function setQuery(array $query = [])
227
    {
228 24
        $defaults = [];
229 24
        if (!empty($this->getAccessToken())) {
230 14
            $defaults['access_token'] = $this->getAccessToken();
231 14
        }
232 24
        return ['query' => array_merge($defaults, $query)];
233
    }
234
235
    /**
236
     * Set Json
237
     *
238
     * @param object $object
239
     *
240
     * @return array
241
     */
242 6
    private function setBody($object)
243
    {
244 6
        $postKey = (version_compare(ClientInterface::VERSION, '6') === 1) ? 'body' : 'form_params';
245 6
        $json = $this->serializer->serialize($object, 'json');
246 6
        return [$postKey => $json];
247
    }
248
}
249