Completed
Push — master ( 292675...df4750 )
by Mauro
02:31
created

MercadoLibreClient::showUserMe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
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\User;
16
17
/**
18
 * Class MercadoLibreClient
19
 *
20
 * @package Zephia\MercadoLibre\Client
21
 * @author  Mauro Moreno <[email protected]>
22
 */
23
class MercadoLibreClient
24
{
25
    // Mercado Libre API URI.
26
    const BASE_URI = 'https://api.mercadolibre.com';
27
28
    /**
29
     * Guzzle Client
30
     *
31
     * @var GuzzleClient
32
     */
33
    private $guzzleClient;
34
35
    /**
36
     * Access Token from MercadoLibre OAuth
37
     *
38
     * @var string
39
     */
40
    private $access_token;
41
42
    /**
43
     * Serializer
44
     *
45
     * @var SerializerInterface
46
     */
47
    private $serializer;
48
49
    /**
50
     * MercadoLibreClient constructor.
51
     *
52
     * @param array $config
53
     * @param SerializerInterface $serializer
54
     */
55 11
    public function __construct(
56
        array $config = [],
57
        SerializerInterface $serializer
58
    ) {
59 11
        $defaults = ['base_uri' => self::BASE_URI];
60 11
        $config = array_merge($defaults, $config);
61
62 11
        $this->guzzleClient = new GuzzleClient($config);
63 11
        $this->serializer = $serializer;
64 11
    }
65
66
    /**
67
     * Get Guzzle client
68
     *
69
     * @return GuzzleClient
70
     */
71 10
    public function getGuzzleClient()
72
    {
73 10
        return $this->guzzleClient;
74
    }
75
76
    /**
77
     * @param string $access_token
78
     *
79
     * @return $this
80
     */
81 7
    public function setAccessToken($access_token)
82
    {
83 7
        $this->access_token = $access_token;
84 7
        return $this;
85
    }
86
87
    /**
88
     * @return string
89
     */
90 10
    public function getAccessToken()
91
    {
92 10
        return $this->access_token;
93 1
    }
94
95
    /**
96
     * Show User resource
97
     *
98
     * @param $customer_id
99
     *
100
     * @return object
101
     */
102 6 View Code Duplication
    public function showUser($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...
103 1
    {
104 6
        $response = $this->getGuzzleClient()
105 6
            ->get('/users/' . $customer_id, $this->setQuery());
106
107 2
        return $this->serializer->deserialize(
108 2
            $response->getBody()->getContents(),
109 2
            User::class, 'json'
110 2
        );
111
    }
112
113 3 View Code Duplication
    public function showUserMe()
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...
114
    {
115 3
        $response = $this->getGuzzleClient()
116 3
            ->get('/users/me', $this->setQuery());
117
118 1
        return $this->serializer->deserialize(
119 1
            $response->getBody()->getContents(),
120 1
            User::class, 'json'
121 1
        );
122
    }
123
124
    /**
125
     * Set query
126
     *
127
     * @param array $query
128
     *
129
     * @return array
130
     */
131 9
    private function setQuery(array $query = [])
132
    {
133 9
        $defaults = [];
134 9
        if (!empty($this->getAccessToken())) {
135 6
            $defaults['access_token'] = $this->getAccessToken();
136 6
        }
137 9
        return ['query' => array_merge($defaults, $query)];
138
    }
139
}
140