|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace tbclla\Revolut; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
|
|
|
|
|
6
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
|
7
|
|
|
use tbclla\Revolut\Exceptions\ApiException; |
|
8
|
|
|
use tbclla\Revolut\Interfaces\MakesHttpRequests; |
|
9
|
|
|
|
|
10
|
|
|
class GuzzleHttpClient implements MakesHttpRequests |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var \GuzzleHttp\Client |
|
14
|
|
|
*/ |
|
15
|
|
|
private $client; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @return void |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->client = new Client(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function post(string $url, array $options = []) |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->send('POST', $url, $options); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function get(string $url, array $options = []) |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->send('GET', $url, $options); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function delete(string $url, array $options = []) : void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->send('DELETE', $url, $options); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Perform a request |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $method The request method |
|
44
|
|
|
* @param string $url The request url |
|
45
|
|
|
* @param array $options The request options |
|
46
|
|
|
* @return array|null The response body |
|
47
|
|
|
* @throws \tbclla\Revolut\Exceptions\ApiException if the client responded with a 4xx-5xx response |
|
48
|
|
|
*/ |
|
49
|
|
|
private function send(string $method, string $url, array $options) |
|
50
|
|
|
{ |
|
51
|
|
|
try { |
|
52
|
|
|
$response = $this->client->request($method, $url, $options); |
|
53
|
|
|
} catch (BadResponseException $e) { |
|
54
|
|
|
throw new ApiException($e->getMessage(), $e->getCode(), $e); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return json_decode($response->getBody(), true); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: