Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.69% |
169 / 173 |
|
69.23% |
9 / 13 |
CRAP | |
0.00% |
0 / 1 |
| Department | |
97.69% |
169 / 173 |
|
69.23% |
9 / 13 |
59 | |
0.00% |
0 / 1 |
| readEntity | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
9 | |||
| readResolvedReferences | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
3.00 | |||
| readList | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
| readEntitiesByIds | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
5.01 | |||
| readByScopeId | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| readByOrganisationId | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
| deleteEntity | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
7 | |||
| writeEntity | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
3 | |||
| updateEntity | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
3 | |||
| writeDepartmentLinks | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
5.02 | |||
| updateDepartmentMail | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| readQueueList | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| removeCache | |
94.44% |
17 / 18 |
|
0.00% |
0 / 1 |
9.01 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BO\Zmsbackend\Department\Service; |
| 4 | |
| 5 | use BO\Zmsentities\Department as Entity; |
| 6 | use BO\Zmsentities\Collection\DepartmentList as Collection; |
| 7 | |
| 8 | /** |
| 9 | * @SuppressWarnings(Coupling) |
| 10 | * @SuppressWarnings(Complexity) |
| 11 | * |
| 12 | */ |
| 13 | class Department extends \BO\Zmsbackend\Base |
| 14 | { |
| 15 | public static $departmentCache = array(); |
| 16 | |
| 17 | /** |
| 18 | * @param false|string $departmentId |
| 19 | * |
| 20 | */ |
| 21 | public function readEntity(string|false $departmentId, int $resolveReferences = 0, bool $disableCache = false) |
| 22 | { |
| 23 | $cacheKey = "department-$departmentId-$resolveReferences"; |
| 24 | |
| 25 | if (!$disableCache && \App::$cache && \App::$cache->has($cacheKey)) { |
| 26 | $department = \App::$cache->get($cacheKey); |
| 27 | } |
| 28 | |
| 29 | if (empty($department)) { |
| 30 | $query = new \BO\Zmsbackend\Department\Repository\Department(\BO\Zmsbackend\Query\Base::SELECT); |
| 31 | $query->addEntityMapping() |
| 32 | ->addResolvedReferences($resolveReferences) |
| 33 | ->addConditionDepartmentId($departmentId); |
| 34 | $department = $this->fetchOne($query, new Entity()); |
| 35 | |
| 36 | if (\App::$cache) { |
| 37 | \App::$cache->set($cacheKey, $department); |
| 38 | if (\App::$log) { |
| 39 | \App::$log->info('Department cache set', ['cache_key' => $cacheKey]); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | if (isset($department['id']) && $department['id']) { |
| 45 | $department = $this->readResolvedReferences($department, $resolveReferences, $disableCache); |
| 46 | return $department->withOutClusterDuplicates(); |
| 47 | } |
| 48 | |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @return Entity |
| 54 | */ |
| 55 | #[\Override] |
| 56 | public function readResolvedReferences( |
| 57 | \BO\Zmsentities\Schema\Entity $entity, |
| 58 | $resolveReferences, |
| 59 | $disableCache = false |
| 60 | ): Entity { |
| 61 | if (!$entity instanceof Entity) { |
| 62 | throw new \InvalidArgumentException('Expected ' . Entity::class); |
| 63 | } |
| 64 | $entity['links'] = (new \BO\Zmsbackend\Link\Service\Link())->readByDepartmentId($entity->id, $disableCache); |
| 65 | $entity['scopes'] = (new \BO\Zmsbackend\Scope\Service\Scope()) |
| 66 | ->readByDepartmentId($entity->id, $resolveReferences - 1, $disableCache) |
| 67 | ->sortByContactName(); |
| 68 | if (0 < $resolveReferences) { |
| 69 | $entity['clusters'] = (new \BO\Zmsbackend\Cluster\Service\Cluster())->readByDepartmentId( |
| 70 | $entity->id, |
| 71 | $resolveReferences - 1, |
| 72 | $disableCache |
| 73 | ); |
| 74 | $entity['dayoff'] = (new \BO\Zmsbackend\Dayoff\Service\DayOff())->readOnlyByDepartmentId($entity->id, $disableCache); |
| 75 | } |
| 76 | return $entity; |
| 77 | } |
| 78 | |
| 79 | public function readList($resolveReferences = 0): Collection |
| 80 | { |
| 81 | $departmentList = new Collection(); |
| 82 | $query = new \BO\Zmsbackend\Department\Repository\Department(\BO\Zmsbackend\Query\Base::SELECT); |
| 83 | $query->addEntityMapping(); |
| 84 | $query->addResolvedReferences($resolveReferences); |
| 85 | $result = $this->fetchList($query, new Entity()); |
| 86 | if (count($result)) { |
| 87 | foreach ($result as $department) { |
| 88 | $department = $this->readResolvedReferences($department, $resolveReferences); |
| 89 | if ($department instanceof Entity) { |
| 90 | $departmentList->addEntity($department->withOutClusterDuplicates()); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | return $departmentList; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * |
| 99 | * @return Entity[] |
| 100 | * |
| 101 | */ |
| 102 | public function readEntitiesByIds(array $departmentIds, int $resolveReferences = 0): array |
| 103 | { |
| 104 | if (empty($departmentIds)) { |
| 105 | return []; |
| 106 | } |
| 107 | |
| 108 | $query = new \BO\Zmsbackend\Department\Repository\Department(\BO\Zmsbackend\Query\Base::SELECT); |
| 109 | $query->addEntityMapping() |
| 110 | ->addResolvedReferences($resolveReferences) |
| 111 | ->addConditionDepartmentIds($departmentIds); |
| 112 | |
| 113 | $departments = []; |
| 114 | $result = $this->fetchList($query, new Entity()); |
| 115 | |
| 116 | foreach ($result as $department) { |
| 117 | if ($department instanceof Entity) { |
| 118 | $department = $this->readResolvedReferences($department, $resolveReferences); |
| 119 | if ($department instanceof Entity) { |
| 120 | $departments[$department->id] = $department->withOutClusterDuplicates(); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return $departments; |
| 126 | } |
| 127 | |
| 128 | public function readByScopeId($scopeId, int $resolveReferences = 0) |
| 129 | { |
| 130 | $query = new \BO\Zmsbackend\Department\Repository\Department(\BO\Zmsbackend\Query\Base::SELECT); |
| 131 | $query->addEntityMapping() |
| 132 | ->addResolvedReferences($resolveReferences) |
| 133 | ->addConditionScopeId($scopeId); |
| 134 | $department = $this->fetchOne($query, new Entity()); |
| 135 | $department = $this->readResolvedReferences($department, $resolveReferences); |
| 136 | return (isset($department['id']) && $department['id']) ? $department->withOutClusterDuplicates() : null; |
| 137 | } |
| 138 | |
| 139 | public function readByOrganisationId($organisationId, int $resolveReferences = 0): Collection |
| 140 | { |
| 141 | $departmentList = new Collection(); |
| 142 | $query = new \BO\Zmsbackend\Department\Repository\Department(\BO\Zmsbackend\Query\Base::SELECT); |
| 143 | $query |
| 144 | ->addEntityMapping() |
| 145 | ->addResolvedReferences($resolveReferences) |
| 146 | ->addConditionOrganisationId($organisationId); |
| 147 | $result = $this->fetchList($query, new Entity()); |
| 148 | if (count($result)) { |
| 149 | foreach ($result as $department) { |
| 150 | if ($department instanceof Entity) { |
| 151 | $department = $this->readResolvedReferences($department, $resolveReferences); |
| 152 | $departmentList->addEntity($department->withOutClusterDuplicates()); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | return $departmentList; |
| 157 | } |
| 158 | |
| 159 | public function deleteEntity($departmentId) |
| 160 | { |
| 161 | $entity = $this->readEntity($departmentId, 1); |
| 162 | if ($entity) { |
| 163 | if ( |
| 164 | 0 < $entity->toProperty()->scopes->get()->count() |
| 165 | || 0 < $entity->toProperty()->clusters->get()->count() |
| 166 | ) { |
| 167 | throw new \BO\Zmsbackend\Department\Exception\ScopeListNotEmpty(); |
| 168 | } |
| 169 | |
| 170 | self::$departmentCache = []; |
| 171 | $query = new \BO\Zmsbackend\Department\Repository\Department(\BO\Zmsbackend\Query\Base::DELETE); |
| 172 | $query->addConditionDepartmentId($departmentId); |
| 173 | $entityDelete = $this->deleteItem($query); |
| 174 | $emailDelete = $this->perform(\BO\Zmsbackend\Department\Repository\Department::QUERY_MAIL_DELETE, array( |
| 175 | $departmentId |
| 176 | )); |
| 177 | } |
| 178 | |
| 179 | $this->removeCache($entity); |
| 180 | |
| 181 | return ($entity && $entityDelete && $emailDelete) ? $entity : null; |
| 182 | } |
| 183 | |
| 184 | public function writeEntity(\BO\Zmsentities\Department $entity, $parentId) |
| 185 | { |
| 186 | self::$departmentCache = []; |
| 187 | $query = new \BO\Zmsbackend\Department\Repository\Department(\BO\Zmsbackend\Query\Base::INSERT); |
| 188 | $values = $query->reverseEntityMapping($entity, $parentId); |
| 189 | // get owner by organisation |
| 190 | $owner = (new \BO\Zmsbackend\Owner\Service\Owner())->readByOrganisationId($parentId); |
| 191 | $values['KundenID'] = $owner->id; |
| 192 | $query->addValues($values); |
| 193 | $this->writeItem($query); |
| 194 | $lastInsertId = $this->getWriter() |
| 195 | ->lastInsertId(); |
| 196 | if ($entity->toProperty()->links->isAvailable()) { |
| 197 | $this->writeDepartmentLinks($lastInsertId, $entity->links); |
| 198 | } |
| 199 | if ($entity->toProperty()->email->isAvailable()) { |
| 200 | $this->updateDepartmentMail( |
| 201 | $lastInsertId, |
| 202 | $entity->email, |
| 203 | $entity->sendEmailReminderEnabled, |
| 204 | $entity->sendEmailReminderMinutesBefore |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | $this->removeCache($entity); |
| 209 | |
| 210 | return $this->readEntity($lastInsertId); |
| 211 | } |
| 212 | |
| 213 | public function updateEntity($departmentId, \BO\Zmsentities\Department $entity) |
| 214 | { |
| 215 | self::$departmentCache = []; |
| 216 | $query = new \BO\Zmsbackend\Department\Repository\Department(\BO\Zmsbackend\Query\Base::UPDATE); |
| 217 | $query->addConditionDepartmentId($departmentId); |
| 218 | $values = $query->reverseEntityMapping($entity); |
| 219 | $query->addValues($values); |
| 220 | $this->writeItem($query); |
| 221 | if ($entity->toProperty()->links->isAvailable()) { |
| 222 | $this->writeDepartmentLinks($departmentId, $entity->links); |
| 223 | } |
| 224 | if ($entity->toProperty()->email->isAvailable()) { |
| 225 | $this->updateDepartmentMail( |
| 226 | $departmentId, |
| 227 | $entity->email, |
| 228 | $entity->sendEmailReminderEnabled, |
| 229 | $entity->sendEmailReminderMinutesBefore |
| 230 | ); |
| 231 | } |
| 232 | $this->removeCache($entity); |
| 233 | return $this->readEntity($departmentId, 0, true); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | /** |
| 238 | * @param false|string $departmentId |
| 239 | * |
| 240 | * @return void |
| 241 | */ |
| 242 | protected function writeDepartmentLinks(string|false $departmentId, $links) |
| 243 | { |
| 244 | if (!$departmentId) { |
| 245 | throw new \BO\Zmsbackend\Department\Exception\InvalidId(); |
| 246 | } |
| 247 | $existingLinks = (new \BO\Zmsbackend\Link\Service\Link())->readByDepartmentId($departmentId); |
| 248 | if ($existingLinks->count()) { |
| 249 | foreach ($existingLinks as $item) { |
| 250 | $query = new \BO\Zmsbackend\Link\Service\Link(); |
| 251 | $query->deleteEntity($item->getId()); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | foreach ($links as $link) { |
| 256 | $link = new \BO\Zmsentities\Link($link); |
| 257 | $query = new \BO\Zmsbackend\Link\Service\Link(); |
| 258 | $query->writeEntity($link, $departmentId); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * @param false|string $departmentId |
| 264 | */ |
| 265 | protected function updateDepartmentMail( |
| 266 | string|false $departmentId, |
| 267 | $email, |
| 268 | $sendEmailReminderEnabled, |
| 269 | $sendEmailReminderMinutesBefore |
| 270 | ) { |
| 271 | self::$departmentCache = []; |
| 272 | $query = \BO\Zmsbackend\Department\Repository\Department::QUERY_MAIL_UPDATE; |
| 273 | return $this->fetchAffected($query, array( |
| 274 | 'email' => $email, |
| 275 | 'departmentId' => $departmentId, |
| 276 | 'sendEmailReminderEnabled' => $sendEmailReminderEnabled, |
| 277 | 'sendEmailReminderMinutesBefore' => $sendEmailReminderMinutesBefore |
| 278 | )); |
| 279 | } |
| 280 | |
| 281 | public function readQueueList( |
| 282 | $departmentId, |
| 283 | \DateTimeInterface $dateTime, |
| 284 | $resolveReferences = 0 |
| 285 | ): \BO\Zmsentities\Collection\QueueList { |
| 286 | $queueList = new \BO\Zmsentities\Collection\QueueList(); |
| 287 | $department = $this->readEntity($departmentId, 2); |
| 288 | |
| 289 | foreach ($department->getScopeList() as $scope) { |
| 290 | $scope = (new \BO\Zmsbackend\Scope\Service\Scope())->readWithWorkstationCount($scope->id, $dateTime); |
| 291 | $scopeQueueList = (new \BO\Zmsbackend\Scope\Service\Scope()) |
| 292 | ->readQueueListWithWaitingTime($scope, $dateTime, $resolveReferences); |
| 293 | if (0 < $scopeQueueList->count()) { |
| 294 | $queueList->addList($scopeQueueList); |
| 295 | } |
| 296 | } |
| 297 | return $queueList; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * @return void |
| 302 | */ |
| 303 | public function removeCache(Entity $department) |
| 304 | { |
| 305 | if (!\App::$cache || !isset($department->id)) { |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | $invalidatedKeys = []; |
| 310 | |
| 311 | // Invalidate department entity cache for all resolveReferences levels (0, 1, 2) |
| 312 | for ($resolveReferences = 0; $resolveReferences <= 2; $resolveReferences++) { |
| 313 | $key = "department-{$department->id}-{$resolveReferences}"; |
| 314 | if (\App::$cache->has($key)) { |
| 315 | \App::$cache->delete($key); |
| 316 | $invalidatedKeys[] = $key; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // Invalidate scopeReadByDepartmentId cache for all resolveReferences levels (0, 1, 2) |
| 321 | for ($resolveReferences = 0; $resolveReferences <= 2; $resolveReferences++) { |
| 322 | $key = "scopeReadByDepartmentId-{$department->id}-{$resolveReferences}"; |
| 323 | if (\App::$cache->has($key)) { |
| 324 | \App::$cache->delete($key); |
| 325 | $invalidatedKeys[] = $key; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | if (!empty($invalidatedKeys) && \App::$log) { |
| 330 | \App::$log->info('Department cache invalidated', [ |
| 331 | 'department_id' => $department->id, |
| 332 | 'invalidated_keys' => $invalidatedKeys |
| 333 | ]); |
| 334 | } |
| 335 | } |
| 336 | } |