PlainPageRenderer::renderPage()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
ccs 9
cts 9
cp 1
crap 4
1
<?php
2
3
namespace SubPageList\Lister\UI\PageRenderer;
4
5
use SubPageList\Lister\Page;
6
use Title;
7
8
/**
9
 * @since 1.2
10
 *
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class PlainPageRenderer extends PageRenderer {
15
16
	const PATH_FULL = 'full'; // Namespace:Foo/Bar/Baz
17
	const PATH_NO_NS = 'noNs'; // Foo/Bar/Baz
18
	const PATH_NONE = 'none'; // Baz
19
	const PATH_SUB_PAGE = 'subPage'; // Baz (for full page Foo:Bar it is Foo:Bar)
20
21
	private $pathStyle;
22
23 32
	public function __construct( $pathStyle = self::PATH_FULL ) {
24 32
		$this->pathStyle = $pathStyle;
25 32
	}
26
27
	/**
28
	 * @see PageRenderer::renderPage
29
	 *
30
	 * @param Page $page
31
	 *
32
	 * @return string
33
	 */
34 32
	public function renderPage( Page $page ) {
35 32
		$title = $page->getTitle();
36
37 32
		if ( $this->pathStyle === self::PATH_NONE ) {
38 4
			return $this->getBaseText( $title );
39
		}
40
41 28
		if ( $this->pathStyle === self::PATH_NO_NS ) {
42 3
			return $this->stripNs( $title->getFullText() );
43
		}
44
45 25
		if ( $this->pathStyle === self::PATH_SUB_PAGE ) {
46 18
			return $title->getSubpageText();
47
		}
48
49 7
		return $title->getFullText();
50
	}
51
52 4
	private function getBaseText( Title $title ) {
53 4
		return $this->stripNs( $title->getSubpageText() );
54
	}
55
56 7
	private function stripNs( $text ) {
57 7
		$namespacePosition = strpos( $text, ':' );
58
59 7
		if ( $namespacePosition !== false ) {
60 3
			$text = substr( $text, $namespacePosition + 1 );
61
		}
62
63 7
		return $text;
64
	}
65
66
}
67