SubPageCount   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 10
c 0
b 0
f 0
ccs 17
cts 19
cp 0.8947
wmc 7
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 10 2
A getSubPageCount() 0 10 2
A getTitle() 0 8 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