Passed
Push — 4.0 ( e67bf9...d6c110 )
by Jerome
15:01 queued 08:31
created

Logger::listen()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 7
eloc 8
c 1
b 1
f 0
nc 4
nop 1
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 7
rs 8.8333
1
<?php
2
3
namespace Elgg\SystemLog;
4
5
/**
6
 * Hook callbacks for systemlog
7
 *
8
 * @since 4.0
9
 * @internal
10
 */
11
class Logger {
12
13
	/**
14
	 * Default system log handler, allows plugins to override, extend or disable logging.
15
	 *
16
	 * @param \Elgg\Event $event 'log', 'systemlog'
17
	 *
18
	 * @return true
19
	 */
20 342
	public static function log(\Elgg\Event $event) {
21 342
		$object = $event->getObject();
22 342
		SystemLog::instance()->insert($object['object'], $object['event']);
23
	
24 342
		return true;
25
	}
26
	
27
	/**
28
	 * System log listener.
29
	 * This function listens to all events in the system and logs anything appropriate.
30
	 *
31
	 * @param \Elgg\Event $event 'all', 'all'
32
	 *
33
	 * @return void
34
	 */
35 342
	public static function listen(\Elgg\Event $event) {
36 342
		$type = $event->getType();
37 342
		$name = $event->getName();
38
		
39 342
		if ($type === 'systemlog' && $name === 'log') {
40 342
			return;
41
		}
42
		
43
		// ignore before and after events if there are no event handlers registered
44 342
		if (strpos($name, ':after') > 0 || strpos($name, ':before') > 0) {
45 342
			if (!elgg()->events->hasHandler($name, $type) && !elgg()->events->hasHandler($name, 'all')) {
46 342
				return;
47
			}
48
		}
49
		
50 342
		elgg_trigger_event('log', 'systemlog', ['object' => $event->getObject(), 'event' => $event->getName()]);
51 342
	}
52
	
53
	/**
54
	 * Disables the logging
55
	 *
56
	 * @param \Elgg\Event $event 'all', 'all'
57
	 *
58
	 * @return true
59
	 */
60 4
	public static function disableLogging(\Elgg\Event $event) {
61
		// disable the system log for upgrades to avoid exceptions when the schema changes.
62 4
		elgg_unregister_event_handler('log', 'systemlog', 'Elgg\SystemLog\Logger::log');
63 4
		elgg_unregister_event_handler('all', 'all', 'Elgg\SystemLog\Logger::listen');
64 4
	}
65
}
66