Passed
Push — master ( cb3898...d20083 )
by Fabio
05:58
created

TParameterizeBehavior::attach()   C

Complexity

Conditions 15
Paths 19

Size

Total Lines 42
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 29
c 1
b 0
f 0
nc 19
nop 1
dl 0
loc 42
rs 5.9166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * TParameterizeBehavior 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\Prado;
14
use Prado\TPropertyValue;
15
use Prado\Exceptions\TConfigurationException;
16
use Prado\Exceptions\TInvalidOperationException;
17
18
/**
19
 * TParameterizeBehavior class.
20
 *
21
 * TParameterizeBehavior sets a specific Property on the owner object
22
 * to a specific application parameter.  It also can install a behavior
23
 * on the Application parameters to apply any changes to the application
24
 * parameter to then route the change to the property by setting the
25
 * RouteBehaviorName.
26
 *
27
 * <code>
28
 *	<behavior name="PageThemeParameter" Class="Prado\Util\Behaviors\TParameterizeBehavior" AttachTo="Page" Parameter="ThemeName" Property="Theme" DefaultValue="Basic"/>
29
 *  <behavior name="PageTitle" Class="Prado\Util\Behaviors\TParameterizeBehavior" AttachTo="Page" Parameter="TPageTitle" Property="Title" Localize="true"/>
30
 *  <behavior name="AuthManagerExpireParameter" Class="Prado\Util\Behaviors\TParameterizeBehavior" AttachTo="module:auth" Parameter="prop:TAuthManager.AuthExpire" Property="AuthExpire" RouteBehaviorName="TAuthManagerAuthExpireRouter" />
31
 *	<behavior name="TSecurityManagerValidationKey" Class="Prado\Util\Behaviors\TParameterizeBehavior" AttachToClass="TSecurityManager" Parameter="prop:TSecurityManager.ValidationKey" Property="ValidationKey" />
32
 *	<behavior name="TSecurityManagerEncryptionKey" Class="Prado\Util\Behaviors\TParameterizeBehavior" AttachToClass="TSecurityManager" Parameter="prop:TSecurityManager.EncryptionKey" Property="EncryptionKey" />
33
 * </code>
34
 *
35
 * @author Brad Anderson <[email protected]>
36
 * @package Prado\Util\Behaviors
37
 * @since 4.2.0
38
 */
