Passed
Pull Request — master (#30)
by
unknown
04:06
created

Client::getBaseUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace RevolutPHP;
4
5
use GuzzleHttp\Client as GuzzleClient;
6
use GuzzleHttp\Psr7\Response;
7
use League\OAuth2\Client\Token\AccessToken;
8
use Psr\Http\Message\ResponseInterface;
9
10
class Client
11
{
12
13
    const REVOLUT_SANDBOX_ENDPOINT = 'https://sandbox-b2b.revolut.com/api/';
14
    const REVOLUT_PRODUCTION_ENDPOINT = 'https://b2b.revolut.com/api/';
15
16
    /**
17
     * @var \GuzzleHttp\Client
18
     */
19
    private $httpClient;
20
21
    /**
22
     * @var \League\OAuth2\Client\Token\AccessToken
23
     */
24
    private $accessToken;
25
26
    /**
27
     * @var string
28
     */
29
    private $mode;
30
31
    /**
32
     * @var array
33
     */
34
    private $clientOptions;
35
36
    /**
37
     * @var string
38
     */
39
    private $baseUrl;
40
41
    /**
42
     * @var Accounts
43
     */
44
    public $accounts;
45
46
    /**
47
     * @var Counterparties
48
     */
49
    public $counterparties;
50
51
    /**
52
     * @var Transfers
53
     */
54
    public $transfers;
55
56
    /**
57
     * @var Payments
58
     */
59
    public $payments;
60
61
    /**
62
     * @var PaymentDrafts
63
     */
64
    public $paymentDrafts;
65
66
    /**
67
     * @var Transactions
68
     */
69
    public $transactions;
70
71
    /**
72
     * @var Rates
73
     */
74
    public $rates;
75
76
    /**
77
     * @var Exchanges
78
     */
79
    public $exchanges;
80
81
    /**
82
     * @var Webhooks
83
     */
84
    public $webhooks;
85
86
    /**
87
     * @var WebhooksV2
88
     */
89
    public $webhooksV2;
90
91
    /**
92
     * Client constructor.
93
     * @param AccessToken $accessToken
94
     * @param string $mode
95
     * @param array $clientOptions
96
     */
97
    public function __construct(AccessToken $accessToken, $mode = 'production', array $clientOptions = [])
98
    {
99
        $this->accessToken = $accessToken;
100
        $this->mode = $mode;
101
        $this->baseUrl = ($mode === 'production' ? self::REVOLUT_PRODUCTION_ENDPOINT : self::REVOLUT_SANDBOX_ENDPOINT);
102
        $this->clientOptions = $clientOptions;
103
104
        $this->initiateHttpClient();
105
106
        $this->accounts = new Accounts($this);
107
        $this->counterparties = new Counterparties($this);
108
        $this->payments = new Payments($this);
109
        $this->paymentDrafts = new PaymentDrafts($this);
110
        $this->transfers = new Transfers($this);
111
        $this->transactions = new Transactions($this);
112
        $this->rates = new Rates($this);
113
        $this->exchanges = new Exchanges($this);
114
        $this->webhooks = new Webhooks($this);
115
        $this->webhooksV2 = new WebhooksV2($this);
116
    }
117
118
    /**
119
     * @param GuzzleClient $client
120
     */
121
    public function setClient(GuzzleClient $client)
122
    {
123
        $this->httpClient = $client;
124
    }
125
126
    /**
127
     * Initiates the HttpClient with required headers
128
     */
129
    private function initiateHttpClient()
130
    {
131
        $options = [
132
            'headers' => [
133
                'Authorization' => 'Bearer '.$this->accessToken->getToken(),
134
            ],
135
        ];
136
137
        $this->httpClient = new GuzzleClient(array_replace_recursive($this->clientOptions, $options));
138
    }
139
140
    private function getBaseUrl()
141
    {
142
        return $this->baseUrl;
143
    }
144
145
    /**
146
     * @param Response $response
147
     * @return mixed
148
     */
149
    private function handleResponse(Response $response)
150
    {
151
        $stream = $response->getBody();
152
        $data = json_decode($stream);
153
154
        return $data;
155
    }
156
157
    /**
158
     * @param string $endpoint
159
     * @param array $json
160
     * @return mixed
161
     * @throws \GuzzleHttp\Exception\GuzzleException
162
     */
163
    public function post($endpoint, $json)
164
    {
165
        $response = $this->httpClient->request('POST', $this->getBaseUrl().$endpoint, ['json' => $json]);
166
167
        return $this->handleResponse($response);
168
    }
169
170
    /**
171
     * @param string $endpoint
172
     * @param array $json
173
     * @return mixed
174
     * @throws \GuzzleHttp\Exception\GuzzleException
175
     */
176
    public function patch($endpoint, $json)
177
    {
178
        $response = $this->httpClient->request('PATCH', $this->getBaseUrl().$endpoint, ['json' => $json]);
179
180
        return $this->handleResponse($response);
181
    }
182
183
    /**
184
     * @param string $endpoint
185
     * @return mixed
186
     * @throws \GuzzleHttp\Exception\GuzzleException
187
     */
188
    public function get($endpoint)
189
    {
190
        $response = $this->httpClient->request('GET', $this->getBaseUrl().$endpoint);
191
192
        return $this->handleResponse($response);
193
    }
194
195
    /**
196
     * @param string $endpoint
197
     * @return mixed
198
     * @throws \GuzzleHttp\Exception\GuzzleException
199
     */
200
    public function delete($endpoint)
201
    {
202
        $response = $this->httpClient->request('DELETE', $this->getBaseUrl().$endpoint);
203
204
        return $this->handleResponse($response);
205
    }
206
}
207