Passed
Push — master ( f946ea...c739e9 )
by Daimona
02:07
created

WikiLogger::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Logger;
4
5
use BotRiconferme\Config;
6
use BotRiconferme\Wiki\Page\Page;
7
use Psr\Log\AbstractLogger;
8
use Psr\Log\LogLevel;
9
10
/**
11
 * Logger that sends messages on-wiki
12
 */
13
class WikiLogger extends AbstractLogger {
14
	use LoggerTrait;
15
16
	/** @var int */
17
	private $minLevel;
18
19
	/** @var Page */
20
	private $logPage;
21
22
	/** @var string[] */
23
	private $buffer;
24
25
	/**
26
	 * @param Page $logPage
27
	 * @param string $minlevel
28
	 */
29
	public function __construct( Page $logPage, $minlevel = LogLevel::INFO ) {
30
		$this->logPage = $logPage;
31
		$this->minLevel = $this->levelToInt( $minlevel );
32
	}
33
34
	/**
35
	 * @inheritDoc
36
	 * @suppress PhanUnusedPublicMethodParameter
37
	 */
38
	public function log( $level, $message, array $context = [] ) {
39
		if ( $this->levelToInt( $level ) >= $this->minLevel ) {
40
			$this->buffer[] = $this->getFormattedMessage( $level, $message );
41
		}
42
	}
43
44
	/**
45
	 * Actually writes data to the wiki
46
	 */
47
	public function doOutput() {
48
		if ( $this->buffer ) {
49
			$this->logPage->edit( [
50
				'appendtext' => implode( "\n", $this->buffer ),
51
				'summary' => Config::getInstance()->getWikiMessage( 'error-page-summary' )
52
			] );
53
		}
54
	}
55
56
	/**
57
	 * @todo Can we move this?
58
	 */
59
	public function __destruct() {
60
		$this->doOutput();
61
	}
62
}
63