Lines
100.00%
46 / 46
Functions and Methods
100.00%
7 / 7
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Owner | 100.00% | 46 / 46 | 100.00% | 7 / 7 | 13 | 100.00% | 1 / 1 | |||
| readEntity | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | |||||
| readResolvedReferences | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 3 | |||||
| readList | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 3 | |||||
| readByOrganisationId | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | |||||
| deleteEntity | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 3 | |||||
| writeEntity | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 1 | |||||
| updateEntity | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsbackend\Owner\Service; | |
| 4 | ||
| 5 | use BO\Zmsentities\Owner as Entity; | |
| 6 | use BO\Zmsentities\Collection\OwnerList as Collection; | |
| 7 | ||
| 8 | class Owner extends \BO\Zmsbackend\Base | |
| 9 | { | |
| 10 | /** | |
| 11 | * read entity | |
| 12 | * | |
| 13 | * @param int|string $itemId | |
| 14 | * @param int $resolveReferences | |
| 15 | * | |
| 16 | * @return Entity | |
| 17 | */ | |
| 18 | public function readEntity($itemId, $resolveReferences = 0) | |
| 19 | { | |
| 20 | $query = new \BO\Zmsbackend\Owner\Repository\Owner(\BO\Zmsbackend\Query\Base::SELECT); | |
| 21 | $query | |
| 22 | ->addEntityMapping() | |
| 23 | ->addResolvedReferences($resolveReferences) | |
| 24 | ->addConditionOwnerId($itemId); | |
| 25 | $owner = $this->fetchOne($query, new Entity()); | |
| 26 | return $this->readResolvedReferences($owner, $resolveReferences); | |
| 27 | } | |
| 28 | ||
| 29 | /** | |
| 30 | * @return \BO\Zmsentities\Schema\Entity | |
| 31 | */ | |
| 32 | #[\Override] | |
| 33 | public function readResolvedReferences(\BO\Zmsentities\Schema\Entity $entity, int $resolveReferences) | |
| 34 | { | |
| 35 | if (0 < $resolveReferences && isset($entity['id'])) { | |
| 36 | $entity['organisations'] = (new \BO\Zmsbackend\Organisation\Service\Organisation())->readByOwnerId($entity['id'], $resolveReferences - 1); | |
| 37 | } | |
| 38 | return $entity; | |
| 39 | } | |
| 40 | ||
| 41 | /** | |
| 42 | * read list of owners | |
| 43 | * | |
| 44 | * @param int $resolveReferences | |
| 45 | * | |
| 46 | * @return Collection | |
| 47 | */ | |
| 48 | public function readList($resolveReferences = 0) | |
| 49 | { | |
| 50 | $ownerList = new Collection(); | |
| 51 | $query = new \BO\Zmsbackend\Owner\Repository\Owner(\BO\Zmsbackend\Query\Base::SELECT); | |
| 52 | $query | |
| 53 | ->addResolvedReferences($resolveReferences) | |
| 54 | ->addEntityMapping(); | |
| 55 | $result = $this->fetchList($query, new Entity()); | |
| 56 | if (count($result)) { | |
| 57 | foreach ($result as $entity) { | |
| 58 | $entity = $this->readResolvedReferences($entity, $resolveReferences); | |
| 59 | $ownerList->addEntity($entity); | |
| 60 | } | |
| 61 | } | |
| 62 | return $ownerList; | |
| 63 | } | |
| 64 | ||
| 65 | public function readByOrganisationId(int $organisationId, int $resolveReferences = 0) | |
| 66 | { | |
| 67 | $query = new \BO\Zmsbackend\Owner\Repository\Owner(\BO\Zmsbackend\Query\Base::SELECT); | |
| 68 | $query | |
| 69 | ->addEntityMapping() | |
| 70 | ->addResolvedReferences($resolveReferences) | |
| 71 | ->addConditionOrganisationId($organisationId); | |
| 72 | $owner = $this->fetchOne($query, new Entity()); | |
| 73 | return $this->readResolvedReferences($owner, $resolveReferences); | |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * remove an owner | |
| 78 | * | |
| 79 | * @param int|string $itemId | |
| 80 | * | |
| 81 | * @return Entity|null | |
| 82 | */ | |
| 83 | public function deleteEntity($itemId) | |
| 84 | { | |
| 85 | $entity = $this->readEntity($itemId, 1); | |
| 86 | if (0 < $entity->toProperty()->organisations->get()->count()) { | |
| 87 | throw new \BO\Zmsbackend\Owner\Exception\OrganisationListNotEmpty(); | |
| 88 | } | |
| 89 | $query = new \BO\Zmsbackend\Owner\Repository\Owner(\BO\Zmsbackend\Query\Base::DELETE); | |
| 90 | $query->addConditionOwnerId($itemId); | |
| 91 | return ($this->deleteItem($query)) ? $entity : null; | |
| 92 | } | |
| 93 | ||
| 94 | /** | |
| 95 | * write an owner | |
| 96 | * | |
| 97 | * @param \BO\Zmsentities\Owner $entity | |
| 98 | * | |
| 99 | * @return Entity | |
| 100 | */ | |
| 101 | public function writeEntity(\BO\Zmsentities\Owner $entity) | |
| 102 | { | |
| 103 | $query = new \BO\Zmsbackend\Owner\Repository\Owner(\BO\Zmsbackend\Query\Base::INSERT); | |
| 104 | $values = $query->reverseEntityMapping($entity); | |
| 105 | $query->addValues($values); | |
| 106 | $this->writeItem($query); | |
| 107 | $lastInsertId = $this->getWriter()->lastInsertId(); | |
| 108 | return $this->readEntity($lastInsertId); | |
| 109 | } | |
| 110 | ||
| 111 | /** | |
| 112 | * update an owner | |
| 113 | * | |
| 114 | * @param int|string $ownerId | |
| 115 | * @param \BO\Zmsentities\Owner $entity | |
| 116 | * | |
| 117 | * @return Entity | |
| 118 | */ | |
| 119 | public function updateEntity($ownerId, \BO\Zmsentities\Owner $entity) | |
| 120 | { | |
| 121 | $query = new \BO\Zmsbackend\Owner\Repository\Owner(\BO\Zmsbackend\Query\Base::UPDATE); | |
| 122 | $query->addConditionOwnerId($ownerId); | |
| 123 | $values = $query->reverseEntityMapping($entity); | |
| 124 | $query->addValues($values); | |
| 125 | $this->writeItem($query); | |
| 126 | return $this->readEntity($ownerId); | |
| 127 | } | |
| 128 | } |