-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProviderJob.php
306 lines (267 loc) · 8.87 KB
/
ProviderJob.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
declare(strict_types=1);
namespace Upmind\ProvisionBase;
use Error;
use Exception;
use Throwable;
use Upmind\ProvisionBase\Exception\CriticalProvisionProviderError;
use Upmind\ProvisionBase\Exception\InvalidDataSetException;
use Upmind\ProvisionBase\Exception\InvalidFunctionParameterDataException;
use Upmind\ProvisionBase\Exception\InvalidFunctionReturnDataException;
use Upmind\ProvisionBase\Exception\InvalidProviderJob;
use Upmind\ProvisionBase\Exception\ProvisionFunctionError;
use Upmind\ProvisionBase\Provider\DataSet\DataSet;
use Upmind\ProvisionBase\Provider\DataSet\ResultData;
use Upmind\ProvisionBase\Registry\Data\FunctionRegister;
use Upmind\ProvisionBase\Result\ProviderResult;
/**
* Class for handling the execution of Provider functions
*/
class ProviderJob
{
/**
* Provider register/instance wrapper.
*
* @var Provider $provider
*/
protected $provider;
/**
* Provision function name.
*
* @var string $function
*/
protected $function;
/**
* Provision function register.
*
* @var FunctionRegister|null
*/
protected $functionRegister;
/**
* Provision function parameters.
*
* @var array|DataSet $parameterData
*/
protected $parameterData;
/**
* Provision function return data.
*
* @var DataSet|null $returnData
*/
protected $returnData;
/**
* Provider job result.
*
* @var ProviderResult
*/
protected $result;
/**
* @param Provider $provider Provider register/instance wrapper
* @param string $function Provision function name
* @param array|DataSet $parameterData Provision function parameters
*
* @throws InvalidProviderJob If the requested function is not supported
*/
public function __construct(Provider $provider, string $function, $parameterData = [])
{
$this->provider = $provider;
$this->function = $function;
$this->parameterData = $parameterData;
if (!$this->provider->getRegister()->hasFunction($function)) {
throw InvalidProviderJob::forInvalidFunction($this);
}
}
/**
* Get the provider register/instance wrapper for this job.
*/
public function getProvider(): Provider
{
return $this->provider;
}
/**
* Get the function name for this job.
*/
public function getFunction(): string
{
return $this->function;
}
/**
* Get the function register for this job.
*/
public function getFunctionRegister(): FunctionRegister
{
if (!isset($this->functionRegister)) {
$this->functionRegister = $this->provider->getRegister()->getFunction($this->function);
}
return $this->functionRegister;
}
/**
* Get the function parameter data set.
*/
public function getParameterData(): DataSet
{
if (!$this->parameterData instanceof DataSet) {
$this->parameterData = $this->getParameterDataSetClass()::create($this->parameterData);
}
return $this->parameterData;
}
/**
* Validate this provider job's parameter data.
*
* @throws InvalidFunctionParameterDataException
*/
protected function validateParameterData(): void
{
try {
$this->getParameterData()->validateIfNotYetValidated();
} catch (InvalidDataSetException $e) {
throw InvalidFunctionParameterDataException::fromInvalidDataSet($e);
}
}
/**
* Get the class name of the job parameter DataSet.
*/
public function getParameterDataSetClass(): string
{
return $this->getFunctionRegister()->getParameter()->getClass();
}
/**
* Get the function return data set.
*/
public function getReturnData(): ?DataSet
{
if (!isset($this->returnData) && !isset($this->result)) {
$this->execute();
}
return $this->returnData ?? null;
}
/**
* Validate this provider job's return data.
*
* @throws InvalidFunctionReturnDataException
*/
protected function validateReturnData(): void
{
try {
$this->getReturnData()->validateIfNotYetValidated();
} catch (InvalidDataSetException $e) {
throw InvalidFunctionReturnDataException::fromInvalidDataSet($e);
}
}
/**
* Get the class name of the job return DataSet.
*/
public function getReturnDataSetClass(): string
{
return $this->getFunctionRegister()->getReturn()->getClass();
}
/**
* Get this provider job's result.
*/
public function getResult(): ProviderResult
{
if (!isset($this->result)) {
$this->execute();
}
return $this->result;
}
/**
* Execute the provision function and obtain the job result.
*/
public function execute(): ProviderResult
{
if (isset($this->result)) {
return $this->result;
}
$executeStartTime = microtime(true);
try {
//validate parameter data
$this->validateParameterData();
try {
//execute function
$this->returnData = call_user_func(
[$this->getProvider()->getInstance(), $this->getFunction()],
$this->getParameterData()
);
} catch (ProvisionFunctionError $e) {
throw $e;
} catch (Exception $e) {
throw new ProvisionFunctionError('Internal provision provider error', (int)$e->getCode(), $e);
} catch (Error $e) {
throw new CriticalProvisionProviderError('Critical provision provider error', (int)$e->getCode(), $e);
}
//ensure return data is of the correct type
if (get_class($this->returnData) !== $this->getReturnDataSetClass()) {
$this->returnData = $this->getReturnDataSetClass()::create($this->returnData);
}
//validate return data
$this->validateReturnData();
//create successful job result
return $this->result = $this->createSuccessResult($this->returnData);
} catch (Throwable $e) {
return $this->result = $this->createErrorResult($e, $this->returnData ?? null)
->withProviderJobDebug($this);
} finally {
try {
// trigger destructors to prevent destruct errors from affecting the provision result
$this->getProvider()->unsetInstance();
} catch (Throwable $e) {
$this->result->withProviderDestructorExceptionDebug($e);
}
return $this->result->withExecutionTimeDebug(microtime(true) - $executeStartTime);
}
}
/**
* Create a successful provider result with the given provision function
* return data.
*/
protected function createSuccessResult(DataSet $returnData): ProviderResult
{
if ($returnData instanceof ResultData) {
return $returnData->toProvisionResult();
}
return new ProviderResult(
ProviderResult::STATUS_OK,
null,
$returnData->toArray(),
null
);
}
/**
* Create an error provider result from the given exception.
*
* @param Throwable $e Encountered error
* @param DataSet|array|null $returnData Function return data, if any
*/
protected function createErrorResult(Throwable $e, $returnData = null): ProviderResult
{
if ($e instanceof ProvisionFunctionError) {
$result = $e->toProvisionResult();
} elseif ($e instanceof InvalidFunctionParameterDataException) {
$e->setErrorPrefix('parameters');
$result = ProviderResult::createErrorResult($e->getMessage(), ['validation_errors' => $e->errors()]);
} elseif ($e instanceof InvalidFunctionReturnDataException) {
$e->setErrorPrefix('provider_output');
$result = ProviderResult::createErrorResult($e->getMessage());
} elseif ($e instanceof CriticalProvisionProviderError) {
$result = ProviderResult::createErrorResult($e->getMessage());
} else {
throw $e; // at this point, any other type of exception is our bad!
}
if (isset($returnData) && $returnData instanceof DataSet) {
$returnData = clone $returnData;
$returnData->autoValidation(false); //disable validation to get return data values without errors
}
return $result->withProviderOutputDebug($returnData)
->withProviderExceptionDebug($e);
}
public function __debugInfo()
{
return [
'provider' => $this->provider,
'function' => $this->function,
'parameterData' => $this->parameterData,
'result' => $this->result,
];
}
}