Passed
Push — master ( 3d4f1f...2a2d37 )
by Daimona
01:36
created

WikiLogger::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
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 implements IFlushingAwareLogger {
14
	use LoggerTrait;
15
16
	/** @var Page */
17
	private $logPage;
18
19
	/** @var int */
20
	protected $minLevel;
21
22
	/** @var string[] */
23
	protected $buffer;
24
25
	/**
26
	 * @param Page $logPage
27
	 * @param string $minlevel
28
	 */
29
	public function __construct( Page $logPage, $minlevel = LogLevel::INFO ) {
30
		$this->minLevel = $this->levelToInt( $minlevel );
31
		$this->logPage = $logPage;
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
	 * @return string
46
	 */
47
	protected function getOutput() : string {
48
		$line = str_repeat( '-', 80 );
49
		return implode( "\n", $this->buffer ) . "\n$line\n\n";
50
	}
51
52
	/**
53
	 * @inheritDoc
54
	 */
55
	public function flush() : void {
56
		if ( $this->buffer ) {
57
			$this->logPage->edit( [
58
				'appendtext' => $this->getOutput(),
59
				'summary' => Config::getInstance()->getWikiMessage( 'error-page-summary' )
60
			] );
61
		}
62
	}
63
}
64