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