ObservableMessageReporter::reportMessage()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.9666
cc 3
nc 4
nop 1
crap 3
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