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