Lines
88.31%
68 / 77
Functions and Methods
62.50%
5 / 8
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Source | 88.31% | 68 / 77 | 62.50% | 5 / 8 | 27.08 | 0.00% | 0 / 1 | |||
| readEntity | 86.66% | 13 / 15 | 0.00% | 0 / 1 | 7.12 | |||||
| readList | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 4 | |||||
| readResolvedReferences | 100.00% | 14 / 14 | 100.00% | 1 / 1 | 2 | |||||
| writeEntity | 94.11% | 16 / 17 | 0.00% | 0 / 1 | 4.00 | |||||
| writeInsertRelations | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | |||||
| writeDeleteBySource | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 2 | |||||
| writeDeleteRelations | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | |||||
| removeCache | 25.00% | 2 / 8 | 0.00% | 0 / 1 | 15.55 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsbackend\Source\Service; | |
| 4 | ||
| 5 | use BO\Zmsbackend\Application as App; | |
| 6 | use BO\Zmsentities\Collection\ScopeList; | |
| 7 | use BO\Zmsentities\Source as Entity; | |
| 8 | use BO\Zmsentities\Collection\SourceList as Collection; | |
| 9 | use BO\Zmsentities\Collection\ProviderList; | |
| 10 | use BO\Zmsentities\Collection\RequestList; | |
| 11 | use BO\Zmsentities\Collection\RequestRelationList; | |
| 12 | ||
| 13 | /** | |
| 14 | * | |
| 15 | * @SuppressWarnings(Coupling) | |
| 16 | * | |
| 17 | */ | |
| 18 | class Source extends \BO\Zmsbackend\Base | |
| 19 | { | |
| 20 | /** | |
| 21 | * Fetch source from db | |
| 22 | * | |
| 23 | * @return \BO\Zmsentities\Source | |
| 24 | */ | |
| 25 | public function readEntity($sourceName, int $resolveReferences = 0, bool $disableCache = false) | |
| 26 | { | |
| 27 | $cacheKey = "source-$sourceName-$resolveReferences"; | |
| 28 | ||
| 29 | if (!$disableCache && App::$cache && App::$cache->has($cacheKey)) { | |
| 30 | $entity = App::$cache->get($cacheKey); | |
| 31 | } | |
| 32 | ||
| 33 | if (empty($entity)) { | |
| 34 | $query = new \BO\Zmsbackend\Source\Repository\Source(\BO\Zmsbackend\Query\Base::SELECT); | |
| 35 | $query | |
| 36 | ->addEntityMapping() | |
| 37 | ->addResolvedReferences($resolveReferences) | |
| 38 | ->addConditionSource($sourceName); | |
| 39 | $entity = $this->fetchOne($query, new Entity()); | |
| 40 | if (! $entity->hasId()) { | |
| 41 | return null; | |
| 42 | } | |
| 43 | ||
| 44 | if (App::$cache) { | |
| 45 | App::$cache->set($cacheKey, $entity); | |
| 46 | } | |
| 47 | } | |
| 48 | ||
| 49 | return $this->readResolvedReferences($entity, $resolveReferences, $disableCache); | |
| 50 | } | |
| 51 | ||
| 52 | /** | |
| 53 | * read a list of sources | |
| 54 | * | |
| 55 | * @return \BO\Zmsentities\Collection\SourceList | |
| 56 | */ | |
| 57 | public function readList(int $resolveReferences = 0) | |
| 58 | { | |
| 59 | $collection = new Collection(); | |
| 60 | $query = new \BO\Zmsbackend\Source\Repository\Source(\BO\Zmsbackend\Query\Base::SELECT); | |
| 61 | $query->addEntityMapping() | |
| 62 | ->addResolvedReferences($resolveReferences); | |
| 63 | $result = $this->fetchList($query, new Entity()); | |
| 64 | if (count($result)) { | |
| 65 | foreach ($result as $entity) { | |
| 66 | if ($entity instanceof Entity) { | |
| 67 | $entity = $this->readResolvedReferences($entity, $resolveReferences); | |
| 68 | $collection->addEntity($entity); | |
| 69 | } | |
| 70 | } | |
| 71 | } | |
| 72 | return $collection; | |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * resolve entity references | |
| 77 | * | |
| 78 | * @return \BO\Zmsentities\Source | |
| 79 | */ | |
| 80 | #[\Override] | |
| 81 | public function readResolvedReferences( | |
| 82 | \BO\Zmsentities\Schema\Entity $entity, | |
| 83 | int $resolveReferences, | |
| 84 | bool $disableCache = false | |
| 85 | ) { | |
| 86 | if (0 < $resolveReferences) { | |
| 87 | $entity['providers'] = (new ProviderList()) | |
| 88 | ->addList((new \BO\Zmsbackend\Provider\Service\Provider())->readListBySource($entity->source, $resolveReferences - 1)); | |
| 89 | $entity['requests'] = (new RequestList()) | |
| 90 | ->addList((new \BO\Zmsbackend\Request\Service\Request())->readListBySource( | |
| 91 | $entity->source, | |
| 92 | $resolveReferences - 1, | |
| 93 | $disableCache | |
| 94 | )); | |
| 95 | $entity['requestrelation'] = (new RequestRelationList()) | |
| 96 | ->addList((new \BO\Zmsbackend\RequestRelation\Service\RequestRelation())->readListBySource($entity->source)); | |
| 97 | $entity['scopes'] = (new ScopeList()) | |
| 98 | ->addList((new \BO\Zmsbackend\Scope\Service\Scope())->readList($resolveReferences - 1, $disableCache)); | |
| 99 | } | |
| 100 | ||
| 101 | return $entity; | |
| 102 | } | |
| 103 | ||
| 104 | /** | |
| 105 | * write or update a source | |
| 106 | * | |
| 107 | * @return \BO\Zmsentities\Source | |
| 108 | */ | |
| 109 | public function writeEntity(Entity $entity, int $resolveReferences = 0) | |
| 110 | { | |
| 111 | if (! $entity->isCompleteAndEditable()) { | |
| 112 | throw new \BO\Zmsbackend\Source\Exception\SourceInvalidInput(); | |
| 113 | } | |
| 114 | $this->writeDeleteBySource($entity->getSource()); | |
| 115 | $query = new \BO\Zmsbackend\Source\Repository\Source(\BO\Zmsbackend\Query\Base::INSERT); | |
| 116 | $query->addValues( | |
| 117 | array( | |
| 118 | 'source' => $entity->getSource(), | |
| 119 | 'label' => $entity->getLabel(), | |
| 120 | 'editable' => ($entity->isEditable()) ? 1 : 0, | |
| 121 | 'contact__name' => $entity->contact['name'], | |
| 122 | 'contact__email' => $entity->contact['email'] | |
| 123 | ) | |
| 124 | ); | |
| 125 | if ($this->writeItem($query)) { | |
| 126 | $this->writeInsertRelations($entity); | |
| 127 | } | |
| 128 | ||
| 129 | $this->removeCache($entity->getSource()); | |
| 130 | ||
| 131 | return $this->readEntity($entity->getSource(), $resolveReferences, true); | |
| 132 | } | |
| 133 | ||
| 134 | /** | |
| 135 | * delete provider and request relations of source | |
| 136 | */ | |
| 137 | public function writeInsertRelations(\BO\Zmsentities\Source $entity): void | |
| 138 | { | |
| 139 | (new \BO\Zmsbackend\Provider\Service\Provider())->writeListBySource($entity); | |
| 140 | (new \BO\Zmsbackend\Request\Service\Request())->writeListBySource($entity); | |
| 141 | (new \BO\Zmsbackend\RequestRelation\Service\RequestRelation())->writeListBySource($entity); | |
| 142 | } | |
| 143 | ||
| 144 | /** | |
| 145 | * delete by sourcename | |
| 146 | * | |
| 147 | * @return \BO\Zmsentities\Source | |
| 148 | */ | |
| 149 | public function writeDeleteBySource($sourceName) | |
| 150 | { | |
| 151 | $entity = $this->readEntity($sourceName); | |
| 152 | $query = new \BO\Zmsbackend\Source\Repository\Source(\BO\Zmsbackend\Query\Base::DELETE); | |
| 153 | $query->addConditionSource($sourceName); | |
| 154 | $this->writeDeleteRelations($sourceName); | |
| 155 | ||
| 156 | $this->removeCache($sourceName); | |
| 157 | ||
| 158 | return ($this->deleteItem($query)) ? $entity : null; | |
| 159 | } | |
| 160 | ||
| 161 | /** | |
| 162 | * delete provider and request relations of source | |
| 163 | */ | |
| 164 | public function writeDeleteRelations($sourceName): void | |
| 165 | { | |
| 166 | (new \BO\Zmsbackend\Provider\Service\Provider())->writeDeleteListBySource($sourceName); | |
| 167 | (new \BO\Zmsbackend\Request\Service\Request())->writeDeleteListBySource($sourceName); | |
| 168 | (new \BO\Zmsbackend\RequestRelation\Service\RequestRelation())->writeDeleteListBySource($sourceName); | |
| 169 | } | |
| 170 | ||
| 171 | /** | |
| 172 | * @return void | |
| 173 | */ | |
| 174 | public function removeCache($sourceName) | |
| 175 | { | |
| 176 | if (!App::$cache) { | |
| 177 | return; | |
| 178 | } | |
| 179 | ||
| 180 | if (App::$cache->has("source-$sourceName-0")) { | |
| 181 | App::$cache->delete("source-$sourceName-0"); | |
| 182 | } | |
| 183 | ||
| 184 | if (App::$cache->has("source-$sourceName-1")) { | |
| 185 | App::$cache->delete("source-$sourceName-1"); | |
| 186 | } | |
| 187 | ||
| 188 | if (App::$cache->has("source-$sourceName-2")) { | |
| 189 | App::$cache->delete("source-$sourceName-2"); | |
| 190 | } | |
| 191 | } | |
| 192 | } |