Completed
Push — master ( 8aa23e...040690 )
by Sébastien
06:56
created

Client   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 122
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 6 1
A post() 0 6 1
A patch() 0 6 1
A delete() 0 6 1
A getBody() 0 10 3
A getUrl() 0 4 1
A getClient() 0 4 1
1
<?php
2
3
namespace Sebdesign\VivaPayments;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Psr7\Response;
7
8
class Client
9
{
10
    /**
11
     * Demo environment URL.
12
     */
13
    const DEMO_URL = 'https://demo.vivapayments.com';
14
15
    /**
16
     * Production environment URL.
17
     */
18
    const PRODUCTION_URL = 'https://www.vivapayments.com';
19
20
    /**
21
     * @var \GuzzleHttp\ClientInterface
22
     */
23
    protected $client;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param \GuzzleHttp\ClientInterface   $client
29
     */
30
    public function __construct(ClientInterface $client)
31
    {
32
        $this->client = $client;
33
    }
34
35
    /**
36
     * Make a GET request.
37
     *
38
     * @param  string $url
39
     * @param  array  $options
40
     * @return object
41
     */
42
    public function get($url, array $options = [])
43
    {
44
        $response = $this->client->get($url, $options);
45
46
        return $this->getBody($response);
47
    }
48
49
    /**
50
     * Make a POST request.
51
     *
52
     * @param  string $url
53
     * @param  array  $options
54
     * @return object
55
     */
56
    public function post($url, array $options = [])
57
    {
58
        $response = $this->client->post($url, $options);
59
60
        return $this->getBody($response);
61
    }
62
63
    /**
64
     * Make a PATCH request.
65
     *
66
     * @param  string $url
67
     * @param  array  $options
68
     * @return object
69
     */
70
    public function patch($url, array $options = [])
71
    {
72
        $response = $this->client->patch($url, $options);
73
74
        return $this->getBody($response);
75
    }
76
77
    /**
78
     * Make a DELETE request.
79
     *
80
     * @param  string $url
81
     * @param  array  $options
82
     * @return object
83
     */
84
    public function delete($url, array $options = [])
85
    {
86
        $response = $this->client->delete($url, $options);
87
88
        return $this->getBody($response);
89
    }
90
91
    /**
92
     * Get the response body.
93
     *
94
     * @param  \GuzzleHttp\Psr7\Response $response
95
     * @return mixed
96
     *
97
     * @throws \Sebdesign\VivaPayments\VivaException
98
     */
99
    protected function getBody(Response $response)
100
    {
101
        $body = json_decode($response->getBody(), false, 512, JSON_BIGINT_AS_STRING);
102
103
        if (isset($body->ErrorCode) && $body->ErrorCode !== 0) {
104
            throw new VivaException($body->ErrorText, $body->ErrorCode);
105
        }
106
107
        return $body;
108
    }
109
110
    /**
111
     * Get the URL.
112
     *
113
     * @return \GuzzleHttp\Psr7\Uri
114
     */
115
    public function getUrl()
116
    {
117
        return $this->client->getConfig('base_uri');
118
    }
119
120
    /**
121
     * Get the Guzzle client.
122
     *
123
     * @return \GuzzleHttp\Client
124
     */
125
    public function getClient()
126
    {
127
        return $this->client;
128
    }
129
}
130