ConfigTimer   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 0
loc 136
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfig() 0 19 5
B handleTimers() 0 47 8
A handleHolidays() 0 15 3
A handleGeneralHolidays() 0 25 4
1
<?php
2
/*
3
 * This file is part of the php-utilities package.
4
 *
5
 * (c) Marc Aschmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Asm\Config;
11
12
/**
13
 * Class ConfigTimer
14
 *
15
 * @package Asm\Config
16
 * @author Marc Aschmann <[email protected]>
17
 */
18
final class ConfigTimer extends AbstractConfig implements ConfigInterface
19
{
20
    /**
21
     * Convert config date strings to \DateTime objects or \DateIntervals.
22
     *
23
     * @param string $file config file
24
     * @return $this
25
     */
26
    public function setConfig(string $file)
27
    {
28
        // iterate conf and check if there are dates/datetimes/times and so on, for conversion
29
        foreach ($this->readConfig($file) as $timerKey => $timers) {
30
            switch ($timerKey) {
31
                case 'timers':
32
                    $this->handleTimers($timers, $timerKey);
33
                    break;
34
                case 'holidays':
35
                    $this->handleHolidays($timers, $timerKey);
36
                    break;
37
                case 'general_holidays':
38
                    $this->handleGeneralHolidays($timers, $timerKey);
39
                    break;
40
            }
41
        }
42
43
        return $this;
44
    }
45
46
    /**
47
     * Generate Datetime objects from config values.
48
     *
49
     * @param array $timers
50
     * @param string $timerKey
51
     */
52
    private function handleTimers(array $timers, string $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
                            }
90
                        }
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 string $timerKey
105
     */
106
    private function handleHolidays(array $timers, string $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 string $timerKey
127
     */
128
    private function handleGeneralHolidays(array $timers, string $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;
148
            }
149
        }
150
151
        $this->set($timerKey, $tmpConf);
152
    }
153
}
154