DispatchContext   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 72
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A newFromArray() 0 9 2
A has() 0 3 1
A set() 0 3 1
A get() 0 8 2
A isPropagationStopped() 0 3 2
1
<?php
2
3
namespace Onoi\EventDispatcher;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Generic context that can be added during the dispatch process to be
9
 * accessible to each invoked listener
10
 *
11
 * @license GNU GPL v2+
12
 * @since 1.0
13
 *
14
 * @author mwjames
15
 */
16
class DispatchContext {
17
18
	/**
19
	 * @var array
20
	 */
21
	private $container = array();
22
23
	/**
24
	 * @since 1.1
25
	 *
26
	 * @param array $container
27
	 *
28
	 * @return DispatchContext
29
	 */
30 1
	public static function newFromArray( array $container ) {
31 1
		$dispatchContext = new DispatchContext();
32
33 1
		foreach ( $container as $key => $value ) {
34 1
			$dispatchContext->set( $key, $value );
35 1
		}
36
37 1
		return $dispatchContext;
38
	}
39
40
	/**
41
	 * @since 1.0
42
	 *
43
	 * @param string $key
44
	 *
45
	 * @return boolean
46
	 */
47 7
	public function has( $key ) {
48 7
		return isset( $this->container[strtolower( $key )] );
49
	}
50
51
	/**
52
	 * @since 1.0
53
	 *
54
	 * @param string $key
55
	 * @param mixed $value
56
	 */
57 6
	public function set( $key, $value ) {
58 6
		$this->container[strtolower( $key )] = $value;
59 6
	}
60
61
	/**
62
	 * @since 1.0
63
	 *
64
	 * @param string $key
65
	 *
66
	 * @return mixed
67
	 * @throws InvalidArgumentException
68
	 */
69 7
	public function get( $key ) {
70
71 7
		if ( $this->has( $key ) ) {
72 6
			return $this->container[strtolower( $key )];
73
		}
74
75 1
		throw new InvalidArgumentException( "{$key} is unknown" );
76
	}
77
78
	/**
79
	 * @since 1.0
80
	 *
81
	 * @return boolean
82
	 */
83 4
	public function isPropagationStopped() {
84 4
		return $this->has( 'propagationstop' ) ? $this->get( 'propagationstop' ) : false;
85
	}
86
87
}
88