Lines
95.29%
81 / 85
Functions and Methods
90.00%
9 / 10
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| DayOff | 95.29% | 81 / 85 | 90.00% | 9 / 10 | 30 | 0.00% | 0 / 1 | |||
| readByDepartmentId | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| readOnlyByDepartmentId | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| readCommon | 100.00% | 17 / 17 | 100.00% | 1 / 1 | 8 | |||||
| readByScopeId | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| readByYear | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 4 | |||||
| readCommonByYear | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 4 | |||||
| writeCommonDayoffsByYear | 100.00% | 19 / 19 | 100.00% | 1 / 1 | 5 | |||||
| deleteByTimeInterval | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 2 | |||||
| deleteEntity | 0.00% | 0 / 4 | 0.00% | 0 / 1 | 2 | |||||
| removeCache | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsbackend\Dayoff\Service; | |
| 4 | ||
| 5 | use BO\Zmsbackend\Application as App; | |
| 6 | use BO\Zmsentities\Dayoff as Entity; | |
| 7 | use BO\Zmsentities\Collection\DayoffList as Collection; | |
| 8 | ||
| 9 | class DayOff extends \BO\Zmsbackend\Base | |
| 10 | { | |
| 11 | /** | |
| 12 | * common DayOff like Xmas... | |
| 13 | * | |
| 14 | */ | |
| 15 | public static $commonList = null; | |
| 16 | ||
| 17 | /** | |
| 18 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
| 19 | */ | |
| 20 | public function readByDepartmentId($departmentId = 0) | |
| 21 | { | |
| 22 | return $this->readCommon(); | |
| 23 | } | |
| 24 | ||
| 25 | /** | |
| 26 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
| 27 | */ | |
| 28 | public function readOnlyByDepartmentId($departmentId = 0, bool $disableCache = false) | |
| 29 | { | |
| 30 | return new Collection(); | |
| 31 | } | |
| 32 | ||
| 33 | public function readCommon(bool $disableCache = false) | |
| 34 | { | |
| 35 | $cacheKey = "dayOffs"; | |
| 36 | ||
| 37 | if (!$disableCache && App::$cache) { | |
| 38 | $data = App::$cache->get($cacheKey); | |
| 39 | if (!empty($data)) { | |
| 40 | return $data; | |
| 41 | } | |
| 42 | } | |
| 43 | ||
| 44 | $dayOffList = new Collection(); | |
| 45 | $query = new \BO\Zmsbackend\Dayoff\Repository\DayOff(\BO\Zmsbackend\Query\Base::SELECT); | |
| 46 | $query->addEntityMapping() | |
| 47 | ->addConditionCommon(); | |
| 48 | $result = $this->fetchList($query, new Entity()); | |
| 49 | if (count($result)) { | |
| 50 | foreach ($result as $entity) { | |
| 51 | if ($entity instanceof Entity) { | |
| 52 | $dayOffList->addEntity($entity); | |
| 53 | } | |
| 54 | } | |
| 55 | } | |
| 56 | ||
| 57 | if (App::$cache) { | |
| 58 | App::$cache->set($cacheKey, $dayOffList); | |
| 59 | } | |
| 60 | ||
| 61 | return $dayOffList; | |
| 62 | } | |
| 63 | ||
| 64 | /** | |
| 65 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
| 66 | */ | |
| 67 | public function readByScopeId($scopeId = 0, bool $disableCache = false) | |
| 68 | { | |
| 69 | return $this->readCommon(); | |
| 70 | } | |
| 71 | ||
| 72 | public function readByYear(int $year): Collection | |
| 73 | { | |
| 74 | $dayOffList = new Collection(); | |
| 75 | $query = new \BO\Zmsbackend\Dayoff\Repository\DayOff(\BO\Zmsbackend\Query\Base::SELECT); | |
| 76 | $query | |
| 77 | ->addEntityMapping() | |
| 78 | ->addConditionYear($year); | |
| 79 | $result = $this->fetchList($query, new Entity()); | |
| 80 | if (count($result)) { | |
| 81 | foreach ($result as $entity) { | |
| 82 | if ($entity instanceof Entity) { | |
| 83 | $dayOffList->addEntity($entity); | |
| 84 | } | |
| 85 | } | |
| 86 | } | |
| 87 | return $dayOffList; | |
| 88 | } | |
| 89 | ||
| 90 | public function readCommonByYear(int $year): Collection | |
| 91 | { | |
| 92 | $dayOffList = new Collection(); | |
| 93 | $query = new \BO\Zmsbackend\Dayoff\Repository\DayOff(\BO\Zmsbackend\Query\Base::SELECT); | |
| 94 | $query | |
| 95 | ->addEntityMapping() | |
| 96 | ->addConditionCommon() | |
| 97 | ->addConditionYear($year); | |
| 98 | $result = $this->fetchList($query, new Entity()); | |
| 99 | if (count($result)) { | |
| 100 | foreach ($result as $entity) { | |
| 101 | if ($entity instanceof Entity) { | |
| 102 | $dayOffList->addEntity($entity); | |
| 103 | } | |
| 104 | } | |
| 105 | } | |
| 106 | return $dayOffList; | |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * create dayoff preferences of a department | |
| 111 | * | |
| 112 | * @param | |
| 113 | * dayoffList, | |
| 114 | * year, | |
| 115 | * drop | |
| 116 | * | |
| 117 | * @return \BO\Zmsentities\Collection\DayoffList | |
| 118 | */ | |
| 119 | public function writeCommonDayoffsByYear($dayoffList, ?int $year = null, bool $drop = true) | |
| 120 | { | |
| 121 | if ($drop && $year) { | |
| 122 | static::$commonList = null; | |
| 123 | $deleteQuery = new \BO\Zmsbackend\Dayoff\Repository\DayOff(\BO\Zmsbackend\Query\Base::DELETE); | |
| 124 | $deleteQuery | |
| 125 | ->addConditionYear($year) | |
| 126 | ->addConditionCommon(); | |
| 127 | $this->deleteItem($deleteQuery); | |
| 128 | } | |
| 129 | $query = new \BO\Zmsbackend\Dayoff\Repository\DayOff(\BO\Zmsbackend\Query\Base::INSERT); | |
| 130 | foreach ($dayoffList as $dayoff) { | |
| 131 | $query->addValues( | |
| 132 | [ | |
| 133 | 'behoerdenid' => 0, //all departments | |
| 134 | 'Feiertag' => $dayoff['name'], | |
| 135 | 'Datum' => (new \DateTimeImmutable())->setTimestamp($dayoff['date'])->format('Y-m-d') | |
| 136 | ] | |
| 137 | ); | |
| 138 | $this->writeItem($query); | |
| 139 | } | |
| 140 | ||
| 141 | $this->removeCache(); | |
| 142 | ||
| 143 | return ($year) ? $this->readCommonByYear($year) : $dayoffList; | |
| 144 | } | |
| 145 | ||
| 146 | /** | |
| 147 | * delete dayoff preferences by time interval | |
| 148 | * | |
| 149 | * @param | |
| 150 | * deleteInSeconds | |
| 151 | * | |
| 152 | * @return boolean | |
| 153 | */ | |
| 154 | public function deleteByTimeInterval(int $deleteInSeconds) | |
| 155 | { | |
| 156 | $selectQuery = new \BO\Zmsbackend\Dayoff\Repository\DayOff(\BO\Zmsbackend\Query\Base::SELECT); | |
| 157 | $selectQuery | |
| 158 | ->addEntityMapping() | |
| 159 | ->addConditionDayoffDeleteInterval($deleteInSeconds); | |
| 160 | $statement = $this->fetchStatement($selectQuery); | |
| 161 | while ($dayoffData = $statement->fetch(\PDO::FETCH_ASSOC)) { | |
| 162 | $dayoffData = (new \BO\Zmsbackend\Dayoff\Repository\DayOff(\BO\Zmsbackend\Query\Base::SELECT))->postProcess($dayoffData); | |
| 163 | $entity = new Entity($dayoffData); | |
| 164 | $deleteQuery = new \BO\Zmsbackend\Dayoff\Repository\DayOff(\BO\Zmsbackend\Query\Base::DELETE); | |
| 165 | $date = (new \DateTimeImmutable())->setTimestamp($entity->date)->format('Y-m-d'); | |
| 166 | $deleteQuery | |
| 167 | ->addConditionDate($date) | |
| 168 | ->addConditionName($entity->name); | |
| 169 | $this->deleteItem($deleteQuery); | |
| 170 | } | |
| 171 | ||
| 172 | $this->removeCache(); | |
| 173 | } | |
| 174 | ||
| 175 | public function deleteEntity($itemId): bool | |
| 176 | { | |
| 177 | $query = new \BO\Zmsbackend\Dayoff\Repository\DayOff(\BO\Zmsbackend\Query\Base::DELETE); | |
| 178 | $query->addConditionDayOffId($itemId); | |
| 179 | ||
| 180 | $this->removeCache(); | |
| 181 | ||
| 182 | return ($this->deleteItem($query)); | |
| 183 | } | |
| 184 | ||
| 185 | /** | |
| 186 | * @return void | |
| 187 | */ | |
| 188 | public function removeCache() | |
| 189 | { | |
| 190 | if (!App::$cache) { | |
| 191 | return; | |
| 192 | } | |
| 193 | ||
| 194 | if (App::$cache->has("dayOffs")) { | |
| 195 | App::$cache->delete("dayOffs"); | |
| 196 | } | |
| 197 | } | |
| 198 | } |