Completed
Push — master ( d875a2...1a13b0 )
by Mauro
02:23
created

MercadoLibreClient::itemCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
crap 1
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\Item;
17
use Zephia\MercadoLibre\Entity\User;
18
19
/**
20
 * Class MercadoLibreClient
21
 *
22
 * @package Zephia\MercadoLibre\Client
23
 * @author  Mauro Moreno <[email protected]>
24
 */
25
class MercadoLibreClient
26
{
27
    // Mercado Libre API URI.
28
    const BASE_URI = 'https://api.mercadolibre.com';
29
30
    /**
31
     * Guzzle Client
32
     *
33
     * @var GuzzleClient
34
     */
35
    private $guzzleClient;
36
37
    /**
38
     * Access Token from MercadoLibre OAuth
39
     *
40
     * @var string
41
     */
42
    private $access_token;
43
44
    /**
45
     * Serializer
46
     *
47
     * @var SerializerInterface
48
     */
49
    private $serializer;
50
51
    /**
52
     * MercadoLibreClient constructor.
53
     *
54
     * @param array $config
55
     * @param SerializerInterface $serializer
56
     */
57 21
    public function __construct(
58
        array $config = [],
59
        SerializerInterface $serializer
60
    ) {
61 21
        $defaults = ['base_uri' => self::BASE_URI];
62 21
        $config = array_merge($defaults, $config);
63
64 21
        $this->guzzleClient = new GuzzleClient($config);
65 21
        $this->serializer = $serializer;
66 21
    }
67
68
    /**
69
     * Get Guzzle client
70
     *
71
     * @return GuzzleClient
72
     */
73 20
    public function getGuzzleClient()
74
    {
75 20
        return $this->guzzleClient;
76
    }
77
78
    /**
79
     * Set MercadoLibre Access Token
80
     *
81
     * @param string $access_token
82
     *
83
     * @return $this
84
     */
85 12
    public function setAccessToken($access_token)
86
    {
87 12
        $this->access_token = $access_token;
88 12
        return $this;
89
    }
90
91
    /**
92
     * Get MercadoLibre Access Token
93
     *
94
     * @return string
95
     */
96 20
    public function getAccessToken()
97
    {
98 20
        return $this->access_token;
99
    }
100
101
    /**
102
     * User show resource
103
     *
104
     * @param $customer_id
105
     *
106
     * @return array|\JMS\Serializer\scalar|object
107
     */
108 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...
109
    {
110 9
        $response = $this->getGuzzleClient()
111 9
            ->get('/users/' . $customer_id, $this->setQuery());
112
113 3
        return $this->serializer->deserialize(
114 3
            $response->getBody()->getContents(),
115 3
            User::class, 'json'
116 3
        );
117
    }
118
119
    /**
120
     * User show me resource
121
     *
122
     * @return array|\JMS\Serializer\scalar|object
123
     */
124 3
    public function userShowMe()
125
    {
126 3
        return $this->userShow('me');
127
    }
128
129
    /**
130
     * Category list resource
131
     *
132
     * @param $site_id string
133
     *
134
     * @return array|\JMS\Serializer\scalar|object
135
     */
136 2
    public function categoryList($site_id)
137
    {
138 2
        $response = $this->getGuzzleClient()
139 2
            ->get('/sites/' . $site_id . '/categories', $this->setQuery());
140
141 1
        return $this->serializer->deserialize(
142 1
            $response->getBody()->getContents(),
143 1
            "array<" . Category::class . ">",
144
            'json'
145 1
        );
146
    }
147
148
    /**
149
     * Item Show resource
150
     *
151
     * @param $item_id
152
     *
153
     * @return array|\JMS\Serializer\scalar|object
154
     */
155 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...
156
    {
157 2
        $response = $this->getGuzzleClient()
158 2
            ->get('/items/' . $item_id, $this->setQuery());
159
160 1
        return $this->serializer->deserialize(
161 1
            $response->getBody()->getContents(),
162 1
            Item::class,
163
            'json'
164 1
        );
165
    }
166
167
    /**
168
     * Item create resource
169
     *
170
     * @param $item
171
     *
172
     * @return array|\JMS\Serializer\scalar|object
173
     */
174 6 View Code Duplication
    public function itemCreate($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...
175
    {
176 6
        $response = $this->getGuzzleClient()
177 6
            ->post(
178 6
                '/items',
179 6
                array_merge($this->setQuery(), $this->setBody($item))
180 6
            );
181
182 1
        return $this->serializer->deserialize(
183 1
            $response->getBody()->getContents(),
184 1
            Item::class,
185
            'json'
186 1
        );
187
    }
188
189
    /**
190
     * Set query
191
     *
192
     * @param array $query
193
     *
194
     * @return array
195
     */
196 19
    private function setQuery(array $query = [])
197
    {
198 19
        $defaults = [];
199 19
        if (!empty($this->getAccessToken())) {
200 11
            $defaults['access_token'] = $this->getAccessToken();
201 11
        }
202 19
        return ['query' => array_merge($defaults, $query)];
203
    }
204
205
    /**
206
     * Set Json
207
     *
208
     * @param $object
209
     *
210
     * @return array
211
     */
212 6
    private function setBody($object)
213
    {
214 6
        $json = $this->serializer->serialize($object, 'json');
215 6
        return ['body' => $json];
216
    }
217
}
218