-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProviderResult.php
216 lines (179 loc) · 6.12 KB
/
ProviderResult.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
<?php
declare(strict_types=1);
namespace Upmind\ProvisionBase\Result;
use Illuminate\Support\Arr;
use Throwable;
use Upmind\ProvisionBase\ProviderJob;
use Upmind\ProvisionBase\Result\Contract\ResultInterface;
/**
* Class which encapsulates the Result of a ProviderJob
*/
class ProviderResult extends Result implements ResultInterface
{
/**
* Create a Result instance for a ProviderJob using the output of a Provider function
*
* @param mixed $output Provider function output
*
* @return self
*/
public static function createFromProviderOutput($output, bool $outputDebug = true): self
{
$result = self::createFromArray(Arr::wrap($output));
if ($outputDebug) {
return $result->withProviderOutputDebug($output);
}
return $result;
}
/**
* Create a Result instance for a ProviderJob using an encountered exception
*
* @param Throwable $exception
*
* @return self
*/
public static function createFromProviderException(Throwable $exception, ?string $errorId = null): self
{
return self::createErrorResult('Critical provider error encountered', [], $errorId)
->withProviderExceptionDebug($exception);
}
/**
* Adds the Provider exception to debug data
*
* @param Throwable $e
*
* @return self
*/
public function withProviderExceptionDebug(Throwable $e): self
{
$this->setException($e);
// Record the first exception in the chain
while ($previous = $e->getPrevious()) {
$e = $previous;
}
$this->debug = Arr::wrap($this->debug);
Arr::set($this->debug, 'provider_exception', self::formatException($e));
return $this->withDebugMessage("Encountered exception: " . get_class($e));
}
/**
* Adds a Provider destructor exception to debug data
*
* @param Throwable $e
*
* @return self
*/
public function withProviderDestructorExceptionDebug(Throwable $e): self
{
// Record the first exception in the chain
while ($previous = $e->getPrevious()) {
$e = $previous;
}
$this->debug = Arr::wrap($this->debug);
Arr::set($this->debug, 'provider_destructor_exception', self::formatException($e));
return $this;
}
/**
* Adds the ProviderJob to debug data
*
* @param ProviderJob $job
*
* @return self
*/
public function withProviderJobDebug(ProviderJob $job): self
{
$this->debug = Arr::wrap($this->debug);
Arr::set($this->debug, 'provider_job', self::formatProviderJob($job));
return $this;
}
/**
* Adds the raw Provider function output to debug data
*
* @param mixed $providerOutput
*
* @return self
*/
public function withProviderOutputDebug($providerOutput): self
{
$providerOutput = json_encode($providerOutput, JSON_INVALID_UTF8_SUBSTITUTE) ?? 'null';
$this->debug = Arr::wrap($this->debug);
Arr::set($this->debug, 'provider_output', $providerOutput);
return $this;
}
public function withExecutionTimeDebug(float $executionTime): self
{
$this->debug = Arr::wrap($this->debug);
Arr::set($this->debug, 'execution_time', round($executionTime, 3));
return $this;
}
protected function standardizeStatus(): void
{
$this->status = strtolower($this->status);
if (!in_array($this->status, [ResultInterface::STATUS_OK, ResultInterface::STATUS_ERROR])) {
$this->status = ResultInterface::STATUS_ERROR;
if (empty($this->message)) {
$this->message = 'Unexpected provider function output';
}
}
parent::standardizeStatus();
}
protected function standardizeMessage(): void
{
if (empty($this->message)) {
switch ($this->status) {
case ResultInterface::STATUS_OK:
$this->message = 'Success';
break;
default:
$this->message = 'Unknown provider function error';
}
}
parent::standardizeMessage();
}
protected function standardizeDebug(): void
{
$this->debug = Arr::wrap($this->debug);
if (!empty($this->debug)) {
$providerData = Arr::get($this->debug, 'provider_data');
if (!is_array($providerData) || empty($providerData)) {
Arr::forget($this->debug, 'provider_data');
}
$providerJob = Arr::get($this->debug, 'provider_job');
if ($providerJob instanceof ProviderJob) {
$providerJob = self::formatProviderJob($providerJob);
Arr::set($this->debug, 'provider_job', $providerJob);
}
if (!is_array($providerJob) || empty($providerJob)) {
Arr::forget($this->debug, 'provider_job');
}
$providerException = Arr::get($this->debug, 'provider_exception');
if ($providerException instanceof Throwable) {
$this->setException($providerException);
$providerException = self::formatException($providerException);
Arr::set($this->debug, 'provider_exception', $providerException);
}
if (!is_array($providerException) || empty($providerException)) {
Arr::forget($this->debug, 'provider_exception');
}
$providerOutput = Arr::get($this->debug, 'provider_output');
if (!is_string($providerOutput)) {
Arr::forget($this->debug, 'provider_output');
}
}
parent::standardizeDebug();
}
/**
* Convert a ProviderJob into a formatted associative array
*
* @param ProviderJob $job
*
* @return array
*/
public static function formatProviderJob(ProviderJob $job): array
{
return [
'provider' => get_class($job->getProvider()->getInstance()),
'function' => $job->getFunction(),
// 'params' => $job->getParams()
];
}
}