SubPageCount::getTitle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 2
1
<?php 
2
3
namespace SubPageList\Counter;
4
5
use ParamProcessor\ProcessingResult;
6
use Parser;
7
use ParserHooks\HookHandler;
8
use SubPageList\TitleFactory;
9
10
/**
11
 * Handler for the subpagecount parser hook.
12
 *
13
 * @since 1.2
14
 *
15
 * @licence GNU GPL v2+
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class SubPageCount implements HookHandler {
19
20
	private $counter;
21
	private $titleFactory;
22
23 25
	public function __construct( SubPageCounter $counter, TitleFactory $titleFactory ) {
24 25
		$this->counter = $counter;
25 25
		$this->titleFactory = $titleFactory;
26 25
	}
27
28
	/**
29
	 * @see HookHandler::handle
30
	 *
31
	 * @since 1.2
32
	 *
33
	 * @param Parser $parser
34
	 * @param ProcessingResult $result
35
	 *
36
	 * @return string
37
	 */
38 5
	public function handle( Parser $parser, ProcessingResult $result ) {
39 5
		if ( $result->hasFatal() ) {
40
			// TODO:
41
			return 'Invalid input. Cannot calculate sub page count.';
42
		}
43
44 5
		$count = $this->getSubPageCount( $parser, $result );
45
46 5
		return $parser->getTargetLanguage()->formatNum( $count );
47
	}
48
49 5
	private function getSubPageCount( Parser $parser, ProcessingResult $result ) {
50 5
		$parameters = $result->getParameters();
51 5
		$title = $this->getTitle( $parser, $parameters['page']->getValue() );
52
53 5
		if ( $title === null ) {
54
			return 0;
55
		}
56
57 5
		return $this->counter->countSubPages( $title );
58
	}
59
60 5
	private function getTitle( Parser $parser, $pageName ) {
61 5
		if ( $pageName === '' ) {
62 1
			return $parser->getTitle();
63
		}
64
		else {
65 4
			return $this->titleFactory->newFromText( $pageName );
66
		}
67
	}
68
69
}
70