HookRunner::getRawArgsList()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 3
1
<?php
2
3
namespace ParserHooks;
4
5
use ParamProcessor\Processor;
6
use Parser;
7
use ParserHooks\Internal\Runner;
8
use PPFrame;
9
10
/**
11
 * Class that handles a parser hook hook call coming from MediaWiki
12
 * by processing the parameters as declared in the hook definition and
13
 * then passes the result to the hook handler.
14
 *
15
 * @since 1.1
16
 *
17
 * @licence GNU GPL v2+
18
 * @author Jeroen De Dauw < [email protected] >
19
 */
20
class HookRunner extends Runner {
21
22
	const OPT_DO_PARSE = 'parse'; // Boolean, since 1.1
23
24
	/**
25
	 * @var Parser
26
	 */
27
	private $parser;
28
29
	/**
30
	 * @var PPFrame
31
	 */
32
	private $frame;
33
34
	/**
35
	 * @since 1.1
36
	 *
37
	 * @param HookDefinition $definition
38
	 * @param HookHandler $handler
39
	 * @param array $options
40
	 * @param Processor|null $paramProcessor
41
	 */
42 5
	public function __construct( HookDefinition $definition, HookHandler $handler, array $options = [], Processor $paramProcessor = null ) {
43 5
		parent::__construct( $definition, $handler, $options, $paramProcessor );
44 5
	}
45
46
	/**
47
	 * @since 1.1
48
	 *
49
	 * @param string $text
50
	 * @param string[] $arguments
51
	 * @param Parser $parser
52
	 * @param PPFrame $frame
53
	 *
54
	 * @return mixed
55
	 */
56 5
	public function run( $text, array $arguments, Parser &$parser, PPFrame $frame ) {
57 5
		$this->parser = $parser;
58 5
		$this->frame = $frame;
59
60 5
		$rawArgs = $this->getRawArgsList( $text, $arguments );
61 5
		$resultText = $this->getResultText( $rawArgs );
62
63 5
		return $this->getProcessedResultText( $resultText );
64
	}
65
66 5
	protected function getRawArgsList( $text, array $arguments ) {
67 5
		$defaultParameters = $this->definition->getDefaultParameters();
68 5
		$defaultParam = array_shift( $defaultParameters );
69
70
		// If there is a first default parameter, set the tag contents as its value.
71 5
		if ( !is_null( $defaultParam ) && !is_null( $text ) ) {
72 2
			$arguments[$defaultParam] = $text;
73
		}
74
75 5
		return $arguments;
76
	}
77
78 5
	protected function getResultText( array $rawArgs ) {
79 5
		return $this->handler->handle(
80 5
			$this->parser,
81 5
			$this->getProcessedArgs( $rawArgs )
82
		);
83
	}
84
85 5
	protected function getProcessedArgs( array $rawArgs ) {
86 5
		$this->paramProcessor->setParameters(
87 5
			$rawArgs,
88 5
			$this->definition->getParameters()
89
		);
90
91 5
		return $this->paramProcessor->processParameters();
92
	}
93
94 5
	protected function getProcessedResultText( $resultText ) {
95 5
		if ( $this->getOption( self::OPT_DO_PARSE ) ) {
96 4
			return $this->parser->recursiveTagParse( $resultText, $this->frame );
97
		}
98
99 1
		return $resultText;
100
	}
101
102
	/**
103
	 * @see Runner::getDefaultOptions
104
	 *
105
	 * @since 1.1
106
	 *
107
	 * @return array
108
	 */
109 5
	protected function getDefaultOptions() {
110
		return [
111 5
			self::OPT_DO_PARSE => true,
112
		];
113
	}
114
115
}
116