DispatchContext::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 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