Passed
Push — master ( dd1fbc...6aef8a )
by Daimona
01:29
created

Page::edit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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