1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\FluentApi\Clients; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use GuzzleHttp\ClientInterface as Guzzle; |
7
|
|
|
use Guzzle\Http\Client; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Adapter for the Guzzle HTTP client. |
11
|
|
|
* |
12
|
|
|
* @author Andrea Marco Sartori |
13
|
|
|
*/ |
14
|
|
|
class GuzzleAdapter implements AsyncClientInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The Guzzle client. |
18
|
|
|
* |
19
|
|
|
* @author Andrea Marco Sartori |
20
|
|
|
* @var GuzzleHttp\ClientInterface |
21
|
|
|
*/ |
22
|
|
|
protected $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Set the dependencies. |
26
|
|
|
* |
27
|
|
|
* @author Andrea Marco Sartori |
28
|
|
|
* @param GuzzleHttp\ClientInterface $client |
29
|
|
|
* @return void |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
public function __construct(Guzzle $client) |
32
|
|
|
{ |
33
|
|
|
$this->client = $client; |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Process the HTTP request synchronously. |
38
|
|
|
* |
39
|
|
|
* @author Andrea Marco Sartori |
40
|
|
|
* @param string $verb |
41
|
|
|
* @param string $endpoint |
42
|
|
|
* @param array $options |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function call($verb, $endpoint, array $options = []) |
46
|
|
|
{ |
47
|
|
|
return $this->client->request($verb, $endpoint, $options); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Process the HTTP request asynchronously. |
52
|
|
|
* |
53
|
|
|
* @author Andrea Marco Sartori |
54
|
|
|
* @param string $verb |
55
|
|
|
* @param string $endpoint |
56
|
|
|
* @param array $options |
57
|
|
|
* @param Closure $success |
58
|
|
|
* @param Closure|null $failure |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
public function then($verb, $endpoint, array $options = [], Closure $success, Closure $failure = null) |
62
|
|
|
{ |
63
|
|
|
return $this->client->requestAsync($verb, $endpoint, $options)->then($success, $failure); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Retrieve the query string parameters from the given options. |
68
|
|
|
* |
69
|
|
|
* @param array $options |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function getQueryByOptions(array $options) |
73
|
|
|
{ |
74
|
|
|
return isset($options['query']) ? $options['query'] : []; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.