Completed
Push — main ( 169704 )
by
unknown
24s queued 19s
created

TParameterizeBehavior   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 254
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 81
c 2
b 0
f 0
dl 0
loc 254
rs 8.96
wmc 43

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getRouteBehaviorName() 0 3 1
A setValidNullValue() 0 6 2
A getDefaultValue() 0 3 1
A detach() 0 8 2
A attachParamMapRoute() 0 14 3
C attach() 0 38 14
A getProperty() 0 3 1
A setEnabled() 0 9 5
A setParameter() 0 8 3
A getLocalize() 0 3 1
A setRouteBehaviorName() 0 6 2
A getValidNullValue() 0 3 1
A setProperty() 0 6 2
A getParameter() 0 3 1
A setLocalize() 0 6 2
A setDefaultValue() 0 6 2

How to fix   Complexity   

Complex Class

Complex classes like TParameterizeBehavior often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use TParameterizeBehavior, and based on these observations, apply Extract Interface, too.

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
 * @since 4.2.0
37
 */
38
class TParameterizeBehavior extends \Prado\Util\TBehavior
39
{
40
	/**
41
	 * @var string the key to the application parameter
42
	 */
43
	private $_parameter;
44
45
	/**
46
	 * @var bool whether or not a null parameter value should be set on the property
47
	 */
48
	private $_validNullValue;
49
50
	/**
51
	 * @var string the key to the application parameter
52
	 */
53
	protected $_property;
54
55
	/**
56
	 * @var string the default value of the property if there is no Parameter
57
	 */
58
	protected $_defaultValue;
59
60
	/**
61
	 * @var bool should the value be localized
62
	 */
63
	protected $_localize;
64
65
	/**
66
	 * @var object {@link TMapRouteBehavior} that routes changes from the parameter to the property
67
	 */
68
	private $_paramBehavior;
69
70
	/**
71
	 * @var string the name of the installed behavior.
72
	 */
73
	protected $_routeBehaviorName;
74
75
	/** @var bool is the behavior attached */
76
	private $_initialized = false;
77
78
	/**
79
	 * This method sets the Owner Property to the Application Parameter of Parameter. When
80
	 * {@link getRouteBehaviorName} is set, a {@link TMapRouteBehavior} is attached to
81
	 * the Application Parameter on the key so any changes are also routed to the Property.
82
	 * @param object $owner the object to which this behavior is being attached
83
	 * @throws TConfigurationException when missing the parameter, property, or property is not able to set
84
	 */
85
	public function attach($owner)
86
	{
87
		parent::attach($owner);
88
89
		if (!$this->getEnabled()) {
90
			$this->_initialized = true;
91
			return;
92
		}
93
		if (!$this->_parameter) {
94
			throw new TConfigurationException('parameterizebehavior_no_parameter');
95
		}
96
		if (!$this->_property) {
97
			throw new TConfigurationException('parameterizebehavior_no_property');
98
		}
99
		if (!$owner->canSetProperty($this->_property)) {
100
			if ($owner->canGetProperty($this->_property)) {
101
				throw new TConfigurationException('parameterizebehavior_owner_get_only_property', $this->_property);
102
			} else {
103
				throw new TConfigurationException('parameterizebehavior_owner_has_no_property', $this->_property);
104
			}
105
		}
106
107
		$appParams = Prado::getApplication()->getParameters();
108
		if (($value = $appParams->itemAt($this->_parameter)) !== null || $this->getValidNullValue()) {
109
			if ($this->_localize && $value && is_string($value)) {
110
				$value = Prado::localize($value);
111
			}
112
			$owner->setSubProperty($this->_property, $value);
113
		} elseif ($this->_defaultValue !== null) {
114
			$value = $this->_defaultValue;
115
			if ($this->_localize && is_string($value)) {
116
				$value = Prado::localize($value);
117
			}
118
			$owner->setSubProperty($this->_property, $value);
119
		}
120
		$this->_initialized = true;
121
122
		$this->attachParamMapRoute();
123
	}
124
125
	/**
126
	 *
127
	 *
128
	 * @param $owner The owner to receive parameter changes
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...
129
	 */
130
	protected function attachParamMapRoute()
131
	{
132
		if ($this->_routeBehaviorName) {
133
			$owner = $this->getOwner();
134
			if ($this->_localize) {
135
				$_property = $this->_property;
136
				$this->_paramBehavior = new TMapRouteBehavior($this->_parameter, function ($v) use ($owner, $_property) {
137
					$owner->$_property = Prado::localize($v);
138
				});
139
			} else {
140
				$this->_paramBehavior = new TMapRouteBehavior($this->_parameter, [$owner, 'set' . $this->_property]);
141
			}
142
			$appParams = Prado::getApplication()->getParameters();
143
			$appParams->attachBehavior($this->_routeBehaviorName, $this->_paramBehavior);
144
		}
145
	}
146
147
148
	/**
149
	 * This attaches and detaches the routing behavior on the Application Parameters.
150
	 * @param bool $enabled whether this behavior is enabled
151
	 */
152
	public function setEnabled($enabled)
153
	{
154
		if ($enabled == true && !$this->_paramBehavior) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
155
			$this->attachParamMapRoute();
156
		} elseif ($enabled == false && $this->_paramBehavior) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
157
			Prado::getApplication()->getParameters()->detachBehavior($this->_routeBehaviorName);
158
			$this->_paramBehavior = null;
159
		}
