Skip to content
This repository was archived by the owner on Oct 26, 2024. It is now read-only.

Commit ff430a5

Browse files
committed
Splitted timer analysis into multiple methods
1 parent a8fdb46 commit ff430a5

File tree

1 file changed

+113
-65
lines changed

1 file changed

+113
-65
lines changed

Config/ConfigTimer.php

Lines changed: 113 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -24,82 +24,130 @@ final class ConfigTimer extends ConfigAbstract implements ConfigInterface
2424
*/
2525
public function setConfig($file)
2626
{
27-
/** @var array $config */
28-
$config = $this->readConfig($file);
29-
3027
// iterate conf and check if there are dates/datetimes/times and so on, for conversion
31-
foreach ($config as $timerKey => $timers) {
28+
foreach ($this->readConfig($file) as $timerKey => $timers) {
3229
switch ($timerKey) {
3330
case 'timers':
34-
foreach ($timers as $timerSubKey => $params) {
35-
foreach ($params as $paramKey => $paramVal) {
36-
switch ($paramKey) {
37-
case 'interval':
38-
// check the contents of interval
39-
foreach ($paramVal as $intervalKey => $interval) {
40-
// convert all sub elements
41-
foreach ($interval as $key => $intervalValue) {
42-
// just a date, no time - one element
43-
switch (count($interval)) {
44-
case 1:
45-
$dateEnd = new \DateTime(
46-
$intervalValue . ' 23:59:59'
47-
);
48-
$config[$timerKey][$timerSubKey][$paramKey][$intervalKey][1] =
49-
$dateEnd;
50-
//fallthrough
51-
case 2:
52-
$data = new \DateTime(
53-
$intervalValue
54-
);
55-
$config[$timerKey][$timerSubKey][$paramKey][$intervalKey][$key] =
56-
$data;
57-
break;
58-
}
59-
}
60-
}
61-
break;
62-
default:
63-
break;
64-
}
65-
}
66-
}
31+
$this->handleTimers($timers, $timerKey);
6732
break;
6833
case 'holidays':
69-
foreach ($timers as $timerSubKey => $params) {
70-
foreach ($params as $paramKey => $paramValue) {
71-
$config[$timerKey][$timerSubKey] = [
72-
new \DateTime($paramValue),
73-
new \DateTime($paramValue . ' 23:59:59'),
74-
];
75-
}
76-
}
34+
$this->handleHolidays($timers, $timerKey);
7735
break;
7836
case 'general_holidays':
79-
$tmpConf = [];
80-
$year = date('Y');
81-
// get server's easter date for later calculation
82-
$easterDate = new \DateTime(date('Y-m-d', easter_date($year)));
37+
$this->handleGeneralHolidays($timers, $timerKey);
38+
break;
39+
default:
40+
$this->set($timerKey, $timers);
41+
break;
42+
}
43+
}
44+
}
8345

84-
foreach ($timers as $params) {
85-
switch ($params['type']) {
86-
case 'fix':
87-
$tmpConf[$params['name']] = new \DateTime(
88-
$year . '-' . $params['value'][0] . '-' . $params['value'][1]
89-
);
90-
break;
91-
case 'var':
92-
$easterDateClone = clone $easterDate;
93-
$tmpConf[$params['name']] = $easterDateClone->{$params['value'][0]}(
94-
new \DateInterval('P' . $params['value'][1] . 'D')
95-
);
96-
break;
46+
/**
47+
* Generate Datetime objects from config values.
48+
*
49+
* @param array $timers
50+
* @param string $timerKey
51+
*/
52+
private function handleTimers(array $timers, $timerKey)
53+
{
54+
foreach ($timers as $timerSubKey => $params) {
55+
foreach ($params as $paramKey => $paramVal) {
56+
switch ($paramKey) {
57+
case 'interval':
58+
// check the contents of interval
59+
foreach ($paramVal as $intervalKey => $interval) {
60+
// convert all sub elements
61+
foreach ($interval as $key => $intervalValue) {
62+
// just a date, no time - one element
63+
switch (count($interval)) {
64+
case 1:
65+
$this->set(
66+
$timerKey,
67+
$timerSubKey,
68+
$paramKey,
69+
$intervalKey,
70+
1,
71+
new \DateTime(
72+
$intervalValue . ' 23:59:59'
73+
)
74+
);
75+
//fallthrough
76+
case 2:
77+
$this->set(
78+
$timerKey,
79+
$timerSubKey,
80+
$paramKey,
81+
$intervalKey,
82+
$key,
83+
new \DateTime(
84+
$intervalValue
85+
)
86+
);
87+
break;
88+
}
89+
}
9790
}
98-
}
99-
$config[$timerKey] = $tmpConf;
91+
break;
92+
default:
93+
$this->set($timerKey, $timerSubKey, $paramKey, $paramVal);
94+
break;
95+
}
96+
}
97+
}
98+
}
99+
100+
/**
101+
* Generate holiday DateTime objects for config.
102+
*
103+
* @param array $timers
104+
* @param mixed $timerKey
105+
*/
106+
private function handleHolidays(array $timers, $timerKey)
107+
{
108+
foreach ($timers as $timerSubKey => $params) {
109+
foreach ($params as $paramKey => $paramValue) {
110+
$this->set(
111+
$timerKey,
112+
$timerSubKey,
113+
[
114+
new \DateTime($paramValue),
115+
new \DateTime($paramValue . ' 23:59:59'),
116+
]
117+
);
118+
}
119+
}
120+
}
121+
122+
/**
123+
* Handle holiday list and covert to DateTime where possible.
124+
*
125+
* @param array $timers
126+
* @param mixed $timerKey
127+
*/
128+
private function handleGeneralHolidays(array $timers, $timerKey)
129+
{
130+
$tmpConf = [];
131+
$year = date('Y');
132+
// get server's easter date for later calculation
133+
$easterDate = new \DateTime(date('Y-m-d', easter_date($year)));
134+
135+
foreach ($timers as $params) {
136+
switch ($params['type']) {
137+
case 'fix':
138+
$tmpConf[$params['name']] = new \DateTime(
139+
$year . '-' . $params['value'][0] . '-' . $params['value'][1]
140+
);
141+
break;
142+
case 'var':
143+
$easterDateClone = clone $easterDate;
144+
$tmpConf[$params['name']] = $easterDateClone->{$params['value'][0]}(
145+
new \DateInterval('P' . $params['value'][1] . 'D')
146+
);
147+
break;
100148
}
101149
}
102150

103-
$this->setByArray($config);
151+
$this->set($timerKey, $tmpConf);
104152
}
105153
}

0 commit comments

Comments
 (0)