ObservableMessageReporter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 60
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1
ccs 13
cts 13
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A reportMessage() 0 9 3
A registerMessageReporter() 0 3 1
A registerReporterCallback() 0 5 2
1
<?php
2
3
namespace Onoi\MessageReporter;
4
5
/**
6
 * Message reporter that reports messages by passing them along to all
7
 * registered handlers.
8
 *
9
 * @since 1.0
10
 *
11
 * @license GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class ObservableMessageReporter implements MessageReporter {
15
16
	/**
17
	 * @since 1.0
18
	 *
19
	 * @var MessageReporter[]
20
	 */
21
	protected $reporters = [];
22
23
	/**
24
	 * @since 1.0
25
	 *
26
	 * @var callable[]
27
	 */
28
	protected $callbacks = [];
29
30
	/**
31
	 * @since 1.0
32
	 *
33
	 * @param string $message
34
	 *
35
	 * @see MessageReporter::report
36
	 */
37 313
	public function reportMessage( $message ) {
38 313
		foreach ( $this->reporters as $reporter ) {
39 208
			$reporter->reportMessage( $message );
40
		}
41
42 313
		foreach ( $this->callbacks as $callback ) {
43 313
			call_user_func( $callback, $message );
44
		}
45 313
	}
46
47
	/**
48
	 * Register a new message reporter.
49
	 *
50
	 * @since 1.0
51
	 *
52
	 * @param MessageReporter $reporter
53
	 *
54
	 */
55 208
	public function registerMessageReporter( MessageReporter $reporter ) {
56 208
		$this->reporters[] = $reporter;
57 208
	}
58
59
	/**
60
	 * Register a callback as message reporter.
61
	 *
62
	 * @since 1.0
63
	 *
64
	 * @param callable $handler |null
65
	 *
66
	 */
67 313
	public function registerReporterCallback( $handler = null ) {
68 313
		if ( is_callable( $handler ) ) {
69 313
			$this->callbacks[] = $handler;
70
		}
71 313
	}
72
73
}
74