Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
90.00% |
36 / 40 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
CalculateDayOff | |
90.00% |
36 / 40 |
|
80.00% |
4 / 5 |
12.14 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
calculateDayOffByYear | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
3 | |||
calculateEaster | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
writeDayOffListUntilYear | |
60.00% |
6 / 10 |
|
0.00% |
0 / 1 |
6.60 | |||
readDayoffList | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb\Helper; |
4 | |
5 | class CalculateDayOff |
6 | { |
7 | protected $dayOffList = [ |
8 | 'Y-01-01' => 'Neujahr', |
9 | 'Y-03-08' => 'Internationaler Frauentag', |
10 | 'E-2' => 'Karfreitag', |
11 | 'E+0' => 'Ostersonntag', |
12 | 'E+1' => 'Ostermontag', |
13 | 'Y-05-01' => 'Maifeiertag', |
14 | 'E+39' => 'Christi Himmelfahrt', |
15 | 'E+50' => 'Pfingstmontag', |
16 | 'Y-10-03' => 'Tag der Deutschen Einheit', |
17 | #'Y-12-24' => 'Heiligabend', |
18 | 'Y-12-25' => '1. Weihnachtstag', |
19 | 'Y-12-26' => '2. Weihnachtstag', |
20 | #'Y-12-31' => 'Silvester' |
21 | ]; |
22 | |
23 | protected $dateEaster; |
24 | |
25 | protected $dateFormat = 'Y-m-d'; |
26 | |
27 | protected $verbose = false; |
28 | |
29 | protected $targetYear; |
30 | |
31 | public function __construct($targetYear, $verbose = false) |
32 | { |
33 | $this->dateTime = new \DateTime('now', new \DateTimeZone('Europe/Berlin')); |
34 | $this->targetYear = $targetYear; |
35 | $this->verbose = $verbose; |
36 | } |
37 | |
38 | protected function calculateDayOffByYear($year) |
39 | { |
40 | $collection = new \BO\Zmsentities\Collection\DayoffList(); |
41 | $dateEaster = $this->calculateEaster($year); |
42 | foreach ($this->dayOffList as $dateExpr => $description) { |
43 | if (strpos($dateExpr, 'E') === 0) { |
44 | $dateExpr = ltrim($dateExpr, 'E'); |
45 | $dtCurr = clone $dateEaster; |
46 | $date = $dtCurr->modify($dateExpr . ' day')->format($this->dateFormat); |
47 | $entity = new \BO\Zmsentities\Dayoff([ |
48 | 'name' => $description, |
49 | 'date' => (new \DateTimeImmutable($date))->getTimestamp() |
50 | ]); |
51 | } else { |
52 | $date = $dateEaster->format($dateExpr); |
53 | $entity = new \BO\Zmsentities\Dayoff([ |
54 | 'name' => $description, |
55 | 'date' => (new \DateTimeImmutable($date))->getTimestamp() |
56 | ]); |
57 | } |
58 | $collection->addEntity($entity); |
59 | } |
60 | return $collection; |
61 | } |
62 | |
63 | protected function calculateEaster($year) |
64 | { |
65 | $date = clone $this->dateTime; |
66 | if ($year) { |
67 | $date = $date->setDate($year, $date->format('m'), $date->format('d')); |
68 | } |
69 | $easterDate = \easter_date($date->format('Y')); |
70 | |
71 | return $date->setTimestamp($easterDate); |
72 | } |
73 | |
74 | public function writeDayOffListUntilYear($commit = false, $fromnow = false) |
75 | { |
76 | $query = new \BO\Zmsdb\DayOff(); |
77 | $list = $this->readDayoffList($query, $this->targetYear); |
78 | |
79 | if ($fromnow) { |
80 | for ($loopYear = $this->dateTime->format('Y'); $loopYear < $this->targetYear; $loopYear++) { |
81 | $collection = $this->readDayoffList($query, $loopYear); |
82 | $list->addList($collection); |
83 | } |
84 | } |
85 | if ($commit) { |
86 | $query->writeCommonDayoffsByYear($list, null, false); |
87 | } |
88 | if ($this->verbose) { |
89 | return $list->sortByCustomKey('date'); |
90 | } |
91 | } |
92 | |
93 | protected function readDayoffList($query, $year) |
94 | { |
95 | $collection = $query->readCommonByYear($year); |
96 | $newDayOffList = $this->calculateDayOffByYear($year); |
97 | $collection = $collection->withNew($newDayOffList); |
98 | return $collection; |
99 | } |
100 | } |