SubPageList   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
dl 0
loc 143
ccs 48
cts 50
cp 0.96
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 7

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A handle() 0 16 3
A getTitle() 0 8 2
A renderForTitle() 0 10 2
A getPageHierarchy() 0 17 2
A shouldUseDefault() 0 5 2
A getDefault() 0 11 3
A paramsToOptions() 0 9 2
A getRenderedList() 0 6 1
1
<?php 
2
3
namespace SubPageList\Lister;
4
5
use LogicException;
6
use ParamProcessor\ProcessedParam;
7
use ParamProcessor\ProcessingResult;
8
use Parser;
9
use ParserHooks\HookHandler;
10
use SubPageList\TitleFactory;
11
use SubPageList\Lister\UI\SubPageListRenderer;
12
use Title;
13
14
/**
15
 * Handler for the subpagelist parser hook.
16
 *
17
 * @since 1.2
18
 *
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class SubPageList implements HookHandler {
23
24
	private $subPageFinder;
25
	private $pageHierarchyCreator;
26
	private $subPageListRenderer;
27
	private $titleFactory;
28
29 25
	public function __construct( SubPageFinder $finder, PageHierarchyCreator $hierarchyCreator,
30
		SubPageListRenderer $renderer, TitleFactory $titleFactory ) {
31
32 25
		$this->subPageFinder = $finder;
33 25
		$this->pageHierarchyCreator = $hierarchyCreator;
34 25
		$this->subPageListRenderer = $renderer;
35 25
		$this->titleFactory = $titleFactory;
36 25
	}
37
38
	/**
39
	 * @see HookHandler::handle
40
	 *
41
	 * @since 1.2
42
	 *
43
	 * @param Parser $parser
44
	 * @param ProcessingResult $result
45
	 *
46
	 * @return string
47
	 */
48 21
	public function handle( Parser $parser, ProcessingResult $result ) {
49 21
		if ( $result->hasFatal() ) {
50
			// This should not occur given the current parameter definitions.
51
			return 'Error: invalid input into subPageList function';
52
		}
53
54 21
		$parameters = $this->paramsToOptions( $result->getParameters() );
55
56 21
		$title = $this->getTitle( $parser, $parameters['page'] );
57
58 21
		if ( $title !== null ) {
59 20
			return $this->renderForTitle( $title, $parameters );
60
		}
61
62 1
		return 'Error: invalid title provided'; // TODO (might want to use a title param...)
63
	}
64
65 21
	private function getTitle( Parser $parser, $pageName ) {
66 21
		if ( $pageName === '' ) {
67 1
			return $parser->getTitle();
68
		}
69
		else {
70 20
			return $this->titleFactory->newFromText( $pageName );
71
		}
72
	}
73
74
	/**
75
	 * @param Title $title
76
	 * @param array $parameters
77
	 *
78
	 * @return string
79
	 */
80 20
	private function renderForTitle( Title $title, array $parameters ) {
81 20
		$topLevelPage = $this->getPageHierarchy( $title, $parameters['sort'], $parameters['limit'], $parameters['redirects'] );
82
83 20
		if ( $this->shouldUseDefault( $topLevelPage, $parameters['showpage'] ) ) {
84 3
			return $this->getDefault( $parameters['page'], $parameters['default'] );
85
		}
86
		else {
87 17
			return $this->getRenderedList( $topLevelPage, $parameters );
88
		}
89
	}
90
91
	/**
92
	 * @param Title $title
93
	 * @param int $limit
94
	 * @param string $sortOrder
95
	 * @param bool $includeRedirects
96
	 *
97
	 * @return Page
98
	 * @throws LogicException
99
	 */
100 20
	private function getPageHierarchy( Title $title, $sortOrder, $limit, $includeRedirects ) {
101 20
		$this->subPageFinder->setLimit( $limit );
102 20
		$this->subPageFinder->setSortOrder( $sortOrder );
103 20
		$this->subPageFinder->setIncludeRedirects( $includeRedirects );
104
105 20
		$subPageTitles = $this->subPageFinder->getSubPagesFor( $title );
106 20
		$subPageTitles[] = $title;
107
108 20
		$pageHierarchy = $this->pageHierarchyCreator->createHierarchy( $subPageTitles );
109
110 20
		if ( count( $pageHierarchy ) !== 1 ) {
111
			throw new LogicException( 'Expected exactly one top level page' );
112
		}
113
114 20
		$topLevelPage = reset( $pageHierarchy );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($pageHierarchy); of type SubPageList\Lister\Page|false adds false to the return on line 115 which is incompatible with the return type documented by SubPageList\Lister\SubPageList::getPageHierarchy of type SubPageList\Lister\Page. It seems like you forgot to handle an error condition.
Loading history...
115 20
		return $topLevelPage;
116
	}
117
118 20
	private function shouldUseDefault( Page $topLevelPage, $showTopLevelPage ) {
119
		// Note: this behaviour is not fully correct.
120
		// Other parameters that omit results need to be held into account as well.
121 20
		return !$showTopLevelPage && $topLevelPage->getSubPages() === [];
122
	}
123
124 17
	private function getRenderedList( Page $topLevelPage, $parameters ) {
125 17
		return $this->subPageListRenderer->render(
126 17
			$topLevelPage,
127
			$parameters
128
		);
129
	}
130
131
	/**
132
	 * @param string $titleText
133
	 * @param string $default
134
	 *
135
	 * @return string
136
	 */
137 3
	private function getDefault( $titleText, $default ) {
138 3
		if ( $default === '' ) {
139 1
			return "\"$titleText\" has no sub pages."; // TODO
140
		}
141
142 2
		if ( $default === '-' ) {
143 1
			return '';
144
		}
145
146 1
		return $default;
147
	}
148
149
	/**
150
	 * @param ProcessedParam[] $parameters
151
	 *
152
	 * @return array
153
	 */
154 21
	private function paramsToOptions( array $parameters ) {
155 21
		$options = [];
156
157 21
		foreach ( $parameters as $parameter ) {
158 21
			$options[$parameter->getName()] = $parameter->getValue();
159
		}
160
161 21
		return $options;
162
	}
163
164
}
165