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

TTimeZoneParameterBehavior::setEnabled()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 9.6111
1
<?php
2
3
/**
4
 * TTimeZoneParameterBehavior 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\Util\TBehavior;
15
16
/**
17
 * TTimeZoneParameterBehavior class.
18
 *
19
 * TTimeZoneParameterBehavior sets the date_default_timezone_set.
20
 * This parameterizes the TimeZone.   {@link TimeZoneParameter} is
21
 * the key to the Application Parameter for setting the TimeZone.
22
 *
23
 * This Behavior is designed to attach to TApplication, but can be
24
 * attached to any TComponent.
25
 *
26
 * <code>
27
 *		<behavior name="TimeZoneParameter" Class="Prado\Util\Behaviors\TTimeZoneParameterBehavior" AttachTo="Application" TimeZoneParameter="TimeZone" TimeZone="America/New_York"/>
28
 * </code>
29
 * This code will set the default timeZone to "America/New_York", and then
30
 * if there is any Application Parameter in "TimeZone", then that takes
31
 * precedence.  Setting the TimeZoneParameter to "" will disable the
32
 * parameter functionality and set the TimeZone from the attribute TimeZone.
33
 *
34
 * This routes changes in the Application Parameter {@link TimeZoneParameter}
35
 * to {@link setTimeZone}. The default TimeZoneParameter is 'prop:TimeZone'.
36
 *
37
 * @author Brad Anderson <[email protected]>
38
 * @since 4.2.0
39
 */
40
class TTimeZoneParameterBehavior extends TBehavior
41
{
42
	/**
43
	 * Name of the Application Parameter Routing Behavior
44
	 */
45
	public const APP_PARAM_ROUTE_BEHAVIOR_NAME = 'TimeZoneParameter';
46
47
	/**
48
	 * Default TimeZoneParameter
49
	 */
50
	public const TIMEZONE_PARAMETER_NAME = 'prop:TimeZone';
51
52
	/**
53
	 * @var string the page theme is set to this parameter key
54
	 */
55
	private $_timeZoneParameter = self::TIMEZONE_PARAMETER_NAME;
56
57
	/**
58
	 * @var object {@link TMapRouteBehavior} that routes changes to the parameter
59
	 * is handled by setTimeZone.
60
	 */
61
	private $_paramBehavior;
62
63
	/**
64
	 * This sets the date_default_timezone_set with the value of the TimeZoneParameter
65
	 * in the application parameters.  It attaches the Application Parameter handler behavior.
66
	 * @param object $owner the object that this behavior is attached to.
67
	 */
68
	public function attach($owner)
69
	{
70
		parent::attach($owner);
71
		if (!$this->getEnabled() || !$this->_timeZoneParameter) {
72
			return;
73
		}
74
		$appParams = Prado::getApplication()->getParameters();
75
		if ($default_timezone = $appParams->itemAt($this->_timeZoneParameter)) {
76
			$this->setTimeZone($default_timezone);
77
		}
78
		$this->_paramBehavior = new TMapRouteBehavior($this->_timeZoneParameter, [$this, 'setTimeZone']);
79
		$appParams->attachBehavior(self::APP_PARAM_ROUTE_BEHAVIOR_NAME, $this->_paramBehavior);
80
	}
81
82
	/**
83
	 * This removes the Application Parameter handler behavior
84
	 * @param object $owner the object that this behavior is attached to.
85
	 */
86
	public function detach($owner)
87
	{
88
		if ($this->_paramBehavior) {
89
			Prado::getApplication()->getParameters()->detachBehavior(self::APP_PARAM_ROUTE_BEHAVIOR_NAME);
90
		}
91
		parent::detach($owner);
92
	}
93
94
95
	/**
96
	 * This attaches and detaches the routing behavior on the Application Parameters.
97
	 * @param bool $enabled whether this behavior is enabled
98
	 */
99
	public function setEnabled($enabled)
100
	{
101
		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...
102
			$this->_paramBehavior = new TMapRouteBehavior($this->_timeZoneParameter, [$this, 'setTimeZone']);
103
			Prado::getApplication()->getParameters()->attachBehavior(self::APP_PARAM_ROUTE_BEHAVIOR_NAME, $this->_paramBehavior);
104
		} 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...
105
			Prado::getApplication()->getParameters()->detachBehavior(self::APP_PARAM_ROUTE_BEHAVIOR_NAME);
106
			$this->_paramBehavior = null;
107
		}
108
		parent::setEnabled($enabled);
109
	}
110
111
	/**
112
	 * @return string Application parameter key to set the php TimeZone.
113
	 */
114
	public function getTimeZoneParameter()
115
	{
116
		return $this->_timeZoneParameter;
117
	}
118
119
	/**
120
	 * @param string $value Application parameter key to set the php TimeZone.
121
	 */
122
	public function setTimeZoneParameter($value)
123
	{
124
		if ($this->_paramBehavior) {
125
			$this->_paramBehavior->setParameter($value);
126
		}
127
		$this->_timeZoneParameter = $value;
128
	}
129
130
	/**
131
	 * @return string the timeZone from date_default_timezone_get.
132
	 */
133
	public function getTimeZone()
134
	{
135
		return date_default_timezone_get();
136
	}
137
138
	/**
139
	 * @param string $value passthrough to date_default_timezone_set
140
	 */
141
	public function setTimeZone($value)
142
	{
143
		$set = true;
144
		try {
145
			date_default_timezone_set($value);
146
		} catch (\Exception $e) {
147
			$set = false;
148
		}
149
		return $set;
150
	}
151
}
152