Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
59.38% |
38 / 64 |
|
47.62% |
10 / 21 |
CRAP | |
0.00% |
0 / 1 |
| KVDeliveryExecution | |
59.38% |
38 / 64 |
|
47.62% |
10 / 21 |
117.13 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getIdentifier | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOriginalIdentifier | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getStartTime | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFinishTime | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| getLabel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getState | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDelivery | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getUserIdentifier | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setState | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| getData | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| hasData | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| setData | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| jsonSerialize | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| exists | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| save | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAllMetadata | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| addMetadata | |
50.00% |
5 / 10 |
|
0.00% |
0 / 1 |
2.50 | |||
| getMetadata | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| extractCollection | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| retryDeliveryExecutionLoad | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2013-2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoDelivery\model\execution; |
| 23 | |
| 24 | use common_exception_Error; |
| 25 | use common_exception_NotFound; |
| 26 | use common_Logger; |
| 27 | use core_kernel_classes_Resource; |
| 28 | use oat\generis\model\OntologyRdfs; |
| 29 | use oat\taoDelivery\model\execution\implementation\KeyValueService; |
| 30 | use oat\taoDelivery\model\execution\metadata\Metadata; |
| 31 | use oat\taoDelivery\model\execution\metadata\MetadataCollection; |
| 32 | |
| 33 | /** |
| 34 | * Service to manage the execution of deliveries |
| 35 | * |
| 36 | * @access public |
| 37 | * @author Joel Bout, <joel@taotesting.com> |
| 38 | * @package taoDelivery |
| 39 | * |
| 40 | */ |
| 41 | class KVDeliveryExecution implements DeliveryExecutionMetadataInterface, OriginalIdAwareDeliveryExecutionInterface |
| 42 | { |
| 43 | /** |
| 44 | * @var KeyValueService |
| 45 | */ |
| 46 | private $service; |
| 47 | |
| 48 | private $id; |
| 49 | |
| 50 | private $data; |
| 51 | |
| 52 | public function __construct(KeyValueService $service, $identifier, $data = null) |
| 53 | { |
| 54 | $this->service = $service; |
| 55 | $this->id = $identifier; |
| 56 | $this->data = $data; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * (non-PHPdoc) |
| 61 | * |
| 62 | * @see DeliveryExecutionInterface::getIdentifier() |
| 63 | */ |
| 64 | public function getIdentifier() |
| 65 | { |
| 66 | return $this->id; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @inheritDoc |
| 71 | */ |
| 72 | public function getOriginalIdentifier(): string |
| 73 | { |
| 74 | return substr($this->id, strlen(KeyValueService::DELIVERY_EXECUTION_PREFIX)); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * (non-PHPdoc) |
| 79 | * |
| 80 | * @see DeliveryExecutionInterface::getStartTime() |
| 81 | */ |
| 82 | public function getStartTime() |
| 83 | { |
| 84 | return $this->getData(OntologyDeliveryExecution::PROPERTY_TIME_START); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * (non-PHPdoc) |
| 89 | * |
| 90 | * @see DeliveryExecutionInterface::getFinishTime() |
| 91 | */ |
| 92 | public function getFinishTime() |
| 93 | { |
| 94 | if ($this->hasData(OntologyDeliveryExecution::PROPERTY_TIME_END)) { |
| 95 | return $this->getData(OntologyDeliveryExecution::PROPERTY_TIME_END); |
| 96 | } |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * (non-PHPdoc) |
| 102 | * |
| 103 | * @see DeliveryExecutionInterface::getLabel() |
| 104 | */ |
| 105 | public function getLabel() |
| 106 | { |
| 107 | return $this->getData(OntologyRdfs::RDFS_LABEL); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * (non-PHPdoc) |
| 112 | * |
| 113 | * @see DeliveryExecutionInterface::getState() |
| 114 | */ |
| 115 | public function getState() |
| 116 | { |
| 117 | return new core_kernel_classes_Resource($this->getData(OntologyDeliveryExecution::PROPERTY_STATUS)); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * (non-PHPdoc) |
| 122 | * |
| 123 | * @see DeliveryExecutionInterface::getDelivery() |
| 124 | */ |
| 125 | public function getDelivery() |
| 126 | { |
| 127 | return new core_kernel_classes_Resource($this->getData(OntologyDeliveryExecution::PROPERTY_DELIVERY)); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * (non-PHPdoc) |
| 132 | * |
| 133 | * @see DeliveryExecutionInterface::getUserIdentifier() |
| 134 | */ |
| 135 | public function getUserIdentifier() |
| 136 | { |
| 137 | return $this->getData(OntologyDeliveryExecution::PROPERTY_SUBJECT); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * (non-PHPdoc) |
| 142 | * @see DeliveryExecutionInterface::setState() |
| 143 | */ |
| 144 | public function setState($state) |
| 145 | { |
| 146 | $oldState = $this->getState()->getUri(); |
| 147 | if ($oldState == $state) { |
| 148 | common_Logger::w('Delivery execution ' . $this->getIdentifier() . ' already in state ' . $state); |
| 149 | return false; |
| 150 | } |
| 151 | $this->setData(OntologyDeliveryExecution::PROPERTY_STATUS, $state); |
| 152 | if ($state == DeliveryExecutionInterface::STATE_FINISHED) { |
| 153 | $this->setData(OntologyDeliveryExecution::PROPERTY_TIME_END, microtime()); |
| 154 | } |
| 155 | return $this->service->updateDeliveryExecutionStatus($this, $oldState, $state); |
| 156 | } |
| 157 | |
| 158 | private function getData($dataKey) |
| 159 | { |
| 160 | if (is_null($this->data)) { |
| 161 | $this->data = $this->service->getData($this->id); |
| 162 | } |
| 163 | if (!isset($this->data[$dataKey])) { |
| 164 | throw new common_exception_NotFound('Information ' . $dataKey . ' not found for entry ' . $this->id); |
| 165 | } |
| 166 | return $this->data[$dataKey]; |
| 167 | } |
| 168 | |
| 169 | private function hasData($dataKey) |
| 170 | { |
| 171 | if (is_null($this->data)) { |
| 172 | $this->data = $this->service->getData($this->id); |
| 173 | } |
| 174 | return isset($this->data[$dataKey]); |
| 175 | } |
| 176 | |
| 177 | private function setData($dataKey, $value): void |
| 178 | { |
| 179 | if (is_null($this->data)) { |
| 180 | $this->data = $this->service->getData($this->id); |
| 181 | } |
| 182 | $this->data[$dataKey] = $value; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * (non-PHPdoc) |
| 187 | * @throws common_exception_Error |
| 188 | */ |
| 189 | public function jsonSerialize(): array |
| 190 | { |
| 191 | if ($this->data === null) { |
| 192 | $this->retryDeliveryExecutionLoad(); |
| 193 | } |
| 194 | |
| 195 | return $this->data; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @return bool |
| 200 | */ |
| 201 | public function exists() |
| 202 | { |
| 203 | return $this->service->exists($this->id); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Stored the current data |
| 208 | * |
| 209 | * @deprecated since version 13.1.0 - not used anywhere |
| 210 | */ |
| 211 | private function save() |
| 212 | { |
| 213 | $this->service->update($this); |
| 214 | } |
| 215 | |
| 216 | public function getAllMetadata(): MetadataCollection |
| 217 | { |
| 218 | $collection = new MetadataCollection(); |
| 219 | try { |
| 220 | $this->extractCollection($collection); |
| 221 | } catch (common_exception_NotFound $exception) { |
| 222 | common_Logger::w($exception->getMessage()); |
| 223 | } |
| 224 | return $collection; |
| 225 | } |
| 226 | |
| 227 | public function addMetadata(Metadata $metadata): void |
| 228 | { |
| 229 | try { |
| 230 | $collection = $this->getAllMetadata()->addMetadata($metadata); |
| 231 | $this->setData( |
| 232 | DeliveryExecutionMetadataInterface::PROPERTY_METADATA, |
| 233 | $collection->jsonSerialize() |
| 234 | ); |
| 235 | } catch (common_exception_NotFound $exception) { |
| 236 | $this->setData( |
| 237 | DeliveryExecutionMetadataInterface::PROPERTY_METADATA, |
| 238 | new MetadataCollection($metadata) |
| 239 | ); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | public function getMetadata(string $metadataId): ?Metadata |
| 244 | { |
| 245 | return $this->getAllMetadata()->getMetadataElement($metadataId); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * @throws common_exception_NotFound |
| 250 | */ |
| 251 | private function extractCollection(MetadataCollection $collection): void |
| 252 | { |
| 253 | foreach ($this->getData(DeliveryExecutionMetadataInterface::PROPERTY_METADATA) as $metadata) { |
| 254 | if ( |
| 255 | !($metadata instanceof Metadata) |
| 256 | && isset($metadata[Metadata::METADATA_ID], $metadata[Metadata::METADATA_CONTENT]) |
| 257 | ) { |
| 258 | $collection->addMetadata( |
| 259 | new Metadata($metadata[Metadata::METADATA_ID], $metadata[Metadata::METADATA_CONTENT]) |
| 260 | ); |
| 261 | continue; |
| 262 | } |
| 263 | $collection->addMetadata($metadata); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * @throws common_exception_Error |
| 269 | */ |
| 270 | private function retryDeliveryExecutionLoad(): void |
| 271 | { |
| 272 | $this->data = $this->service->getData($this->getIdentifier()); |
| 273 | |
| 274 | if ($this->data === null) { |
| 275 | throw new common_exception_Error('Delivery not found'); |
| 276 | } |
| 277 | } |
| 278 | } |