Completed
Push — master ( d84f32...0fcf92 )
by Mauro
02:24
created

MercadoLibreClient::setQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
/*
3
 * This file is part of the Mercado Libre API client package.
4
 *
5
 * (c) Mauro Moreno <[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
15
/**
16
 * Class MercadoLibreClient
17
 *
18
 * @package Zephia\MercadoLibre\Client
19
 * @author  Mauro Moreno <[email protected]>
20
 */
21
class MercadoLibreClient
22
{
23
    // Mercado Libre API URI.
24
    const BASE_URI = 'https://api.mercadolibre.com';
25
26
    /**
27
     * Guzzle Client
28
     *
29
     * @var GuzzleClient
30
     */
31
    private $guzzleClient;
32
33
    /**
34
     * Access Token from MercadoLibre OAuth
35
     *
36
     * @var string
37
     */
38 4
    private $access_token;
39
40 4
    /**
41
     * MercadoLibreClient constructor.
42 4
     *
43
     * @param array $config
44 4
     */
45 4
    public function __construct(array $config = [])
46
    {
47
        $defaults = ['base_uri' => self::BASE_URI];
48
        $config = array_merge($defaults, $config);
49
50
        $this->guzzleClient = new GuzzleClient($config);
51
    }
52 4
53
    /**
54 4
     * Get Guzzle client
55
     *
56
     * @return GuzzleClient
57
     */
58
    public function getGuzzleClient()
59
    {
60
        return $this->guzzleClient;
61
    }
62
63
    /**
64 3
     * @param string $access_token
65 1
     *
66 3
     * @return $this
67
     */
68
    public function setAccessToken(string $access_token)
69
    {
70
        $this->access_token = $access_token;
71
        return $this;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getAccessToken()
78
    {
79
        return $this->access_token;
80
    }
81
82
    /**
83
     * Show User resource
84
     *
85
     * @param $customer_id
86
     *
87
     * @return \Psr\Http\Message\ResponseInterface
88
     */
89
    public function showUser($customer_id)
90
    {
91
        $query = $this->setQuery([]);
92
93
        return $this->getGuzzleClient()->get('/users/' . $customer_id, $query);
94
    }
95
96
    /**
97
     * Set query
98
     *
99
     * @param array $query
100
     *
101
     * @return array
102
     */
103
    private function setQuery(array $query = [])
104
    {
105
        $defaults = [];
106
        if (!empty($this->getAccessToken())) {
107
            $defaults['access_token'] = $this->getAccessToken();
108
        }
109
        return ['query' => array_merge($defaults, $query)];
110
    }
111
}
112