Completed
Push — master ( 0fcf92...816db7 )
by Mauro
02:15
created

MercadoLibreClient::getAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
    private $access_token;
39
40
    /**
41
     * MercadoLibreClient constructor.
42
     *
43
     * @param array $config
44
     */
45 8
    public function __construct(array $config = [])
46
    {
47 8
        $defaults = ['base_uri' => self::BASE_URI];
48 8
        $config = array_merge($defaults, $config);
49
50 8
        $this->guzzleClient = new GuzzleClient($config);
51 8
    }
52
53
    /**
54
     * Get Guzzle client
55
     *
56
     * @return GuzzleClient
57
     */
58 7
    public function getGuzzleClient()
59
    {
60 7
        return $this->guzzleClient;
61
    }
62
63
    /**
64
     * @param string $access_token
65
     *
66
     * @return $this
67
     */
68 4
    public function setAccessToken($access_token)
69
    {
70 4
        $this->access_token = $access_token;
71 4
        return $this;
72
    }
73
74
    /**
75
     * @return string
76
     */
77 7
    public function getAccessToken()
78
    {
79 7
        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 6
    public function showUser($customer_id)
90
    {
91 6
        $query = $this->setQuery([]);
92
93 6
        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 6
    private function setQuery(array $query = [])
104
    {
105 6
        $defaults = [];
106 6
        if (!empty($this->getAccessToken())) {
107 3
            $defaults['access_token'] = $this->getAccessToken();
108 3
        }
109 6
        return ['query' => array_merge($defaults, $query)];
110
    }
111
}
112