Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.67% |
33 / 36 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
ApiFlowBasePost | |
91.67% |
33 / 36 |
|
75.00% |
3 / 4 |
10.06 | |
0.00% |
0 / 1 |
execute | |
90.91% |
30 / 33 |
|
0.00% |
0 / 1 |
7.04 | |||
mustBePosted | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isWriteMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
needsToken | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Flow\Api; |
4 | |
5 | use Flow\Model\Anchor; |
6 | use Flow\Model\UUID; |
7 | use Message; |
8 | |
9 | abstract class ApiFlowBasePost extends ApiFlowBase { |
10 | public function execute() { |
11 | $loader = $this->getLoader(); |
12 | $blocks = $loader->getBlocks(); |
13 | /** @var \Flow\Model\Workflow $workflow */ |
14 | $workflow = $loader->getWorkflow(); |
15 | $action = $this->getAction(); |
16 | |
17 | $result = $this->getResult(); |
18 | $params = $this->getBlockParams(); |
19 | $blocksToCommit = $loader->handleSubmit( |
20 | $this->getContext(), |
21 | $action, |
22 | $params |
23 | ); |
24 | |
25 | // See if any of the blocks generated an error (in which case the |
26 | // request will terminate with an the error message) |
27 | $this->processError( $blocks ); |
28 | |
29 | // If nothing is ready to be committed, we'll consider that an error (at least some |
30 | // block should've been able to process the POST request) |
31 | if ( !count( $blocksToCommit ) ) { |
32 | $this->dieWithError( 'flow-error-no-commit', 'no-commit' ); |
33 | } |
34 | |
35 | $commitMetadata = $loader->commit( $blocksToCommit ); |
36 | $savedBlocks = []; |
37 | $result->setIndexedTagName( $savedBlocks, 'block' ); |
38 | |
39 | foreach ( $blocksToCommit as $block ) { |
40 | $savedBlocks[] = $block->getName(); |
41 | } |
42 | |
43 | $output = [ $action => [ |
44 | 'status' => 'ok', |
45 | 'workflow' => $workflow->isNew() ? '' : $workflow->getId()->getAlphadecimal(), |
46 | 'committed' => $commitMetadata, |
47 | ] ]; |
48 | |
49 | // required until php5.4 which has the JsonSerializable interface |
50 | array_walk_recursive( $output, static function ( &$value ) { |
51 | if ( $value instanceof Anchor ) { |
52 | $value = $value->toArray(); |
53 | } elseif ( $value instanceof Message ) { |
54 | $value = $value->text(); |
55 | } elseif ( $value instanceof UUID ) { |
56 | $value = $value->getAlphadecimal(); |
57 | } |
58 | } ); |
59 | |
60 | $this->getResult()->addValue( null, $this->apiFlow->getModuleName(), $output ); |
61 | } |
62 | |
63 | /** |
64 | * @inheritDoc |
65 | */ |
66 | public function mustBePosted() { |
67 | return true; |
68 | } |
69 | |
70 | /** |
71 | * @inheritDoc |
72 | */ |
73 | public function isWriteMode() { |
74 | return true; |
75 | } |
76 | |
77 | /** |
78 | * @inheritDoc |
79 | */ |
80 | public function needsToken() { |
81 | return 'csrf'; |
82 | } |
83 | } |