1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Common\Api; |
4
|
|
|
|
5
|
|
|
use function GuzzleHttp\uri_template; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use GuzzleHttp\Promise\Promise; |
8
|
|
|
use OpenStack\Common\Resource\ResourceInterface; |
9
|
|
|
use OpenStack\Common\Transport\RequestSerializer; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* {@inheritDoc} |
14
|
|
|
*/ |
15
|
|
|
abstract class Operator implements OperatorInterface |
16
|
|
|
{ |
17
|
|
|
/** @var ClientInterface */ |
18
|
|
|
private $client; |
19
|
|
|
|
20
|
|
|
/** @var ApiInterface */ |
21
|
|
|
protected $api; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritDoc} |
25
|
|
|
*/ |
26
|
250 |
|
public function __construct(ClientInterface $client, ApiInterface $api) |
27
|
|
|
{ |
28
|
250 |
|
$this->client = $client; |
29
|
250 |
|
$this->api = $api; |
30
|
250 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Magic method for dictating how objects are rendered when var_dump is called. |
34
|
|
|
* For the benefit of users, extremely verbose and heavy properties (such as HTTP clients) are |
35
|
|
|
* removed to provide easier access to normal state, such as resource attributes. |
36
|
|
|
* |
37
|
|
|
* @codeCoverageIgnore |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public function __debugInfo() |
41
|
|
|
{ |
42
|
|
|
$excludedVars = ['client', 'errorBuilder', 'api']; |
43
|
|
|
|
44
|
|
|
$output = []; |
45
|
|
|
|
46
|
|
|
foreach (get_object_vars($this) as $key => $val) { |
47
|
|
|
if (!in_array($key, $excludedVars)) { |
48
|
|
|
$output[$key] = $val; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $output; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Retrieves a populated Operation according to the definition and values provided. A |
57
|
|
|
* HTTP client is also injected into the object to allow it to communicate with the remote API. |
58
|
|
|
* |
59
|
|
|
* @param array $definition The data that dictates how the operation works |
60
|
|
|
* |
61
|
|
|
* @return Operation |
62
|
|
|
*/ |
63
|
193 |
|
public function getOperation(array $definition) |
64
|
|
|
{ |
65
|
193 |
|
return new Operation($definition); |
66
|
|
|
} |
67
|
|
|
|
68
|
192 |
|
protected function sendRequest(Operation $operation, array $userValues = [], $async = false) |
69
|
|
|
{ |
70
|
192 |
|
$operation->validate($userValues); |
71
|
|
|
|
72
|
192 |
|
$options = (new RequestSerializer)->serializeOptions($operation, $userValues); |
73
|
192 |
|
$method = $async ? 'requestAsync' : 'request'; |
74
|
192 |
|
$uri = uri_template($operation->getPath(), $userValues); |
75
|
|
|
|
76
|
192 |
|
return $this->client->$method($operation->getMethod(), $uri, $options); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritDoc} |
81
|
|
|
*/ |
82
|
161 |
|
public function execute(array $definition, array $userValues = []) |
83
|
|
|
{ |
84
|
161 |
|
return $this->sendRequest($this->getOperation($definition), $userValues); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritDoc} |
89
|
|
|
*/ |
90
|
1 |
|
public function executeAsync(array $definition, array $userValues = []) |
91
|
|
|
{ |
92
|
1 |
|
return $this->sendRequest($this->getOperation($definition), $userValues, true); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritDoc} |
97
|
|
|
*/ |
98
|
90 |
|
public function model($class, $data = null) |
99
|
|
|
{ |
100
|
90 |
|
$model = new $class($this->client, $this->api); |
101
|
|
|
|
102
|
|
|
// @codeCoverageIgnoreStart |
103
|
|
|
if (!$model instanceof ResourceInterface) { |
104
|
|
|
throw new \RuntimeException(sprintf('%s does not implement %s', $class, ResourceInterface::class)); |
105
|
|
|
} |
106
|
|
|
// @codeCoverageIgnoreEnd |
107
|
|
|
|
108
|
90 |
|
if ($data instanceof ResponseInterface) { |
109
|
3 |
|
$model->populateFromResponse($data); |
110
|
90 |
|
} elseif (is_array($data)) { |
111
|
28 |
|
$model->populateFromArray($data); |
112
|
28 |
|
} |
113
|
|
|
|
114
|
90 |
|
return $model; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Will create a new instance of this class with the current HTTP client and API injected in. This |
119
|
|
|
* is useful when enumerating over a collection since multiple copies of the same resource class |
120
|
|
|
* are needed. |
121
|
|
|
* |
122
|
|
|
* @return static |
123
|
|
|
*/ |
124
|
34 |
|
public function newInstance() |
125
|
|
|
{ |
126
|
34 |
|
return new static($this->client, $this->api); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return \GuzzleHttp\Psr7\Uri |
131
|
|
|
*/ |
132
|
8 |
|
protected function getHttpBaseUrl() |
133
|
|
|
{ |
134
|
8 |
|
return $this->client->getConfig('base_uri'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Magic method which intercepts async calls, finds the sequential version, and wraps it in a |
139
|
|
|
* {@see Promise} object. In order for this to happen, the called methods need to be in the |
140
|
|
|
* following format: `createAsync`, where `create` is the sequential method being wrapped. |
141
|
|
|
* |
142
|
|
|
* @param $methodName The name of the method being invoked. |
143
|
|
|
* @param $args The arguments to be passed to the sequential method. |
144
|
|
|
* |
145
|
|
|
* @throws \RuntimeException If method does not exist |
146
|
|
|
* |
147
|
|
|
* @return Promise |
148
|
|
|
*/ |
149
|
3 |
|
public function __call($methodName, $args) |
150
|
|
|
{ |
151
|
|
|
$e = function ($name) { |
152
|
2 |
|
return new \RuntimeException(sprintf('%s::%s is not defined', get_class($this), $name)); |
153
|
3 |
|
}; |
154
|
|
|
|
155
|
3 |
|
if (substr($methodName, -5) === 'Async') { |
156
|
2 |
|
$realMethod = substr($methodName, 0, -5); |
157
|
2 |
|
if (!method_exists($this, $realMethod)) { |
158
|
1 |
|
throw $e($realMethod); |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
$promise = new Promise( |
162
|
1 |
|
function () use (&$promise, $realMethod, $args) { |
163
|
1 |
|
$value = call_user_func_array([$this, $realMethod], $args); |
164
|
1 |
|
$promise->resolve($value); |
165
|
1 |
|
} |
166
|
1 |
|
); |
167
|
|
|
|
168
|
1 |
|
return $promise; |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
throw $e($methodName); |
172
|
|
|
} |
173
|
|
|
} |