Client::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace tbclla\Revolut;
4
5
use Illuminate\Support\Str;
6
use tbclla\Revolut\Auth\AccessToken;
7
use tbclla\Revolut\Auth\TokenManager;
8
use tbclla\Revolut\Exceptions\RevolutException;
9
use tbclla\Revolut\Interfaces\MakesHttpRequests;
10
11
/**
12
 * @method \tbclla\Revolut\Resources\Account account()
13
 * @method \tbclla\Revolut\Resources\Exchange exchange()
14
 * @method \tbclla\Revolut\Resources\Payment payment()
15
 * @method \tbclla\Revolut\Resources\PaymentDraft paymentDraft()
16
 * @method \tbclla\Revolut\Resources\Rate rate()
17
 * @method \tbclla\Revolut\Resources\Transaction transaction()
18
 * @method \tbclla\Revolut\Resources\Transfer transfer()
19
 * @method \tbclla\Revolut\Resources\Webhook webhook()
20
 */
21
class Client
22
{
23
    /**
24
     * The production URL
25
     * 
26
     * @var string
27
     */
28
    const PRODUCTION_URL = 'https://b2b.revolut.com';
29
30
    /**
31
     * The sandbox URL
32
     * 
33
     * @var string
34
     */
35
    const SANDBOX_URL = 'https://sandbox-b2b.revolut.com';
36
37
    /**
38
     * The API URI
39
     * 
40
     * @var string
41
     */
42
    const API_ENDPOINT = '/api';
43
44
    /**
45
     * The API version
46
     * 
47
     * @var string
48
     */
49
    const API_VERSION = '1.0';
50
51
    /**
52
     * The token manager
53
     *
54
     * @var \tbclla\Revolut\Auth\TokenManager
55
     */
56
    private $tokenManager;
57
58
    /**
59
     * the HTTP client
60
     * 
61
     * @var \tbclla\Revolut\Interfaces\MakesHttpRequests
62
     */
63
    private $httpClient;
64
65
    /**
66
     * The access token
67
     * 
68
     * @var \tbclla\Revolut\Auth\AccessToken
69
     */
70
    private $accessToken;
71
72
    /**
73
     * Create the client instance
74
     * 
75
     * @param \tbclla\Revolut\Auth\TokenManager $tokenManager
76
     * @param \tbclla\Revolut\Interfaces\MakesHttpRequests $httpClient
77
     * @return void
78
     */
79
    public function __construct(TokenManager $tokenManager, MakesHttpRequests $httpClient)
80
    {
81
        $this->tokenManager = $tokenManager;
82
        $this->httpClient = $httpClient;
83
    }
84
85
    /**
86
     * @param string $name
87
     * @param mixed $arguments
88
     * @return \tbclla\Revolut\Resources\Resource
89
     * @throws \tbclla\Revolut\Exceptions\RevolutException
90
     */
91
    public function __call($name, $arguments)
92
    {
93
        $resource = __NAMESPACE__ . '\\Resources\\' . ucfirst($name);
94
        if (!class_exists($resource)) {
95
            throw new RevolutException($resource . ' is not a valid API resource');
96
        }
97
        return new $resource($this);
98
    }
99
100
    /**
101
     * Set the access token
102
     *
103
     * @param \tbclla\Revolut\Auth\AccessToken $accessToken
104
     * @return void
105
     */
106
    public function setAccessToken(AccessToken $accessToken): void
107
    {
108
        $this->accessToken = $accessToken;
109
    }
110
111
    /**
112
     * Create and set a fresh access token
113
     * 
114
     * @return void
115
     */
116
    public function refreshAccessToken(): void
117
    {
118
        $this->setAccessToken($this->tokenManager->refreshAccessToken());
119
    }
120
121
    /**
122
     * Get the base URI for all API requests
123
     * 
124
     * @param string $endpoint
125
     * @return string
126
     */
127
    public static function buildUri(string $endpoint = '')
128
    {
129
        $url = config('revolut.sandbox', true) ? self::SANDBOX_URL : self::PRODUCTION_URL;
130
131
        return $url . self::apiUri() . $endpoint;
132
    }
133
    
134
    /**
135
     * Get the URI for API requests
136
     * 
137
     * @return string
138
     */
139
    public static function apiUri()
140
    {
141
        return self::API_ENDPOINT . '/' . self::API_VERSION;
142
    }
143
144
    /**
145
     * Perform a POST request against a specified endpoint
146
     *
147
     * @param string $endpoint
148
     * @param array $options
149
     * @return array The response body
150
     * @throws \tbclla\Revolut\Exceptions\ApiException if the client responded with a 4xx-5xx response
151
     * @throws \tbclla\Revolut\Exceptions\AppUnauthorizedException if the app needs to be re-authorized
152
     */
153
    public function post(string $endpoint, array $options = [])
154
    {
155
        return $this->httpClient->post($this->buildUri($endpoint), $this->buildOptions($options));
156
    }
157
158
    /**
159
     * Perform a GET request against a specified endpoint
160
     *
161
     * @param string $endpoint
162
     * @return array The response body
163
     * @throws \tbclla\Revolut\Exceptions\ApiException if the client responded with a 4xx-5xx response
164
     * @throws \tbclla\Revolut\Exceptions\AppUnauthorizedException if the app needs to be re-authorized
165
     */
166
    public function get(string $endpoint, array $options = [])
167
    {
168
        return $this->httpClient->get($this->buildUri($endpoint), $this->buildOptions($options));
169
    }
170
171
    /**
172
     * Perform a DELETE request against a specified endpoint
173
     *
174
     * @param string $endpoint
175
     * @return void
176
     * @throws \tbclla\Revolut\Exceptions\ApiException if the client responded with a 4xx-5xx response
177
     * @throws \tbclla\Revolut\Exceptions\AppUnauthorizedException if the app needs to be re-authorized
178
     */
179
    public function delete(string $endpoint)
180
    {
181
        $this->httpClient->delete($this->buildUri($endpoint), $this->buildOptions());
182
    }
183
184
    /**
185
     * Build the request options
186
     * 
187
     * @param array $options
188
     * @return array
189
     */
190
    private function buildOptions(array $options = [])
191
    {
192
        if (!$this->accessToken) {
193
            $this->setAccessToken($this->tokenManager->getAccessToken());
194
        } else if ($this->accessToken->hasExpired()) {
195
            $this->refreshAccessToken();
196
        }
197
198
        return array_merge($options, ['headers' => ['Authorization' => 'Bearer ' . $this->accessToken->value]]);
199
    }
200
201
    /**
202
     * Generate a v4 UUID to use as a request ID
203
     * 
204
     * @return string
205
     */
206
    public static function generateRequestId()
207
    {
208
        return (string) Str::Uuid();
209
    }
210
}
211