1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AcquiaCloudApi\Connector; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
7
|
|
|
use AcquiaCloudApi\Exception\ApiErrorException; |
8
|
|
|
use Psr\Http\Message\StreamInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Client |
12
|
|
|
* @package AcquiaCloudApi\CloudApi |
13
|
|
|
*/ |
14
|
|
|
class Client implements ClientInterface |
15
|
|
|
{ |
16
|
|
|
/** @var ConnectorInterface The API connector. */ |
17
|
|
|
protected $connector; |
18
|
|
|
|
19
|
|
|
/** @var array Query strings to be applied to the request. */ |
20
|
|
|
protected $query = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Client constructor. |
24
|
|
|
* |
25
|
|
|
* @param ConnectorInterface $connector |
26
|
|
|
*/ |
27
|
|
|
public function __construct(ConnectorInterface $connector) |
28
|
|
|
{ |
29
|
|
|
$this->connector = $connector; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Client factory method for instantiating . |
34
|
|
|
* |
35
|
|
|
* @param ConnectorInterface $connector |
36
|
|
|
* |
37
|
|
|
* @return static |
38
|
|
|
*/ |
39
|
|
|
public static function factory(ConnectorInterface $connector) |
40
|
|
|
{ |
41
|
|
|
$client = new static( |
42
|
|
|
$connector |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
return $client; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Takes parameters passed in, makes a request to the API, and processes the response. |
50
|
|
|
* |
51
|
|
|
* @param string $verb |
52
|
|
|
* @param string $path |
53
|
|
|
* @param array $options |
54
|
|
|
* |
55
|
|
|
* @return object|array |
56
|
|
|
*/ |
57
|
|
|
public function request(string $verb, string $path, array $options = []) |
58
|
|
|
{ |
59
|
|
|
$options['query'] = $this->query; |
60
|
|
|
|
61
|
|
|
if (!empty($options['query']['filter']) && is_array($options['query']['filter'])) { |
62
|
|
|
// Default to an AND filter. |
63
|
|
|
$options['query']['filter'] = implode(',', $options['query']['filter']); |
64
|
|
|
} |
65
|
|
|
$response = $this->makeRequest($verb, $path, $options); |
66
|
|
|
|
67
|
|
|
return $this->processResponse($response); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Makes a request to the API. |
72
|
|
|
* |
73
|
|
|
* @param string $verb |
74
|
|
|
* @param string $path |
75
|
|
|
* @param array $options |
76
|
|
|
* |
77
|
|
|
* @return ResponseInterface |
78
|
|
|
*/ |
79
|
|
|
public function makeRequest(string $verb, string $path, array $options = []) |
80
|
|
|
{ |
81
|
|
|
try { |
82
|
|
|
$response = $this->connector->sendRequest($verb, $path, $options); |
83
|
|
|
} catch (BadResponseException $e) { |
84
|
|
|
$response = $e->getResponse(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $response; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Processes the returned response from the API. |
92
|
|
|
* |
93
|
|
|
* @param ResponseInterface $response |
94
|
|
|
* @return object|array |
95
|
|
|
* @throws ApiErrorException |
96
|
|
|
*/ |
97
|
|
|
public function processResponse(ResponseInterface $response) |
98
|
|
|
{ |
99
|
|
|
|
100
|
|
|
$body = $response->getBody(); |
101
|
|
|
|
102
|
|
|
$object = json_decode($body); |
103
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
104
|
|
|
return $body; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (property_exists($object, '_embedded') && property_exists($object->_embedded, 'items')) { |
108
|
|
|
return $object->_embedded->items; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (property_exists($object, 'error') && property_exists($object, 'message')) { |
112
|
|
|
throw new ApiErrorException($object); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $object; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get query from Client. |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function getQuery() |
124
|
|
|
{ |
125
|
|
|
return $this->query; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Clear query. |
130
|
|
|
*/ |
131
|
|
|
public function clearQuery() |
132
|
|
|
{ |
133
|
|
|
$this->query = []; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Add a query parameter to filter results. |
138
|
|
|
* |
139
|
|
|
* @param string $name |
140
|
|
|
* @param string|int $value |
141
|
|
|
*/ |
142
|
|
|
public function addQuery($name, $value) |
143
|
|
|
{ |
144
|
|
|
$this->query = array_merge_recursive($this->query, [$name => $value]); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|