Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
94 / 94 |
|
100.00% |
13 / 13 |
CRAP | |
100.00% |
1 / 1 |
| Organisation | |
100.00% |
94 / 94 |
|
100.00% |
13 / 13 |
29 | |
100.00% |
1 / 1 |
| readEntity | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| readResolvedReferences | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| readByScopeId | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| readByDepartmentId | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| readByClusterId | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| readByOwnerId | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
| readByHash | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| readList | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| deleteEntity | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| writeEntity | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| updateEntity | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| writeOrganisationTicketprinters | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| updateOrganisationTicketprinters | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BO\Zmsbackend\Organisation\Service; |
| 4 | |
| 5 | use BO\Zmsentities\Organisation as Entity; |
| 6 | use BO\Zmsentities\Collection\OrganisationList as Collection; |
| 7 | |
| 8 | /** |
| 9 | * |
| 10 | * @SuppressWarnings(Public) |
| 11 | * |
| 12 | */ |
| 13 | class Organisation extends \BO\Zmsbackend\Base |
| 14 | { |
| 15 | /** |
| 16 | * @param false|int|string $itemId |
| 17 | * |
| 18 | */ |
| 19 | public function readEntity(string|int|false $itemId, int $resolveReferences = 0) |
| 20 | { |
| 21 | $query = new \BO\Zmsbackend\Organisation\Repository\Organisation(\BO\Zmsbackend\Query\Base::SELECT); |
| 22 | $query->addEntityMapping() |
| 23 | ->addResolvedReferences($resolveReferences) |
| 24 | ->addConditionOrganisationId($itemId); |
| 25 | $organisation = $this->fetchOne($query, new Entity()); |
| 26 | if (isset($organisation['id'])) { |
| 27 | return $this->readResolvedReferences($organisation, $resolveReferences); |
| 28 | } |
| 29 | return array(); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @return \BO\Zmsentities\Schema\Entity |
| 34 | */ |
| 35 | #[\Override] |
| 36 | public function readResolvedReferences(\BO\Zmsentities\Schema\Entity $entity, $resolveReferences) |
| 37 | { |
| 38 | if (0 < $resolveReferences && $entity->hasId()) { |
| 39 | $entity['departments'] = (new \BO\Zmsbackend\Department\Service\Department()) |
| 40 | ->readByOrganisationId($entity->id, $resolveReferences - 1); |
| 41 | $entity['ticketprinters'] = (new \BO\Zmsbackend\Ticketprinter\Service\Ticketprinter()) |
| 42 | ->readByOrganisationId($entity->id, $resolveReferences - 1); |
| 43 | } |
| 44 | return $entity; |
| 45 | } |
| 46 | |
| 47 | public function readByScopeId($scopeId, $resolveReferences = 0) |
| 48 | { |
| 49 | $query = new \BO\Zmsbackend\Organisation\Repository\Organisation(\BO\Zmsbackend\Query\Base::SELECT); |
| 50 | $query->addEntityMapping() |
| 51 | ->addResolvedReferences($resolveReferences) |
| 52 | ->addConditionScopeId($scopeId); |
| 53 | $organisation = $this->fetchOne($query, new Entity()); |
| 54 | return $this->readResolvedReferences($organisation, $resolveReferences); |
| 55 | } |
| 56 | |
| 57 | public function readByDepartmentId($departmentId, int $resolveReferences = 0) |
| 58 | { |
| 59 | $query = new \BO\Zmsbackend\Organisation\Repository\Organisation(\BO\Zmsbackend\Query\Base::SELECT); |
| 60 | $query->addEntityMapping() |
| 61 | ->addResolvedReferences($resolveReferences) |
| 62 | ->addConditionDepartmentId($departmentId); |
| 63 | $organisation = $this->fetchOne($query, new Entity()); |
| 64 | return $this->readResolvedReferences($organisation, $resolveReferences); |
| 65 | } |
| 66 | |
| 67 | public function readByClusterId($clusterId, $resolveReferences = 0) |
| 68 | { |
| 69 | $scope = (new \BO\Zmsbackend\Scope\Service\Scope())->readByClusterId($clusterId, $resolveReferences)->getFirst(); |
| 70 | if (! $scope) { |
| 71 | throw new \BO\Zmsbackend\Cluster\Exception\ClusterWithoutScopes(); |
| 72 | } |
| 73 | return $this->readByScopeId($scope->id, $resolveReferences); |
| 74 | } |
| 75 | |
| 76 | public function readByOwnerId($ownerId, $resolveReferences = 0): Collection |
| 77 | { |
| 78 | $organisationList = new Collection(); |
| 79 | $query = new \BO\Zmsbackend\Organisation\Repository\Organisation(\BO\Zmsbackend\Query\Base::SELECT); |
| 80 | $query->addEntityMapping() |
| 81 | ->addResolvedReferences($resolveReferences) |
| 82 | ->addConditionOwnerId($ownerId); |
| 83 | $result = $this->fetchList($query, new Entity()); |
| 84 | if (count($result)) { |
| 85 | foreach ($result as $organisation) { |
| 86 | $entity = $this->readResolvedReferences($organisation, $resolveReferences); |
| 87 | if ($entity instanceof Entity) { |
| 88 | $organisationList->addEntity($entity); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | return $organisationList; |
| 93 | } |
| 94 | |
| 95 | public function readByHash($hash) |
| 96 | { |
| 97 | $organisationId = $this->getReader() |
| 98 | ->fetchValue((new \BO\Zmsbackend\Ticketprinter\Repository\Ticketprinter(\BO\Zmsbackend\Query\Base::SELECT)) |
| 99 | ->getOrganisationIdByHash(), ['hash' => $hash]); |
| 100 | |
| 101 | return $this->readEntity($organisationId); |
| 102 | } |
| 103 | |
| 104 | public function readList($resolveReferences = 0): Collection |
| 105 | { |
| 106 | $organisationList = new Collection(); |
| 107 | $query = new \BO\Zmsbackend\Organisation\Repository\Organisation(\BO\Zmsbackend\Query\Base::SELECT); |
| 108 | $query->addEntityMapping() |
| 109 | ->addResolvedReferences($resolveReferences); |
| 110 | $result = $this->fetchList($query, new Entity()); |
| 111 | if (count($result)) { |
| 112 | foreach ($result as $organisation) { |
| 113 | $entity = new Entity($organisation); |
| 114 | $entity = $this->readResolvedReferences($entity, $resolveReferences); |
| 115 | $organisationList->addEntity($entity); |
| 116 | } |
| 117 | } |
| 118 | return $organisationList; |
| 119 | } |
| 120 | |
| 121 | public function deleteEntity($itemId) |
| 122 | { |
| 123 | $entity = $this->readEntity($itemId, 1); |
| 124 | if (0 < $entity->toProperty()->departments->get()->count()) { |
| 125 | throw new \BO\Zmsbackend\Organisation\Exception\DepartmentListNotEmpty(); |
| 126 | } |
| 127 | $query = new \BO\Zmsbackend\Organisation\Repository\Organisation(\BO\Zmsbackend\Query\Base::DELETE); |
| 128 | $query->addConditionOrganisationId($itemId); |
| 129 | return ($this->deleteItem($query)) ? $entity : null; |
| 130 | } |
| 131 | |
| 132 | public function writeEntity(\BO\Zmsentities\Organisation $entity, $parentId) |
| 133 | { |
| 134 | $query = new \BO\Zmsbackend\Organisation\Repository\Organisation(\BO\Zmsbackend\Query\Base::INSERT); |
| 135 | $values = $query->reverseEntityMapping($entity, $parentId); |
| 136 | $query->addValues($values); |
| 137 | $this->writeItem($query); |
| 138 | $lastInsertId = $this->getWriter() |
| 139 | ->lastInsertId(); |
| 140 | if ($entity->toProperty()->ticketprinters->isAvailable()) { |
| 141 | $this->writeOrganisationTicketprinters($lastInsertId, $entity->ticketprinters); |
| 142 | } |
| 143 | return $this->readEntity($lastInsertId); |
| 144 | } |
| 145 | |
| 146 | public function updateEntity($organisationId, \BO\Zmsentities\Organisation $entity) |
| 147 | { |
| 148 | $query = new \BO\Zmsbackend\Organisation\Repository\Organisation(\BO\Zmsbackend\Query\Base::UPDATE); |
| 149 | $query->addConditionOrganisationId($organisationId); |
| 150 | $values = $query->reverseEntityMapping($entity); |
| 151 | $query->addValues($values); |
| 152 | $this->writeItem($query); |
| 153 | if ($entity->toProperty()->ticketprinters->isAvailable()) { |
| 154 | $this->updateOrganisationTicketprinters($entity->ticketprinters, $organisationId); |
| 155 | } |
| 156 | return $this->readEntity($organisationId, 1, true); |
| 157 | } |
| 158 | |
| 159 | protected function writeOrganisationTicketprinters(string|false $organisationId, $ticketprinterList): void |
| 160 | { |
| 161 | $deleteQuery = new \BO\Zmsbackend\Ticketprinter\Repository\Ticketprinter(\BO\Zmsbackend\Query\Base::DELETE); |
| 162 | $deleteQuery->addConditionOrganisationId($organisationId); |
| 163 | $this->deleteItem($deleteQuery); |
| 164 | foreach ($ticketprinterList as $ticketprinter) { |
| 165 | $ticketprinter['enabled'] = (isset($ticketprinter['enabled']) && $ticketprinter['enabled']); |
| 166 | $ticketprinter = new \BO\Zmsentities\Ticketprinter($ticketprinter); |
| 167 | $query = new \BO\Zmsbackend\Ticketprinter\Service\Ticketprinter(); |
| 168 | $query->writeEntity($ticketprinter, $organisationId); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | protected function updateOrganisationTicketprinters($ticketprinterList, $organisationId): void |
| 173 | { |
| 174 | foreach ($ticketprinterList as $item) { |
| 175 | $query = new \BO\Zmsbackend\Ticketprinter\Repository\Ticketprinter(\BO\Zmsbackend\Query\Base::UPDATE); |
| 176 | $entity = new \BO\Zmsentities\Ticketprinter($item); |
| 177 | $query->addConditionHash($entity->getId()); |
| 178 | $query->addValues($query->reverseEntityMapping($entity, $organisationId)); |
| 179 | $this->writeItem($query); |
| 180 | } |
| 181 | } |
| 182 | } |