FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cli/src/Support/ControlPlaneResponse.php at main · durable-workflow/cli · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
durable-workflow
/
cli
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cli
/
src
/
Support
/
ControlPlaneResponse.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
354 lines (301 loc) · 10.7 KB
Breadcrumbs
cli
/
src
/
Support
/
ControlPlaneResponse.php
Copy path
File metadata and controls
354 lines (301 loc) · 10.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
declare
(strict_types=
1
);
namespace
DurableWorkflow
\
Cli
\
Support
;
final
class
ControlPlaneResponse
{
/**
* @param array<string, mixed> $body
* @return array<string, mixed>
*/
public
static
function
normalize
(
string
$
method
,
string
$
path
,
array
$
body
,
int
$
statusCode
):
array
{
$
contract
=
$
body
[
'
control_plane
'
] ??
null
;
if
(!
is_array
(
$
contract
)) {
throw
new
\
RuntimeException
(
sprintf
(
'
Server error: control-plane response for [%s] is missing the shared control-plane contract.
'
,
$
path
,
));
}
$
definition
=
self
::
definition
(
$
path
,
$
contract
);
self
::
assertNoLegacyFields
(
$
path
,
$
body
,
self
::
legacyFields
(
$
path
,
$
definition
));
self
::
assertContract
(
$
path
,
$
contract
,
$
definition
,
$
body
,
$
statusCode
);
$
body
[
'
control_plane_schema
'
] ??=
$
contract
[
'
schema
'
] ??
null
;
$
body
[
'
control_plane_schema_version
'
] ??=
$
contract
[
'
version
'
] ??
null
;
$
body
[
'
control_plane_operation
'
] ??=
$
contract
[
'
operation
'
] ??
null
;
foreach
(
$
contract
as
$
field
=>
$
value
) {
if
(
in_array
(
$
field
, [
'
schema
'
,
'
version
'
,
'
contract
'
],
true
)) {
continue
;
}
if
(!
array_key_exists
(
$
field
,
$
body
)) {
$
body
[
$
field
] =
$
value
;
}
}
$
nameField
=
self
::
stringValue
(
$
contract
[
'
operation_name_field
'
] ??
null
);
if
(
$
nameField
!==
null
&& !
array_key_exists
(
$
nameField
,
$
body
)
&&
array_key_exists
(
'
operation_name
'
,
$
contract
)
) {
$
body
[
$
nameField
] =
$
contract
[
'
operation_name
'
];
}
$
body
=
self
::
normalizeWorkflowUpdateIdentifier
(
$
path
,
$
contract
,
$
body
,
$
statusCode
);
return
$
body
;
}
/**
* @param array<string, mixed> $contract
* @param array<string, mixed> $body
* @return array<string, mixed>
*/
private
static
function
normalizeWorkflowUpdateIdentifier
(
string
$
path
,
array
$
contract
,
array
$
body
,
int
$
statusCode
,
):
array
{
if
(
$
statusCode
>=
400
||
self
::
stringValue
(
$
contract
[
'
operation
'
] ??
null
) !==
'
update
'
) {
return
$
body
;
}
$
updateId
=
self
::
stringValue
(
$
body
[
'
update_id
'
] ??
null
)
??
self
::
stringValue
(
$
contract
[
'
update_id
'
] ??
null
);
if
(
$
updateId
===
null
) {
throw
new
\
RuntimeException
(
sprintf
(
'
Server error: successful workflow update response for [%s] is missing required field [update_id].
'
,
$
path
,
));
}
$
body
[
'
update_id
'
] =
$
updateId
;
return
$
body
;
}
/**
* @param array<string, mixed> $body
*/
private
static
function
assertNoLegacyFields
(
string
$
path
,
array
$
body
,
array
$
legacyFields
):
void
{
foreach
(
$
legacyFields
as
$
legacy
=>
$
canonical
) {
if
(!
array_key_exists
(
$
legacy
,
$
body
)) {
continue
;
}
throw
new
\
RuntimeException
(
sprintf
(
'
Server error: non-canonical control-plane field [%s] received for [%s]; expected [%s].
'
,
$
legacy
,
$
path
,
$
canonical
,
));
}
}
/**
* @param array<string, mixed> $contract
* @param array<string, mixed> $body
*/
private
static
function
assertContract
(
string
$
path
,
array
$
contract
,
array
$
definition
,
array
$
body
,
int
$
statusCode
,
):
void
{
self
::
requireNonEmptyString
(
$
path
,
$
contract
[
'
schema
'
] ??
null
,
'
invalid control-plane contract schema
'
,
);
self
::
requirePositiveInt
(
$
path
,
$
contract
[
'
version
'
] ??
null
,
'
invalid control-plane contract version
'
,
);
self
::
requireNonEmptyString
(
$
path
,
$
contract
[
'
operation
'
] ??
null
,
'
control-plane contract is missing required field [operation]
'
,
);
self
::
assertRequiredFields
(
$
path
,
$
contract
,
self
::
stringList
(
$
path
,
$
definition
[
'
required_fields
'
] ??
null
,
'
required_fields
'
),
);
self
::
assertBodyConsistency
(
$
path
,
$
contract
,
$
body
);
if
(
$
statusCode
>=
400
) {
return
;
}
foreach
(
self
::
stringList
(
$
path
,
$
definition
[
'
success_fields
'
] ??
null
,
'
success_fields
'
)
as
$
field
) {
if
(!
array_key_exists
(
$
field
,
$
contract
)) {
throw
new
\
RuntimeException
(
sprintf
(
'
Server error: control-plane contract for [%s] is missing required field [%s].
'
,
$
path
,
$
field
,
));
}
}
}
/**
* @param array<string, mixed> $contract
*/
private
static
function
assertRequiredFields
(
string
$
path
,
array
$
contract
,
array
$
fields
):
void
{
foreach
(
$
fields
as
$
field
) {
$
value
=
$
contract
[
$
field
] ??
null
;
if
(!
is_string
(
$
value
) ||
trim
(
$
value
) ===
''
) {
throw
new
\
RuntimeException
(
sprintf
(
'
Server error: control-plane contract for [%s] is missing required field [%s].
'
,
$
path
,
$
field
,
));
}
}
}
/**
* @param array<string, mixed> $contract
* @param array<string, mixed> $body
*/
private
static
function
assertBodyConsistency
(
string
$
path
,
array
$
contract
,
array
$
body
):
void
{
self
::
assertMatchingField
(
$
path
,
'
workflow_id
'
,
$
contract
,
$
body
);
self
::
assertMatchingField
(
$
path
,
'
run_id
'
,
$
contract
,
$
body
);
self
::
assertMatchingField
(
$
path
,
'
update_id
'
,
$
contract
,
$
body
);
$
nameField
=
self
::
stringValue
(
$
contract
[
'
operation_name_field
'
] ??
null
);
if
(
$
nameField
===
null
) {
return
;
}
self
::
assertMatchingField
(
$
path
,
$
nameField
, [
$
nameField
=>
$
contract
[
'
operation_name
'
] ??
null
,
],
$
body
);
}
/**
* @param array<string, mixed> $contract
* @param array<string, mixed> $body
*/
private
static
function
assertMatchingField
(
string
$
path
,
string
$
field
,
array
$
contract
,
array
$
body
):
void
{
$
contractValue
=
self
::
stringValue
(
$
contract
[
$
field
] ??
null
);
$
bodyValue
=
self
::
stringValue
(
$
body
[
$
field
] ??
null
);
if
(
$
contractValue
===
null
||
$
bodyValue
===
null
||
$
contractValue
===
$
bodyValue
) {
return
;
}
throw
new
\
RuntimeException
(
sprintf
(
'
Server error: control-plane contract for [%s] reported [%s=%s], but the response body reported [%s=%s].
'
,
$
path
,
$
field
,
$
contractValue
,
$
field
,
$
bodyValue
,
));
}
/**
* @param array<string, mixed> $contract
* @return array<string, mixed>
*/
private
static
function
definition
(
string
$
path
,
array
$
contract
):
array
{
$
definition
=
$
contract
[
'
contract
'
] ??
null
;
if
(!
is_array
(
$
definition
)) {
throw
new
\
RuntimeException
(
sprintf
(
'
Server error: control-plane response for [%s] is missing the shared control-plane contract definition.
'
,
$
path
,
));
}
self
::
requireNonEmptyString
(
$
path
,
$
definition
[
'
schema
'
] ??
null
,
'
invalid nested control-plane contract schema
'
,
);
self
::
requirePositiveInt
(
$
path
,
$
definition
[
'
version
'
] ??
null
,
'
invalid nested control-plane contract version
'
,
);
self
::
requireNonEmptyString
(
$
path
,
$
definition
[
'
legacy_field_policy
'
] ??
null
,
'
invalid nested control-plane legacy field policy
'
,
);
return
$
definition
;
}
/**
* @param array<string, mixed> $definition
* @return array<string, string>
*/
private
static
function
legacyFields
(
string
$
path
,
array
$
definition
):
array
{
$
legacyFields
=
$
definition
[
'
legacy_fields
'
] ??
null
;
if
(!
is_array
(
$
legacyFields
)) {
throw
new
\
RuntimeException
(
sprintf
(
'
Server error: control-plane contract definition for [%s] is missing required field [legacy_fields].
'
,
$
path
,
));
}
foreach
(
$
legacyFields
as
$
legacy
=>
$
canonical
) {
if
(!
is_string
(
$
legacy
) ||
trim
(
$
legacy
) ===
''
|| !
is_string
(
$
canonical
) ||
trim
(
$
canonical
) ===
''
) {
throw
new
\
RuntimeException
(
sprintf
(
'
Server error: control-plane contract definition for [%s] contains an invalid legacy field mapping.
'
,
$
path
,
));
}
}
/** @var array<string, string> $legacyFields */
return
$
legacyFields
;
}
/**
* @return list<string>
*/
private
static
function
stringList
(
string
$
path
,
mixed
$
value
,
string
$
field
):
array
{
if
(!
is_array
(
$
value
)) {
throw
new
\
RuntimeException
(
sprintf
(
'
Server error: control-plane contract definition for [%s] is missing required field [%s].
'
,
$
path
,
$
field
,
));
}
$
values
= [];
foreach
(
$
value
as
$
entry
) {
if
(!
is_string
(
$
entry
) ||
trim
(
$
entry
) ===
''
) {
throw
new
\
RuntimeException
(
sprintf
(
'
Server error: control-plane contract definition for [%s] contains an invalid [%s] entry.
'
,
$
path
,
$
field
,
));
}
$
values
[] =
trim
(
$
entry
);
}
return
$
values
;
}
private
static
function
stringValue
(
mixed
$
value
): ?
string
{
return
is_string
(
$
value
) &&
trim
(
$
value
) !==
''
?
trim
(
$
value
)
:
null
;
}
private
static
function
requireNonEmptyString
(
string
$
path
,
mixed
$
value
,
string
$
message
,
):
string
{
$
resolved
=
self
::
stringValue
(
$
value
);
if
(
$
resolved
!==
null
) {
return
$
resolved
;
}
throw
new
\
RuntimeException
(
sprintf
(
'
Server error: %s for [%s].
'
,
$
message
,
$
path
,
));
}
private
static
function
requirePositiveInt
(
string
$
path
,
mixed
$
value
,
string
$
message
,
):
int
{
if
(
is_int
(
$
value
) &&
$
value
>
0
) {
return
$
value
;
}
if
(
is_string
(
$
value
) &&
ctype_digit
(
$
value
) && (
int
)
$
value
>
0
) {
return
(
int
)
$
value
;
}
throw
new
\
RuntimeException
(
sprintf
(
'
Server error: %s for [%s].
'
,
$
message
,
$
path
,
));
}
}
Back
|
FazBrowse Home
|
New Git URL