Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.62% |
29 / 32 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ApiFlowBaseGet | |
90.62% |
29 / 32 |
|
33.33% |
1 / 3 |
10.08 | |
0.00% |
0 / 1 |
| execute | |
93.33% |
28 / 30 |
|
0.00% |
0 / 1 |
8.02 | |||
| mustBePosted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| needsToken | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Flow\Api; |
| 4 | |
| 5 | use Flow\Block\AbstractBlock; |
| 6 | use Flow\Model\Anchor; |
| 7 | use Flow\Model\UUID; |
| 8 | use MediaWiki\Message\Message; |
| 9 | |
| 10 | abstract class ApiFlowBaseGet extends ApiFlowBase { |
| 11 | public function execute() { |
| 12 | $loader = $this->getLoader(); |
| 13 | $blocks = $loader->getBlocks(); |
| 14 | $context = $this->getContext(); |
| 15 | $action = $this->getAction(); |
| 16 | $passedParams = $this->getBlockParams(); |
| 17 | |
| 18 | $output = [ $action => [ |
| 19 | 'result' => [], |
| 20 | 'status' => 'ok', |
| 21 | ] ]; |
| 22 | |
| 23 | /** @var AbstractBlock $block */ |
| 24 | foreach ( $blocks as $block ) { |
| 25 | $block->init( $context, $action ); |
| 26 | |
| 27 | if ( $block->canRender( $action ) ) { |
| 28 | $blockParams = []; |
| 29 | if ( isset( $passedParams[$block->getName()] ) ) { |
| 30 | $blockParams = $passedParams[$block->getName()]; |
| 31 | } |
| 32 | |
| 33 | $output[$action]['result'][$block->getName()] = $block->renderApi( $blockParams ); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // See if any of the blocks generated an error (in which case the |
| 38 | // request will terminate with an the error message) |
| 39 | $this->processError( $blocks ); |
| 40 | |
| 41 | // If nothing could render, we'll consider that an error (at least some |
| 42 | // block should've been able to render a GET request) |
| 43 | if ( !$output[$action]['result'] ) { |
| 44 | $this->dieWithError( 'flow-error-no-render', 'no-render' ); |
| 45 | } |
| 46 | |
| 47 | $blocks = array_keys( $output[$action]['result'] ); |
| 48 | $this->getResult()->setIndexedTagName( $blocks, 'block' ); |
| 49 | |
| 50 | // Required until php5.4 which has the JsonSerializable interface |
| 51 | array_walk_recursive( $output, static function ( &$value ) { |
| 52 | if ( $value instanceof Anchor ) { |
| 53 | $value = $value->toArray(); |
| 54 | } elseif ( $value instanceof Message ) { |
| 55 | $value = $value->text(); |
| 56 | } elseif ( $value instanceof UUID ) { |
| 57 | $value = $value->getAlphadecimal(); |
| 58 | } |
| 59 | } ); |
| 60 | |
| 61 | $this->getResult()->addValue( null, $this->apiFlow->getModuleName(), $output ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @inheritDoc |
| 66 | */ |
| 67 | public function mustBePosted() { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @inheritDoc |
| 73 | */ |
| 74 | public function needsToken() { |
| 75 | return false; |
| 76 | } |
| 77 | } |