Passed
Push — master ( 0986c5...31e6ff )
by Daimona
01:50
created

Page::getContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme;
4
5
/**
6
 * Represents a single on-wiki page
7
 */
8
class Page {
9
	/** @var string */
10
	protected $title;
11
	/** @var WikiController */
12
	protected $controller;
13
	/** @var string */
14
	protected $content;
15
16
	/**
17
	 * @param string $title
18
	 * @param WikiController $controller
19
	 */
20
	public function __construct( string $title, WikiController $controller ) {
21
		$this->title = $title;
22
		$this->controller = $controller;
23
	}
24
25
	/**
26
	 * @return string
27
	 */
28
	public function getTitle() : string {
29
		return $this->title;
30
	}
31
32
	/**
33
	 * Get the content of this page
34
	 *
35
	 * @param int|null $section A section number to retrieve the content of that section
36
	 * @return string
37
	 */
38
	public function getContent( int $section = null ) : string {
39
		if ( $this->content === null ) {
40
			$this->content = $this->controller->getPageContent( $this->title, $section );
41
		}
42
		return $this->content;
43
	}
44
45
	/**
46
	 * Edit this page and update content
47
	 *
48
	 * @param array $params
49
	 */
50
	public function edit( array $params ) {
51
		$params = [
52
			'title' => $this->getTitle()
53
		] + $params;
54
55
		$this->controller->editPage( $params );
56
		if ( isset( $params['text'] ) ) {
57
			$this->content = $params['text'];
58
		} elseif ( isset( $params['appendtext'] ) ) {
59
			$this->content .= $params['appendtext'];
60
		} else {
61
			// Clear the cache anyway
62
			( new Logger )->warning( 'Resetting content cache. Params: ' . var_export( $params, true ) );
63
			$this->content = null;
64
		}
65
	}
66
67
	/**
68
	 * For easier logging etc.
69
	 *
70
	 * @return string
71
	 */
72
	public function __toString() {
73
		return $this->getTitle();
74
	}
75
}
76