1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Common\Api; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
6
|
|
|
use GuzzleHttp\Psr7\Uri; |
7
|
|
|
use function GuzzleHttp\uri_template; |
8
|
|
|
use GuzzleHttp\ClientInterface; |
9
|
|
|
use GuzzleHttp\Promise\Promise; |
10
|
|
|
use OpenStack\Common\Resource\ResourceInterface; |
11
|
|
|
use OpenStack\Common\Transport\RequestSerializer; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
|
14
|
|
|
trait OperatorTrait |
15
|
|
|
{ |
16
|
|
|
/** @var ClientInterface */ |
17
|
|
|
protected $client; |
18
|
|
|
|
19
|
|
|
/** @var ApiInterface */ |
20
|
|
|
protected $api; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritDoc} |
24
|
|
|
*/ |
25
|
|
|
public function __construct(ClientInterface $client, ApiInterface $api) |
26
|
|
|
{ |
27
|
|
|
$this->client = $client; |
28
|
|
|
$this->api = $api; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Magic method for dictating how objects are rendered when var_dump is called. |
33
|
|
|
* For the benefit of users, extremely verbose and heavy properties (such as HTTP clients) are |
34
|
|
|
* removed to provide easier access to normal state, such as resource attributes. |
35
|
|
|
* |
36
|
|
|
* @codeCoverageIgnore |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function __debugInfo() |
40
|
|
|
{ |
41
|
|
|
$excludedVars = ['client', 'errorBuilder', 'api']; |
42
|
|
|
|
43
|
|
|
$output = []; |
44
|
|
|
|
45
|
|
|
foreach (get_object_vars($this) as $key => $val) { |
46
|
|
|
if (!in_array($key, $excludedVars)) { |
47
|
|
|
$output[$key] = $val; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $output; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Magic method which intercepts async calls, finds the sequential version, and wraps it in a |
56
|
|
|
* {@see Promise} object. In order for this to happen, the called methods need to be in the |
57
|
|
|
* following format: `createAsync`, where `create` is the sequential method being wrapped. |
58
|
|
|
* |
59
|
|
|
* @param $methodName The name of the method being invoked. |
60
|
|
|
* @param $args The arguments to be passed to the sequential method. |
61
|
|
|
* |
62
|
|
|
* @throws \RuntimeException If method does not exist |
63
|
|
|
* |
64
|
|
|
* @return Promise |
65
|
|
|
*/ |
66
|
|
|
public function __call($methodName, $args) |
67
|
|
|
{ |
68
|
|
|
$e = function ($name) { |
69
|
|
|
return new \RuntimeException(sprintf('%s::%s is not defined', get_class($this), $name)); |
70
|
|
|
}; |
71
|
|
|
|
72
|
|
|
if (substr($methodName, -5) === 'Async') { |
73
|
|
|
$realMethod = substr($methodName, 0, -5); |
74
|
|
|
if (!method_exists($this, $realMethod)) { |
75
|
|
|
throw $e($realMethod); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$promise = new Promise( |
79
|
|
|
function () use (&$promise, $realMethod, $args) { |
80
|
|
|
$value = call_user_func_array([$this, $realMethod], $args); |
81
|
|
|
$promise->resolve($value); |
82
|
|
|
} |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
return $promise; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
throw $e($methodName); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function getOperation(array $definition): Operation |
95
|
|
|
{ |
96
|
|
|
return new Operation($definition); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Operation $operation |
101
|
|
|
* @param array $userValues |
102
|
|
|
* @param bool $async |
103
|
|
|
* |
104
|
|
|
* @return mixed |
105
|
|
|
* @throws \Exception |
106
|
|
|
*/ |
107
|
|
|
protected function sendRequest(Operation $operation, array $userValues = [], bool $async = false) |
108
|
|
|
{ |
109
|
|
|
$operation->validate($userValues); |
110
|
|
|
|
111
|
|
|
$options = (new RequestSerializer)->serializeOptions($operation, $userValues); |
112
|
|
|
$method = $async ? 'requestAsync' : 'request'; |
113
|
|
|
$uri = uri_template($operation->getPath(), $userValues); |
114
|
|
|
|
115
|
|
|
return $this->client->$method($operation->getMethod(), $uri, $options); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritDoc} |
120
|
|
|
*/ |
121
|
|
|
public function execute(array $definition, array $userValues = []): ResponseInterface |
122
|
|
|
{ |
123
|
|
|
return $this->sendRequest($this->getOperation($definition), $userValues); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritDoc} |
128
|
|
|
*/ |
129
|
|
|
public function executeAsync(array $definition, array $userValues = []): PromiseInterface |
130
|
|
|
{ |
131
|
|
|
return $this->sendRequest($this->getOperation($definition), $userValues, true); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritDoc} |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function model(string $class, $data = null): ResourceInterface |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$model = new $class($this->client, $this->api); |
140
|
|
|
// @codeCoverageIgnoreStart |
141
|
|
|
if (!$model instanceof ResourceInterface) { |
142
|
|
|
throw new \RuntimeException(sprintf('%s does not implement %s', $class, ResourceInterface::class)); |
143
|
|
|
} |
144
|
|
|
// @codeCoverageIgnoreEnd |
145
|
|
|
if ($data instanceof ResponseInterface) { |
146
|
|
|
$model->populateFromResponse($data); |
147
|
|
|
} elseif (is_array($data)) { |
148
|
|
|
$model->populateFromArray($data); |
149
|
|
|
} |
150
|
|
|
return $model; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.