Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
95 / 95 |
|
100.00% |
13 / 13 |
CRAP | |
100.00% |
1 / 1 |
| Organisation | |
100.00% |
95 / 95 |
|
100.00% |
13 / 13 |
30 | |
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% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
| 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\Zmsdb; |
| 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 Base |
| 14 | { |
| 15 | public function readEntity($itemId, $resolveReferences = 0) |
| 16 | { |
| 17 | $query = new Query\Organisation(Query\Base::SELECT); |
| 18 | $query->addEntityMapping() |
| 19 | ->addResolvedReferences($resolveReferences) |
| 20 | ->addConditionOrganisationId($itemId); |
| 21 | $organisation = $this->fetchOne($query, new Entity()); |
| 22 | if (isset($organisation['id'])) { |
| 23 | return $this->readResolvedReferences($organisation, $resolveReferences); |
| 24 | } |
| 25 | return array(); |
| 26 | } |
| 27 | |
| 28 | public function readResolvedReferences(\BO\Zmsentities\Schema\Entity $entity, $resolveReferences) |
| 29 | { |
| 30 | if (0 < $resolveReferences && $entity->hasId()) { |
| 31 | //error_log("Organisation Level $resolveReferences"); |
| 32 | $entity['departments'] = (new Department()) |
| 33 | ->readByOrganisationId($entity->id, $resolveReferences - 1); |
| 34 | $entity['ticketprinters'] = (new Ticketprinter()) |
| 35 | ->readByOrganisationId($entity->id, $resolveReferences - 1); |
| 36 | } |
| 37 | return $entity; |
| 38 | } |
| 39 | |
| 40 | public function readByScopeId($scopeId, $resolveReferences = 0) |
| 41 | { |
| 42 | $query = new Query\Organisation(Query\Base::SELECT); |
| 43 | $query->addEntityMapping() |
| 44 | ->addResolvedReferences($resolveReferences) |
| 45 | ->addConditionScopeId($scopeId); |
| 46 | $organisation = $this->fetchOne($query, new Entity()); |
| 47 | return $this->readResolvedReferences($organisation, $resolveReferences); |
| 48 | } |
| 49 | |
| 50 | public function readByDepartmentId($departmentId, $resolveReferences = 0) |
| 51 | { |
| 52 | $query = new Query\Organisation(Query\Base::SELECT); |
| 53 | $query->addEntityMapping() |
| 54 | ->addResolvedReferences($resolveReferences) |
| 55 | ->addConditionDepartmentId($departmentId); |
| 56 | $organisation = $this->fetchOne($query, new Entity()); |
| 57 | return $this->readResolvedReferences($organisation, $resolveReferences); |
| 58 | } |
| 59 | |
| 60 | public function readByClusterId($clusterId, $resolveReferences = 0) |
| 61 | { |
| 62 | $scope = (new Scope())->readByClusterId($clusterId, $resolveReferences)->getFirst(); |
| 63 | if (! $scope) { |
| 64 | throw new Exception\ClusterWithoutScopes(); |
| 65 | } |
| 66 | return $this->readByScopeId($scope->id, $resolveReferences); |
| 67 | } |
| 68 | |
| 69 | public function readByOwnerId($ownerId, $resolveReferences = 0) |
| 70 | { |
| 71 | $organisationList = new Collection(); |
| 72 | $query = new Query\Organisation(Query\Base::SELECT); |
| 73 | $query->addEntityMapping() |
| 74 | ->addResolvedReferences($resolveReferences) |
| 75 | ->addConditionOwnerId($ownerId); |
| 76 | $result = $this->fetchList($query, new Entity()); |
| 77 | if (count($result)) { |
| 78 | foreach ($result as $organisation) { |
| 79 | $entity = $this->readResolvedReferences($organisation, $resolveReferences); |
| 80 | if ($entity instanceof Entity) { |
| 81 | $organisationList->addEntity($entity); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | return $organisationList; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * read Organisation by Ticketprinter Hash |
| 90 | * |
| 91 | * @param |
| 92 | * hash |
| 93 | * |
| 94 | * @return Resource Entity |
| 95 | */ |
| 96 | public function readByHash($hash) |
| 97 | { |
| 98 | $organisationId = $this->getReader() |
| 99 | ->fetchValue((new Query\Ticketprinter(Query\Base::SELECT)) |
| 100 | ->getOrganisationIdByHash(), ['hash' => $hash]); |
| 101 | |
| 102 | return $this->readEntity($organisationId); |
| 103 | } |
| 104 | |
| 105 | public function readList($resolveReferences = 0) |
| 106 | { |
| 107 | $organisationList = new Collection(); |
| 108 | $query = new Query\Organisation(Query\Base::SELECT); |
| 109 | $query->addEntityMapping() |
| 110 | ->addResolvedReferences($resolveReferences); |
| 111 | $result = $this->fetchList($query, new Entity()); |
| 112 | if (count($result)) { |
| 113 | foreach ($result as $organisation) { |
| 114 | $entity = new Entity($organisation); |
| 115 | if ($entity instanceof Entity) { |
| 116 | $entity = $this->readResolvedReferences($entity, $resolveReferences); |
| 117 | $organisationList->addEntity($entity); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | return $organisationList; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * remove an organisation |
| 126 | * |
| 127 | * @param |
| 128 | * itemId |
| 129 | * |
| 130 | * @return Resource Status |
| 131 | */ |
| 132 | public function deleteEntity($itemId) |
| 133 | { |
| 134 | $entity = $this->readEntity($itemId, 1); |
| 135 | if (0 < $entity->toProperty()->departments->get()->count()) { |
| 136 | throw new Exception\Organisation\DepartmentListNotEmpty(); |
| 137 | } |
| 138 | $query = new Query\Organisation(Query\Base::DELETE); |
| 139 | $query->addConditionOrganisationId($itemId); |
| 140 | return ($this->deleteItem($query)) ? $entity : null; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * write a organisation |
| 145 | * |
| 146 | * @param |
| 147 | * organisationId |
| 148 | * |
| 149 | * @return Entity |
| 150 | */ |
| 151 | public function writeEntity(\BO\Zmsentities\Organisation $entity, $parentId) |
| 152 | { |
| 153 | $query = new Query\Organisation(Query\Base::INSERT); |
| 154 | $values = $query->reverseEntityMapping($entity, $parentId); |
| 155 | $query->addValues($values); |
| 156 | $this->writeItem($query); |
| 157 | $lastInsertId = $this->getWriter() |
| 158 | ->lastInsertId(); |
| 159 | if ($entity->toProperty()->ticketprinters->isAvailable()) { |
| 160 | $this->writeOrganisationTicketprinters($lastInsertId, $entity->ticketprinters); |
| 161 | } |
| 162 | return $this->readEntity($lastInsertId); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * update a organisation |
| 167 | * |
| 168 | * @param |
| 169 | * organisationId |
| 170 | * |
| 171 | * @return Entity |
| 172 | */ |
| 173 | public function updateEntity($organisationId, \BO\Zmsentities\Organisation $entity) |
| 174 | { |
| 175 | $query = new Query\Organisation(Query\Base::UPDATE); |
| 176 | $query->addConditionOrganisationId($organisationId); |
| 177 | $values = $query->reverseEntityMapping($entity); |
| 178 | $query->addValues($values); |
| 179 | $this->writeItem($query); |
| 180 | if ($entity->toProperty()->ticketprinters->isAvailable()) { |
| 181 | $this->updateOrganisationTicketprinters($entity->ticketprinters, $organisationId); |
| 182 | } |
| 183 | return $this->readEntity($organisationId, 1, true); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * create ticketprinters of an organisation |
| 188 | * |
| 189 | * @param |
| 190 | * organisationID, |
| 191 | * ticketprinterList |
| 192 | * |
| 193 | * @return Boolean |
| 194 | */ |
| 195 | protected function writeOrganisationTicketprinters($organisationId, $ticketprinterList) |
| 196 | { |
| 197 | $deleteQuery = new Query\Ticketprinter(Query\Base::DELETE); |
| 198 | $deleteQuery->addConditionOrganisationId($organisationId); |
| 199 | $this->deleteItem($deleteQuery); |
| 200 | foreach ($ticketprinterList as $ticketprinter) { |
| 201 | $ticketprinter['enabled'] = (isset($ticketprinter['enabled']) && $ticketprinter['enabled']); |
| 202 | $ticketprinter = new \BO\Zmsentities\Ticketprinter($ticketprinter); |
| 203 | $query = new Ticketprinter(); |
| 204 | $query->writeEntity($ticketprinter, $organisationId); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * update ticketprinters of an organisation |
| 210 | * |
| 211 | * @param |
| 212 | * organisationID, |
| 213 | * ticketprinterList |
| 214 | * |
| 215 | * @return Boolean |
| 216 | */ |
| 217 | protected function updateOrganisationTicketprinters($ticketprinterList, $organisationId) |
| 218 | { |
| 219 | foreach ($ticketprinterList as $item) { |
| 220 | $query = new Query\Ticketprinter(Query\Base::UPDATE); |
| 221 | $entity = new \BO\Zmsentities\Ticketprinter($item); |
| 222 | $query->addConditionHash($entity->getId()); |
| 223 | $query->addValues($query->reverseEntityMapping($entity, $organisationId)); |
| 224 | $this->writeItem($query); |
| 225 | } |
| 226 | } |
| 227 | } |