WikiLogger::flush()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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