Passed
Push — master ( c84974...1a8235 )
by Daimona
02:33 queued 44s
created

SimpleLogger::log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 3
rs 10
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme;
4
5
use Psr\Log\AbstractLogger;
6
use Psr\Log\LogLevel;
7
8
/**
9
 * Implementation for a PSR-3 logger
10
 */
11
class SimpleLogger extends AbstractLogger {
12
	/** @var int */
13
	private $minLevel;
14
15
	/**
16
	 * @param string $minlevel
17
	 */
18
	public function __construct( $minlevel = LogLevel::INFO ) {
19
		$this->minLevel = $this->levelToInt( $minlevel );
20
	}
21
22
	/**
23
	 * Translate a LogLevel constant to an integer
24
	 *
25
	 * @param string $level
26
	 * @return int
27
	 */
28
	private function levelToInt( string $level ) : int {
29
		// Order matters
30
		$mapping = [
31
			LogLevel::DEBUG,
32
			LogLevel::INFO,
33
			LogLevel::NOTICE,
34
			LogLevel::WARNING,
35
			LogLevel::ERROR,
36
			LogLevel::CRITICAL,
37
			LogLevel::ALERT,
38
			LogLevel::EMERGENCY
39
		];
40
		return array_search( $level, $mapping );
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_search($level, $mapping) could return the type false|string which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
41
	}
42
43
	/**
44
	 * @inheritDoc
45
	 * @suppress PhanUnusedPublicMethodParameter
46
	 */
47
	public function log( $level, $message, array $context = [] ) {
48
		if ( $this->levelToInt( $level ) >= $this->minLevel ) {
49
			printf( "%s [%s] - %s\n", date( 'd M H:i:s' ), $level, $message );
50
		}
51
	}
52
}
53