Passed
Push — master ( e877b4...cb3898 )
by Fabio
05:28
created

TMapRouteBehavior::getParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * TMapRouteBehavior class file.
5
 *
6
 * @author Brad Anderson <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
11
namespace Prado\Util\Behaviors;
12
13
use Prado\Exceptions\TInvalidDataTypeException;
14
use Prado\Util\TBehavior;
15
16
/**
17
 * TMapRouteBehavior class.
18
 *
19
 * TMapRouteBehavior routes the changes to Application Parameters to
20
 * actual functions to affect change.
21
 * <code>
22
 *		Prado::getApplication()->getParameters()->attachBehavior('name'
23
 *			new TMapRouteBehavior('parameterToHook', [$obj, 'setParam']));
24
 * </code>
25
 * This code will call $obj->setParam($value) every time the parameter
26
 * 'parameterToHook' changes in the Application Parameters.
27
 *
28
 * <code>
29
 *		Prado::getApplication()->getParameters()->attachBehavior('name'
30
 *			new TMapRouteBehavior(null, [$obj, 'setParam']));
31
 * </code>
32
 * This code will call $obj->setParam($key, $value) every time any parameter
33
 * changes in the Application Parameters.
34
 *
35
 * @author Brad Anderson <[email protected]>
36
 * @package Prado\Util\Behaviors
37
 * @since 4.2.0
38
 */
39
class TMapRouteBehavior extends TBehavior
40
{
41
	/**
42
	 * @var string the parameter to check for when there are changes.
43
	 */
44
	private $_parameter;
45
	
46
	/**
47
	 * @var callable the parameter to check for when there are changes
48
	 */
49
	private $_handler;
50
	
51
	/**
52
	 * @param $parameter string  the name of the map key parameter to hook
53
	 * @param $handler callable the handler for setting the parameter
54
	 */
55
	public function __construct($parameter, $handler)
56
	{
57
		if (!is_callable($handler)) {
58
			throw new TInvalidDataTypeException('maproutebehavior_handler_not_callable');
59
		}
60
		$this->_parameter = $parameter;
61
		$this->_handler = $handler;
62
		parent::__construct();
63
	}
64
	
65
	/**
66
	 * This is the dynamic event for handling TMap dyAddItem.
67
	 * When there is a parameter, when the key is equal to the parameter,
68
	 * this calls handler($value).
69
	 * When parameter is null, this calls handler($key, $value).
70
	 * @param $key string the key of the item being added
71
	 * @param $value mixed the value of the item being added
72
	 * @param $callchain TCallChain of event handlers
0 ignored issues
show
Bug introduced by
The type Prado\Util\Behaviors\TCallChain was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
73
	 * @return mixed returns the argv[0], chained to all handlers
74
	 */
75
	public function dyAddItem($key, $value, $callchain)
76
	{
77
		if ($key == $this->_parameter && $key !== null && $this->_handler) {
78
			call_user_func($this->_handler, $value);
79
		} elseif ($this->_parameter === null) {
80
			call_user_func($this->_handler, $key, $value);
81
		}
82
		return $callchain->dyAddItem($key, $value);
83
	}
84
	
85
	
86
	/**
87
	 * This is the dynamic event for handling TMap dyRemoveItem.
88
	 * When there is a parameter, when the key is equal to the parameter,
89
	 * this calls handler(null).
90
	 * When parameter is null, this calls handler($key, null).
91
	 * @param $key string the key of the item being added
92
	 * @param $value mixed the value of the item being added
93
	 * @param $callchain TCallChain of event handlers
94
	 * @return mixed returns the argv[0], chained to all handlers
95
	 */
96
	public function dyRemoveItem($key, $value, $callchain)
97
	{
98
		if ($key == $this->_parameter && $key !== null && $this->_handler) {
99
			call_user_func($this->_handler, null);
100
		} elseif ($this->_parameter === null) {
101
			call_user_func($this->_handler, $key, null);
102
		}
103
		return $callchain->dyRemoveItem($key, $value);
104
	}
105
	
106
	/**
107
	 * @return string parameter
108
	 */
109
	public function getParameter()
110
	{
111
		return $this->_parameter;
112
	}
113
	
114
	/**
115
	 *
116
	 * @param mixed $param
117
	 */
118
	public function setParameter($param)
119
	{
120
		$this->_parameter = $param;
121
	}
122
}
123