160
		parent::setEnabled($enabled);
161
	}
162
163
	/**
164
	 * This removes the Application Parameter handler behavior
165
	 * @param object $owner the object that this behavior is attached to.
166
	 */
167
	public function detach($owner)
168
	{
169
		if ($this->_paramBehavior) {
170
			Prado::getApplication()->getParameters()->detachBehavior($this->_routeBehaviorName);
171
			$this->_routeBehaviorName = null;
172
		}
173
		$this->_initialized = false;
174
		parent::detach($owner);
175
	}
176
177
	/**
178
	 * @return string Application parameter key to set the property.
179
	 */
180
	public function getParameter()
181
	{
182
		return $this->_parameter;
183
	}
184
185
	/**
186
	 * @param string $value Application parameter key to set the property.
187
	 */
188
	public function setParameter($value)
189
	{
190
		$this->_parameter = TPropertyValue::ensureString($value);
191
		if ($this->_paramBehavior) {
192
			if (!strlen($value)) {
193
				throw new TInvalidOperationException('parameterizebehavior_cannot_set_parameter_to_blank_after_attach');
194
			}
195
			$this->_paramBehavior->setParameter($value);
196
		}
197
	}
198
199
	/**
200
	 * @return string Application parameter key to set the property.
201
	 */
202
	public function getValidNullValue()
203
	{
204
		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...
205
	}
206
207
	/**
208
	 * @param string $value Application parameter key to set the property.
209
	 */
210
	public function setValidNullValue($value)
211
	{
212
		if ($this->_initialized) {
213
			throw new TInvalidOperationException('parameterizebehavior_cannot_set_validNullValue_after_attach');
214
		}
215
		$this->_validNullValue = TPropertyValue::ensureBoolean($value);
216
	}
217
218
	/**
219
	 * @return string Application parameter key to set the property.
220
	 */
221
	public function getProperty()
222
	{
223
		return $this->_property;
224
	}
225
226
	/**
227
	 * @param string $value Application parameter key to set the property.
228
	 */
229
	public function setProperty($value)
230
	{
231
		if ($this->_initialized) {
232
			throw new TInvalidOperationException('parameterizebehavior_cannot_set_property_after_attach');
233
		}
234
		$this->_property = TPropertyValue::ensureString($value);
235
	}
236
237
	/**
238
	 * @return string The default value when there is no property and ValidNullValue is false.
239
	 */
240
	public function getDefaultValue()
241
	{
242
		return $this->_defaultValue;
243
	}
244
245
	/**
246
	 * @param string $value The default value when there is no property and ValidNullValue is false.
247
	 */
248
	public function setDefaultValue($value)
249
	{
250
		if ($this->_initialized) {
251
			throw new TInvalidOperationException('parameterizebehavior_cannot_set_defaultValue_after_attach');
252
		}
253
		$this->_defaultValue = TPropertyValue::ensureString($value);
254
	}
255
256
	/**
257
	 * @return string should the parameter or defaultValue be localized.
258
	 */
259
	public function getLocalize()
260
	{
261
		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...
262
	}
263
264
	/**
265
	 * @param string $value should the parameter or defaultValue be localized.
266
	 */
267
	public function setLocalize($value)
268
	{
269
		if ($this->_initialized) {
270
			throw new TInvalidOperationException('parameterizebehavior_cannot_set_localize_after_attach');
271
		}
272
		$this->_localize = TPropertyValue::ensureBoolean($value);
273
	}
274
275
	/**
276
	 * @return string The TMap Routing Behavior Name for changes on the Parameter key updating the Property.
277
	 */
278
	public function getRouteBehaviorName()
279
	{
280
		return $this->_routeBehaviorName;
281
	}
282
283
	/**
284
	 * @param string $value The TMap Routing Behavior Name for changes on the Parameter key updating the Property.
285
	 */
286
	public function setRouteBehaviorName($value)
287
	{
288
		if ($this->_initialized) {
289
			throw new TInvalidOperationException('parameterizebehavior_cannot_set_routeBehaviorName_after_attach');
290
		}
291
		$this->_routeBehaviorName = TPropertyValue::ensureString($value);
292
	}
293
}
294