Test Failed
Push — master ( d9ae52...79202d )
by Jean-Christophe
11:01
created

Logger   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 59.61%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 29
eloc 35
c 1
b 0
f 0
dl 0
loc 104
ccs 31
cts 52
cp 0.5961
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A registerError() 0 2 1
A error() 0 3 2
A init() 0 3 3
A warn() 0 3 2
A alert() 0 3 2
A critical() 0 3 2
A createLogger() 0 8 3
A log() 0 3 2
A inContext() 0 5 2
A info() 0 3 2
A asObjects() 0 4 3
A clearAll() 0 3 2
A isActive() 0 2 1
A close() 0 3 2
1
<?php
2
3
namespace Ubiquity\log;
4
5
/**
6
 * Abstract class for logging
7
 * Ubiquity\log$Logger
8
 * This class is part of Ubiquity
9
 *
10
 * @author jcheron <[email protected]>
11
 * @version 1.0.3
12
 *
13
 */
14
abstract class Logger {
15
	/**
16
	 *
17
	 * @var Logger
18
	 */
19
	private static $instance;
20
	private static $test;
21
22 42
	private static function createLogger(&$config) {
23 42
		if (\is_callable ( $logger = $config ["logger"] )) {
24 42
			$instance = $logger ();
25
		} else {
26
			$instance = $config ["logger"];
27
		}
28 42
		if ($instance instanceof Logger) {
29 42
			self::$instance = $instance;
30
		}
31 42
	}
32
33
	abstract public function _registerError();
34
35
	public static function registerError() {
36
		self::$instance->_registerError ();
37
	}
38
39
	public static function inContext($contexts, $context) {
40
		if ($contexts === null) {
41
			return true;
42
		}
43
		return array_search ( $context, $contexts ) !== false;
44
	}
45
46 42
	public static function init(&$config) {
47 42
		if ($config ["debug"] === true && (self::$test = isset ( $config ["logger"] ))) {
48 42
			self::createLogger ( $config );
49
		}
50 42
	}
51
52
	public static function log($level, $context, $message, $part = null, $extra = null) {
53
		if (self::$test)
54
			self::$instance->_log ( $level, $context, $message, $part, $extra );
55
	}
56
57 89
	public static function info($context, $message, $part = null, $extra = null) {
58 89
		if (self::$test)
59 89
			self::$instance->_info ( $context, $message, $part, $extra );
60 89
	}
61
62 3
	public static function warn($context, $message, $part = null, $extra = null) {
63 3
		if (self::$test)
64 3
			self::$instance->_warn ( $context, $message, $part, $extra );
65 3
	}
66
67 1
	public static function error($context, $message, $part = null, $extra = null) {
68 1
		if (self::$test)
69 1
			self::$instance->_error ( $context, $message, $part, $extra );
70 1
	}
71
72
	public static function critical($context, $message, $part = null, $extra = null) {
73
		if (self::$test)
74
			self::$instance->_critical ( $context, $message, $part, $extra );
75
	}
76
77
	public static function alert($context, $message, $part = null, $extra = null) {
78
		if (self::$test)
79
			self::$instance->_alert ( $context, $message, $part, $extra );
80
	}
81
82 1
	public static function asObjects($reverse = true, $maxlines = 10, $contexts = null) {
83 1
		if (isset ( self::$instance ) && self::$test)
84 1
			return self::$instance->_asObjects ( $reverse, $maxlines, $contexts );
85
		return [ ];
86
	}
87
88 1
	public static function clearAll() {
89 1
		if (self::$test) {
90 1
			self::$instance->_clearAll ();
91
		}
92 1
	}
93
94
	public static function close() {
95
		if (self::$test) {
96
			self::$instance->close ();
97
		}
98
	}
99
100
	abstract public function _log($level, $context, $message, $part, $extra);
101
102
	abstract public function _info($context, $message, $part, $extra);
103
104
	abstract public function _warn($context, $message, $part, $extra);
105
106
	abstract public function _error($context, $message, $part, $extra);
107
108
	abstract public function _critical($context, $message, $part, $extra);
109
110 23
	abstract public function _alert($context, $message, $part, $extra);
111 23
112
	abstract public function _asObjects($reverse = true, $maxlines = 10, $contexts = null);
113
114
	abstract public function _clearAll();
115
116
	public static function isActive() {
117
		return self::$test;
118
	}
119
}
120