Lines
47.45%
28 / 59
Functions and Methods
16.66%
1 / 6
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Closure | 47.45% | 28 / 59 | 16.66% | 1 / 6 | 53.13 | 0.00% | 0 / 1 | |||
| readByScopeId | 82.35% | 14 / 17 | 0.00% | 0 / 1 | 8.35 | |||||
| readByScopeIdAndDate | 0.00% | 0 / 5 | 0.00% | 0 / 1 | 2 | |||||
| readByScopesInRange | 100.00% | 14 / 14 | 100.00% | 1 / 1 | 2 | |||||
| deleteEntity | 0.00% | 0 / 5 | 0.00% | 0 / 1 | 6 | |||||
| createOne | 0.00% | 0 / 14 | 0.00% | 0 / 1 | 6 | |||||
| readEntity | 0.00% | 0 / 4 | 0.00% | 0 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsbackend\Availability\Service; | |
| 4 | ||
| 5 | use BO\Zmsbackend\Application as App; | |
| 6 | use BO\Zmsentities\Closure as Entity; | |
| 7 | use BO\Zmsentities\Collection\ClosureList as Collection; | |
| 8 | use DateTime; | |
| 9 | ||
| 10 | class Closure extends \BO\Zmsbackend\Base | |
| 11 | { | |
| 12 | public function readByScopeId($scopeId = 0, bool $disableCache = false) | |
| 13 | { | |
| 14 | $cacheKey = "closuresByScope-$scopeId"; | |
| 15 | ||
| 16 | if (!$disableCache && App::$cache) { | |
| 17 | $data = App::$cache->get($cacheKey); | |
| 18 | if (!empty($data)) { | |
| 19 | return $data; | |
| 20 | } | |
| 21 | } | |
| 22 | ||
| 23 | $closureList = new Collection(); | |
| 24 | $query = new \BO\Zmsbackend\Availability\Repository\Closure(\BO\Zmsbackend\Query\Base::SELECT); | |
| 25 | $query->addEntityMapping() | |
| 26 | ->addConditionScopeId($scopeId); | |
| 27 | $result = $this->fetchList($query, new Entity()); | |
| 28 | if (count($result)) { | |
| 29 | foreach ($result as $entity) { | |
| 30 | if ($entity instanceof Entity) { | |
| 31 | $closureList->addEntity($entity); | |
| 32 | } | |
| 33 | } | |
| 34 | } | |
| 35 | ||
| 36 | if (App::$cache) { | |
| 37 | App::$cache->set($cacheKey, $closureList); | |
| 38 | } | |
| 39 | ||
| 40 | return $closureList; | |
| 41 | } | |
| 42 | ||
| 43 | public function readByScopeIdAndDate($scopeId, DateTime $date): Entity | |
| 44 | { | |
| 45 | $query = new \BO\Zmsbackend\Availability\Repository\Closure(\BO\Zmsbackend\Query\Base::SELECT); | |
| 46 | $query->addEntityMapping() | |
| 47 | ->addConditionScopeId($scopeId) | |
| 48 | ->addConditionDate($date); | |
| 49 | ||
| 50 | return $this->fetchOne($query, new \BO\Zmsentities\Closure()); | |
| 51 | } | |
| 52 | ||
| 53 | public function readByScopesInRange( | |
| 54 | array $scopeIds, | |
| 55 | \DateTimeInterface $from, | |
| 56 | \DateTimeInterface $until | |
| 57 | ): array { | |
| 58 | $query = (new \BO\Zmsbackend\Availability\Repository\Closure(\BO\Zmsbackend\Query\Base::SELECT)) | |
| 59 | ->addEntityMapping() | |
| 60 | ->addSelectVirtualDate() | |
| 61 | ->addConditionScopeIds($scopeIds) | |
| 62 | ->addConditionDateRange($from, $until); | |
| 63 | ||
| 64 | $entities = $this->fetchList($query, new Entity()); | |
| 65 | ||
| 66 | $result = []; | |
| 67 | foreach ($entities as $entity) { | |
| 68 | $date = $entity->date ?? $entity->getDateTime()->format('Y-m-d'); | |
| 69 | $result [] = [ | |
| 70 | 'scopeId' => (int) $entity->scopeId, | |
| 71 | 'date' => (string) $date, | |
| 72 | ]; | |
| 73 | } | |
| 74 | return $result ; | |
| 75 | } | |
| 76 | ||
| 77 | public function deleteEntity($closure): bool | |
| 78 | { | |
| 79 | $query = new \BO\Zmsbackend\Availability\Repository\Closure(\BO\Zmsbackend\Query\Base::DELETE); | |
| 80 | $query->addConditionId($closure->getId()); | |
| 81 | ||
| 82 | if (App::$cache) { | |
| 83 | App::$cache->delete('closuresByScope-' . $closure->scopeId); | |
| 84 | } | |
| 85 | ||
| 86 | return ($this->deleteItem($query)); | |
| 87 | } | |
| 88 | ||
| 89 | public function createOne($scopeId, DateTime $date) | |
| 90 | { | |
| 91 | $query = new \BO\Zmsbackend\Availability\Repository\Closure(\BO\Zmsbackend\Query\Base::INSERT); | |
| 92 | $query->addValues( | |
| 93 | array( | |
| 94 | 'StandortID' => $scopeId, | |
| 95 | 'year' => (int) $date->format('Y'), | |
| 96 | 'month' => (int) $date->format('m'), | |
| 97 | 'day' => (int) $date->format('d') | |
| 98 | ) | |
| 99 | ); | |
| 100 | ||
| 101 | if (App::$cache) { | |
| 102 | App::$cache->delete('closuresByScope-' . $scopeId); | |
| 103 | } | |
| 104 | ||
| 105 | $this->writeItem($query); | |
| 106 | $id = $this->getWriter()->lastInsertId(); | |
| 107 | ||
| 108 | return $this->readEntity($id); | |
| 109 | } | |
| 110 | ||
| 111 | public function readEntity(string|false $id): Entity | |
| 112 | { | |
| 113 | $query = new \BO\Zmsbackend\Availability\Repository\Closure(\BO\Zmsbackend\Query\Base::SELECT); | |
| 114 | $query->addEntityMapping() | |
| 115 | ->addConditionId($id); | |
| 116 | ||
| 117 | return $this->fetchOne($query, new \BO\Zmsentities\Closure()); | |
| 118 | } | |
| 119 | } |