39
class TParameterizeBehavior extends \Prado\Util\TBehavior
40
{
41
	/**
42
	 * @var string the key to the application parameter
43
	 */
44
	private $_parameter;
45
	
46
	/**
47
	 * @var bool whether or not a null parameter value should be set on the property
48
	 */
49
	private $_validNullValue;
50
	
51
	/**
52
	 * @var string the key to the application parameter
53
	 */
54
	protected $_property;
55
	
56
	/**
57
	 * @var string the default value of the property if there is no Parameter
58
	 */
59
	protected $_defaultValue;
60
	
61
	/**
62
	 * @var bool should the value be localized
63
	 */
64
	protected $_localize;
65
	
66
	/**
67
	 * @var object {@link TMapRouteBehavior} that routes changes from the parameter to the property
68
	 */
69
	private $_paramBehavior;
70
	
71
	/**
72
	 * @var the name of the installed behavior.
0 ignored issues
show
Bug introduced by
The type Prado\Util\Behaviors\the 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
	 */
74
	protected $_routeBehaviorName;
75
	
76
	/** @var bool is the behavior attached */
77
	private $_initialized = false;
78
	
79
	/**
80
	 * This method sets the Owner Property to the Application Parameter of Parameter. When
81
	 * {@link getRouteBehaviorName} is set, a {@link TMapRouteBehavior} is attached to
82
	 * the Application Parameter on the key so any changes are also routed to the Property.
83
	 * @param $owner object the object to which this behavior is being attached
84
	 * @throws TConfigurationException when missing the parameter, property, or property is not able to set
85
	 */
86
	public function attach($owner)
87
	{
88
		parent::attach($owner);
89
		
90
		if (!$this->_parameter) {
91
			throw new TConfigurationException('parameterizebehavior_no_parameter');
92
		}
93
		if (!$this->_property) {
94
			throw new TConfigurationException('parameterizebehavior_no_property');
95
		}
96
		if (!$owner->canSetProperty($this->_property)) {
97
			if ($owner->canGetProperty($this->_property)) {
98
				throw new TConfigurationException('parameterizebehavior_owner_get_only_property', $this->_property);
99
			} else {
100
				throw new TConfigurationException('parameterizebehavior_owner_has_no_property', $this->_property);
101
			}
102
		}
103
		
104
		$appParams = Prado::getApplication()->getParameters();
105
		if (($value = $appParams->itemAt($this->_parameter)) !== null || $this->getValidNullValue()) {
106
			if ($this->_localize && $value && is_string($value)) {
107
				$value = Prado::localize($value);
108
			}
109
			$owner->setSubProperty($this->_property, $value);
110
		} elseif ($this->_defaultValue !== null) {
111
			$value = $this->_defaultValue;
112
			if ($this->_localize && is_string($value)) {
113
				$value = Prado::localize($value);
114
			}
115
			$owner->setSubProperty($this->_property, $value);
116
		}
117
		$this->_initialized = true;
118
		if ($this->_routeBehaviorName) {
119
			if ($this->_localize) {
120
				$_property = $this->_property;
121
				$this->_paramBehavior = new TMapRouteBehavior($this->_parameter, function ($v) use ($owner, $_property) {
122
					$owner->$_property = Prado::localize($v);
123
				});
124
			} else {
125
				$this->_paramBehavior = new TMapRouteBehavior($this->_parameter, [$owner, 'set' . $this->_property]);
126
			}
127
			$appParams->attachBehavior($this->_routeBehaviorName, $this->_paramBehavior);
128
		}
129
	}
130
	
131
	/**
132
	 * This removes the Application Parameter handler behavior
133
	 * @param $owner object the object that this behavior is attached to.
134
	 */
135
	public function detach($owner)
136
	{
137
		if ($this->_paramBehavior) {
138
			Prado::getApplication()->getParameters()->detachBehavior($this->_routeBehaviorName);
139
		}
140
		$this->_initialized = false;
141
		parent::detach($owner);
142
	}
143
	
144
	/**
145
	 * @return string Application parameter key to set the property.
146
	 */
147
	public function getParameter()
148
	{
149
		return $this->_parameter;
150
	}
151
	
152
	/**
153
	 * @param string $value Application parameter key to set the property.
154
	 */
155
	public function setParameter($value)
156
	{
157
		$this->_parameter = TPropertyValue::ensureString($value);
158
		if ($this->_paramBehavior) {
159
			if (!strlen($value)) {
160
				throw new TInvalidOperationException('parameterizebehavior_cannot_set_parameter_to_blank_after_attach');
161
			}
162
			$this->_paramBehavior->setParameter($value);
163
		}
164
	}
165
	
166
	/**
167
	 * @return string Application parameter key to set the property.
168
	 */
169
	public function getValidNullValue()
170
	{
171
		return $this->_validNullValue;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_validNullValue returns the type boolean which is incompatible with the documented return type string.
Loading history...
172
	}
173
	
174
	/**
175
	 * @param string $value Application parameter key to set the property.
176
	 */
177
	public function setValidNullValue($value)
178
	{
179
		if ($this->_initialized) {
180
			throw new TInvalidOperationException('parameterizebehavior_cannot_set_validNullValue_after_attach');
181
		}
182
		$this->_validNullValue = TPropertyValue::ensureBoolean($value);
183
	}
184
	
185
	/**
186
	 * @return string Application parameter key to set the property.
187
	 */
188
	public function getProperty()
189
	{
190
		return $this->_property;
191
	}
192
	
193
	/**
194
	 * @param string $value Application parameter key to set the property.
195
	 */
196
	public function setProperty($value)
197
	{
198
		if ($this->_initialized) {
199
			throw new TInvalidOperationException('parameterizebehavior_cannot_set_property_after_attach');
200
		}
201
		$this->_property = TPropertyValue::ensureString($value);
202
	}
203
	
204
	/**
205
	 * @return string The default value when there is no property and ValidNullValue is false.
206
	 */
207
	public function getDefaultValue()
208
	{
209
		return $this->_defaultValue;
210
	}
211
	
212
	/**
213
	 * @param string $value The default value when there is no property and ValidNullValue is false.
214
	 */
215
	public function setDefaultValue($value)
216
	{
217
		if ($this->_initialized) {
218
			throw new TInvalidOperationException('parameterizebehavior_cannot_set_defaultValue_after_attach');
219
		}
220
		$this->_defaultValue = TPropertyValue::ensureString($value);
221
	}
222
	
223
	/**
224
	 * @return string should the parameter or defaultValue be localized.
225
	 */
226
	public function getLocalize()
227
	{
228
		return $this->_localize;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_localize returns the type boolean which is incompatible with the documented return type string.
Loading history...
229
	}
230
	
231
	/**
232
	 * @param string $value should the parameter or defaultValue be localized.
233
	 */
234
	public function setLocalize($value)
235
	{
236
		if ($this->_initialized) {
237
			throw new TInvalidOperationException('parameterizebehavior_cannot_set_localize_after_attach');
238
		}
239
		$this->_localize = TPropertyValue::ensureBoolean($value);
240
	}
241
	
242
	/**
243
	 * @return string The TMap Routing Behavior Name for changes on the Parameter key updating the Property.
244
	 */
245
	public function getRouteBehaviorName()
246
	{
247
		return $this->_routeBehaviorName;
248
	}
249
	
250
	/**
251
	 * @param string $value The TMap Routing Behavior Name for changes on the Parameter key updating the Property.
252
	 */
253
	public function setRouteBehaviorName($value)
254
	{
255
		if ($this->_initialized) {
256
			throw new TInvalidOperationException('parameterizebehavior_cannot_set_routeBehaviorName_after_attach');
257
		}
258
		$this->_routeBehaviorName = TPropertyValue::ensureString($value);
0 ignored issues
show
Documentation Bug introduced by
It seems like Prado\TPropertyValue::ensureString($value) of type string is incompatible with the declared type Prado\Util\Behaviors\the of property $_routeBehaviorName.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
259
	}
260
}
261