ModuleOptions::skipWeekends()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace JhFlexiTime\Options;
4
5
use JhFlexiTime\DateTime\DateTime;
6
use Zend\Stdlib\AbstractOptions;
7
8
/**
9
 * Class ModuleOptions
10
 * @package JhFlexiTime\Options
11
 * @author Aydin Hassan <[email protected]>
12
 */
13
class ModuleOptions extends AbstractOptions
14
{
15
    /**
16
     * @var float
17
     */
18
    protected $hoursInDay = 7.5;
19
20
    /**
21
     * @var bool
22
     */
23
    protected $skipWeekends = true;
24
25
    /**
26
     * @var float
27
     */
28
    protected $lunchDuration = 1;
29
30
    /**
31
     * @var bool
32
     */
33
    protected $cappedCreditEnabled = false;
34
35
    /**
36
     * @var array
37
     */
38
    protected $creditCaps = [];
39
40
    /**
41
     * @param float $hoursInDay
42
     * @return \JhFlexiTime\Options\ModuleOptions
43
     */
44
    public function setHoursInDay($hoursInDay)
45
    {
46
        $this->hoursInDay = $hoursInDay;
47
        return $this;
48
    }
49
50
    /**
51
     * @return float
52
     */
53
    public function getHoursInDay()
54
    {
55
        return $this->hoursInDay;
56
    }
57
58
    /**
59
     * @param float $lunchDuration
60
     * @return \JhFlexiTime\Options\ModuleOptions
61
     */
62
    public function setLunchDuration($lunchDuration)
63
    {
64
        $this->lunchDuration = $lunchDuration;
65
        return $this;
66
    }
67
68
    /**
69
     * @return float
70
     */
71
    public function getLunchDuration()
72
    {
73
        return $this->lunchDuration;
74
    }
75
76
    /**
77
     * @param boolean $skipWeekends
78
     * @return \JhFlexiTime\Options\ModuleOptions
79
     */
80
    public function setSkipWeekends($skipWeekends)
81
    {
82
        $this->skipWeekends = $skipWeekends;
83
        return $this;
84
    }
85
86
    /**
87
     * @return boolean
88
     */
89
    public function getSkipWeekends()
90
    {
91
        return $this->skipWeekends;
92
    }
93
94
    /**
95
     * @return boolean
96
     */
97
    public function skipWeekends()
98
    {
99
        return $this->skipWeekends;
100
    }
101
102
    /**
103
     * @param bool $enabled
104
     */
105
    public function setCreditCapEnabled($enabled)
106
    {
107
        $this->cappedCreditEnabled = $enabled;
108
    }
109
110
    /**
111
     * @return bool
112
     */
113
    public function getCreditCapEnabled()
114
    {
115
        return $this->cappedCreditEnabled;
116
    }
117
118
    /**
119
     * @return bool
120
     */
121
    public function creditCapEnabled()
122
    {
123
        return $this->cappedCreditEnabled;
124
    }
125
126
    /**
127
     * @param array $caps
128
     */
129
    public function setCreditCaps(array $caps)
130
    {
131
        $months = [];
132
        foreach ($caps as $month => $capLimit) {
133
            if (!is_string($month) || !preg_match('/^(0[1-9])|^(1[0-2])-\d{4}$/', $month)) {
134
                throw new \InvalidArgumentException(sprintf('Date should be in the format m-Y. Given: "%s"', $month));
135
            }
136
137
            $months[] = array(
138
                'month' => new DateTime(sprintf('01-%s 00:00:00', $month)),
139
                'limit' => $capLimit
140
            );
141
142
            usort($months, function ($a, $b) {
143
                return $a['month'] > $b['month'] ? 1 : -1;
144
            });
145
        }
146
147
        $this->creditCaps = $months;
148
    }
149
150
    /**
151
     * @return array
152
     */
153
    public function getCreditCaps()
154
    {
155
        return $this->creditCaps;
156
    }
157
158
    /**
159
     * @param DateTime $date
160
     *
161
     * @return float|null
162
     */
163
    public function getCreditCapForDate(DateTime $date)
164
    {
165
        foreach (array_reverse($this->creditCaps) as $index => $creditCap) {
166
            if ($date >= $creditCap['month']) {
167
                return $creditCap['limit'];
168
            }
169
        }
170
        return null;
171
    }
172
}
173