GuzzleClient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
crap 1
1
<?php
2
3
namespace MercadoPago\Http;
4
5
use GuzzleHttp\Client as GuzzleHttpClient;
6
use GuzzleHttp\Exception\ClientException;
7
use MercadoPago\MercadoPago;
8
9
class GuzzleClient extends Client
10
{
11
    /**
12
     * @var GuzzleHttpClient
13
     */
14
    protected $client;
15
16
    /**
17
     * GuzzleClient constructor.
18
     */
19 54
    public function __construct()
20
    {
21 54
        $this->client = new GuzzleHttpClient([
22 54
            'base_uri' => self::API_BASE_URL,
23
            'headers' => [
24
                'User-Agent' => 'MercadoPago PHP SDK v' . MercadoPago::VERSION,
25
                'Content-Type' => 'application/json',
26
                'Accept' => 'application/json',
27
            ],
28
        ]);
29 54
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34 24
    public function request($uri = '/', $method = 'GET', array $data = [], array $params = [])
35
    {
36 24
        $options = [];
37
38 24
        if ($method !== self::METHOD_GET && !empty($data)) {
39 15
            if (empty($params['form'])) {
40 9
                $options['json'] = $data;
41
            } else {
42 6
                $options['form_params'] = $data;
43
            }
44
        }
45
46 24
        if ($method === self::METHOD_GET && !empty($data)) {
47 3
            $options['query'] = $data;
48
        }
49
50 24
        if (!empty($params['access_token'])) {
51 18
            $options['query']['access_token'] = $params['access_token'];
52
        }
53
54
        try {
55 24
            $response = $this->client->request($method, $uri, $options);
56 12
        } catch (ClientException $exception) {
57 12
            $response = $exception->getResponse();
58
        }
59
60 24
        return new Response($response->getStatusCode(), $response->getBody());
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 9
    public function get($uri = '/', array $data = [], array $params = [])
67
    {
68 9
        return $this->request($uri, self::METHOD_GET, $data, $params);
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 15
    public function post($uri = '/', array $data = [], array $params = [])
75
    {
76 15
        return $this->request($uri, self::METHOD_POST, $data, $params);
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public function put($uri = '/', array $data = [], array $params = [])
83
    {
84
        return $this->request($uri, self::METHOD_PUT, $data, $params);
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function delete($uri = '/', array $data = [], array $params = [])
91
    {
92
        return $this->request($uri, self::METHOD_DELETE, $data, $params);
93
    }
94
}
95