Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 244 |
|
0.00% |
0 / 17 |
CRAP | |
0.00% |
0 / 1 |
| RestDelivery | |
0.00% |
0 / 244 |
|
0.00% |
0 / 17 |
6162 | |
0.00% |
0 / 1 |
| getEventManager | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| generate | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
30 | |||
| generateDeferred | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
42 | |||
| update | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
42 | |||
| updateProperties | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| updateDeferred | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
42 | |||
| deleteDeferred | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
30 | |||
| get | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
240 | |||
| getStatus | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| getStatusesForChildren | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
20 | |||
| getTaskStatus | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| addExtraReturnData | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| createClass | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |||
| getDeliveryClassByParameters | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
132 | |||
| getDeliveryRootClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDeliveryService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDeliveryPatchRequestHandler | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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 | */ |
| 19 | |
| 20 | namespace oat\taoDeliveryRdf\controller; |
| 21 | |
| 22 | use common_Exception as BaseException; |
| 23 | use common_exception_MethodNotAllowed as HttpMethodNotAllowedException; |
| 24 | use common_exception_ResourceNotFound as ResourceNotFoundException; |
| 25 | use common_exception_RestApi as BadRequestException; |
| 26 | use core_kernel_classes_Class as KernelClass; |
| 27 | use core_kernel_classes_Resource as kernelResource; |
| 28 | use oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService; |
| 29 | use oat\oatbox\event\EventManager; |
| 30 | use oat\search\base\exception\SearchGateWayExeption; |
| 31 | use oat\tao\model\taskQueue\QueueDispatcher; |
| 32 | use oat\tao\model\taskQueue\TaskLog\Broker\TaskLogBrokerInterface; |
| 33 | use oat\tao\model\taskQueue\TaskLog\Entity\EntityInterface; |
| 34 | use oat\tao\model\taskQueue\TaskLog\TaskLogFilter; |
| 35 | use oat\tao\model\taskQueue\TaskLogActionTrait; |
| 36 | use oat\tao\model\taskQueue\TaskLogInterface; |
| 37 | use oat\taoDeliveryRdf\model\Delete\DeliveryDeleteTask; |
| 38 | use oat\taoDeliveryRdf\model\Delivery\Business\Service\DeliveryService; |
| 39 | use oat\taoDeliveryRdf\model\Delivery\Presentation\Web\RequestHandler\DeliveryPatchRequestHandler; |
| 40 | use oat\taoDeliveryRdf\model\DeliveryAssemblyService; |
| 41 | use oat\generis\model\OntologyRdfs; |
| 42 | use oat\taoDeliveryRdf\model\DeliveryFactory; |
| 43 | use oat\taoDeliveryRdf\model\tasks\CompileDelivery; |
| 44 | use oat\taoDeliveryRdf\model\tasks\UpdateDelivery; |
| 45 | use Request; |
| 46 | use RuntimeException; |
| 47 | use tao_models_classes_dataBinding_GenerisFormDataBindingException as FormDataBindingException; |
| 48 | |
| 49 | class RestDelivery extends \tao_actions_RestController |
| 50 | { |
| 51 | use TaskLogActionTrait; |
| 52 | |
| 53 | public const REST_DELIVERY_TEST_ID = 'test'; |
| 54 | public const REST_DELIVERY_SEARCH_PARAMS = 'searchParams'; |
| 55 | public const REST_DELIVERY_ID = 'delivery'; |
| 56 | public const REST_DELIVERY_CLASS_URI = 'delivery-uri'; |
| 57 | public const REST_DELIVERY_CLASS_LABEL = 'delivery-label'; |
| 58 | public const REST_DELIVERY_CLASS_PARENT = 'delivery-parent'; |
| 59 | public const REST_DELIVERY_CLASS_COMMENT = 'delivery-comment'; |
| 60 | public const TASK_ID_PARAM = 'id'; |
| 61 | |
| 62 | public const CLASS_LABEL_PARAM = 'delivery-label'; |
| 63 | public const CLASS_COMMENT_PARAM = 'delivery-comment'; |
| 64 | public const PARENT_CLASS_URI_PARAM = 'delivery-parent'; |
| 65 | |
| 66 | /** |
| 67 | * @return EventManager |
| 68 | */ |
| 69 | protected function getEventManager() |
| 70 | { |
| 71 | return $this->getServiceLocator()->get(EventManager::SERVICE_ID); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Generate a delivery from test uri |
| 76 | * Test uri has to be set and existing |
| 77 | */ |
| 78 | public function generate() |
| 79 | { |
| 80 | try { |
| 81 | if (!$this->hasRequestParameter(self::REST_DELIVERY_TEST_ID)) { |
| 82 | throw new \common_exception_MissingParameter(self::REST_DELIVERY_TEST_ID, $this->getRequestURI()); |
| 83 | } |
| 84 | |
| 85 | $test = $this->getResource($this->getRequestParameter(self::REST_DELIVERY_TEST_ID)); |
| 86 | if (!$test->exists()) { |
| 87 | throw new BadRequestException('Unable to find a test associated to the given uri.'); |
| 88 | } |
| 89 | |
| 90 | $label = 'Delivery of ' . $test->getLabel(); |
| 91 | $deliveryClass = $this->getDeliveryClassByParameters(); |
| 92 | |
| 93 | $deliveryFactory = $this->getServiceManager()->get(DeliveryFactory::SERVICE_ID); |
| 94 | /** @var \common_report_Report $report */ |
| 95 | $report = $deliveryFactory->create($deliveryClass, $test, $label); |
| 96 | |
| 97 | if ($report->getType() == \common_report_Report::TYPE_ERROR) { |
| 98 | $this->logInfo('Unable to generate delivery execution ' . |
| 99 | 'into taoDeliveryRdf::RestDelivery for test uri ' . $test->getUri()); |
| 100 | throw new BaseException('Unable to generate delivery execution.'); |
| 101 | } |
| 102 | $delivery = $report->getData(); |
| 103 | |
| 104 | /** @var DeliveryFactory $deliveryFactoryService */ |
| 105 | $deliveryFactoryService = $this->getServiceManager()->get(DeliveryFactory::SERVICE_ID); |
| 106 | $initialProperties = $deliveryFactoryService->getInitialPropertiesFromRequest($this->getRequest()); |
| 107 | $delivery = $deliveryFactoryService->setInitialProperties($initialProperties, $delivery); |
| 108 | $this->returnSuccess(['delivery' => $delivery->getUri()]); |
| 109 | } catch (\Exception $e) { |
| 110 | $this->returnFailure($e); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Put task to generate a delivery from test uri to the task queue |
| 116 | * Test uri has to be set and existing |
| 117 | */ |
| 118 | public function generateDeferred() |
| 119 | { |
| 120 | try { |
| 121 | if (! $this->hasRequestParameter(self::REST_DELIVERY_TEST_ID)) { |
| 122 | throw new \common_exception_MissingParameter(self::REST_DELIVERY_TEST_ID, $this->getRequestURI()); |
| 123 | } |
| 124 | |
| 125 | $test = $this->getResource($this->getRequestParameter(self::REST_DELIVERY_TEST_ID)); |
| 126 | if (! $test->exists()) { |
| 127 | throw new BadRequestException('Unable to find a test associated to the given uri.'); |
| 128 | } |
| 129 | |
| 130 | $deliveryClass = $this->getDeliveryClassByParameters(); |
| 131 | |
| 132 | /** @var DeliveryFactory $deliveryFactoryService */ |
| 133 | $deliveryFactoryService = $this->getServiceManager()->get(DeliveryFactory::SERVICE_ID); |
| 134 | $initialProperties = $deliveryFactoryService->getInitialPropertiesFromRequest($this->getRequest()); |
| 135 | |
| 136 | $task = CompileDelivery::createTask($test, $deliveryClass, $initialProperties); |
| 137 | |
| 138 | $result = [ |
| 139 | 'reference_id' => $task->getId() |
| 140 | ]; |
| 141 | |
| 142 | /** @var TaskLogInterface $taskLog */ |
| 143 | $taskLog = $this->getServiceManager()->get(TaskLogInterface::SERVICE_ID); |
| 144 | |
| 145 | $report = $taskLog->getReport($task->getId()); |
| 146 | |
| 147 | if (!empty($report)) { |
| 148 | if ($report instanceof \common_report_Report) { |
| 149 | //serialize report to array |
| 150 | $report = json_decode($report); |
| 151 | } |
| 152 | $result['common_report_Report'] = $report; |
| 153 | } |
| 154 | |
| 155 | return $this->returnSuccess($result); |
| 156 | } catch (\Exception $e) { |
| 157 | $this->returnFailure($e); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Update delivery by parameters |
| 163 | */ |
| 164 | public function update() |
| 165 | { |
| 166 | try { |
| 167 | if ($this->getRequestMethod() !== Request::HTTP_POST) { |
| 168 | throw new \common_exception_NotImplemented('Only post method is accepted to updating delivery'); |
| 169 | } |
| 170 | |
| 171 | if (! $this->hasRequestParameter(self::REST_DELIVERY_SEARCH_PARAMS)) { |
| 172 | throw new \common_exception_MissingParameter(self::REST_DELIVERY_SEARCH_PARAMS, $this->getRequestURI()); |
| 173 | } |
| 174 | |
| 175 | $where = json_decode( |
| 176 | html_entity_decode($this->getRequestParameter(self::REST_DELIVERY_SEARCH_PARAMS)), |
| 177 | true |
| 178 | ); |
| 179 | $propertyValues = $this->getRequestParameters(); |
| 180 | unset($propertyValues[self::REST_DELIVERY_SEARCH_PARAMS]); |
| 181 | |
| 182 | $deliveryModelClass = $this->getDeliveryRootClass(); |
| 183 | $deliveries = $deliveryModelClass->searchInstances($where, ['like' => false, 'recursive' => true]); |
| 184 | |
| 185 | $response = []; |
| 186 | |
| 187 | /** @var kernelResource $delivery */ |
| 188 | foreach ($deliveries as $key => $delivery) { |
| 189 | foreach ($propertyValues as $rdfKey => $rdfValue) { |
| 190 | $rdfKey = \tao_helpers_Uri::decode($rdfKey); |
| 191 | $property = $this->getProperty($rdfKey); |
| 192 | $delivery->editPropertyValues($property, $rdfValue); |
| 193 | } |
| 194 | $response[] = ['delivery' => $delivery->getUri()]; |
| 195 | } |
| 196 | $this->returnSuccess($response); |
| 197 | } catch (\Exception $e) { |
| 198 | $this->returnFailure($e); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @throws HttpMethodNotAllowedException |
| 204 | * @throws ResourceNotFoundException |
| 205 | * @throws BadRequestException |
| 206 | * @throws FormDataBindingException |
| 207 | */ |
| 208 | public function updateProperties(): void |
| 209 | { |
| 210 | if ($this->getRequestMethod() !== Request::HTTP_PATCH) { |
| 211 | throw new HttpMethodNotAllowedException(null, 0, [Request::HTTP_PATCH]); |
| 212 | } |
| 213 | |
| 214 | try { |
| 215 | $delivery = $this->getDeliveryService()->update( |
| 216 | $this->getDeliveryPatchRequestHandler()->handle( |
| 217 | $this->getPsrRequest() |
| 218 | ) |
| 219 | ); |
| 220 | } catch (RuntimeException $exception) { |
| 221 | throw new BadRequestException($exception->getMessage(), 422); |
| 222 | } |
| 223 | |
| 224 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 225 | $this->returnSuccess(['delivery' => $delivery->getUri()]); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Update delivery by parameters |
| 230 | */ |
| 231 | public function updateDeferred() |
| 232 | { |
| 233 | try { |
| 234 | if ($this->getRequestMethod() !== Request::HTTP_POST) { |
| 235 | throw new \common_exception_NotImplemented('Only post method is accepted to updating delivery'); |
| 236 | } |
| 237 | if (! $this->hasRequestParameter(self::REST_DELIVERY_SEARCH_PARAMS)) { |
| 238 | throw new \common_exception_MissingParameter(self::REST_DELIVERY_SEARCH_PARAMS, $this->getRequestURI()); |
| 239 | } |
| 240 | $where = json_decode( |
| 241 | html_entity_decode($this->getRequestParameter(self::REST_DELIVERY_SEARCH_PARAMS)), |
| 242 | true |
| 243 | ); |
| 244 | $propertyValues = $this->getRequestParameters(); |
| 245 | unset($propertyValues[self::REST_DELIVERY_SEARCH_PARAMS]); |
| 246 | |
| 247 | $task = UpdateDelivery::createTask($where, $propertyValues); |
| 248 | |
| 249 | $result = [ |
| 250 | 'reference_id' => $task->getId() |
| 251 | ]; |
| 252 | |
| 253 | /** @var TaskLogInterface $taskLog */ |
| 254 | $taskLog = $this->getServiceManager()->get(TaskLogInterface::SERVICE_ID); |
| 255 | $report = $taskLog->getReport($task->getId()); |
| 256 | if (!empty($report)) { |
| 257 | if ($report instanceof \common_report_Report) { |
| 258 | //serialize report to array |
| 259 | $report = json_decode($report); |
| 260 | } |
| 261 | $result['common_report_Report'] = $report; |
| 262 | } |
| 263 | return $this->returnSuccess($result); |
| 264 | } catch (\Exception $e) { |
| 265 | $this->returnFailure($e); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Delete delivery by URI |
| 271 | */ |
| 272 | public function deleteDeferred() |
| 273 | { |
| 274 | try { |
| 275 | if ($this->getRequestMethod() !== Request::HTTP_DELETE) { |
| 276 | throw new \common_exception_NotImplemented('Only delete method is accepted to deleting delivery'); |
| 277 | } |
| 278 | |
| 279 | if (!$this->hasRequestParameter('uri')) { |
| 280 | throw new \common_exception_MissingParameter('uri', $this->getRequestURI()); |
| 281 | } |
| 282 | |
| 283 | $uri = $this->getRequestParameter('uri'); |
| 284 | $delivery = $this->getResource($uri); |
| 285 | |
| 286 | if (!$delivery->exists()) { |
| 287 | $this->returnFailure(new \common_exception_NotFound('Delivery has not been found')); |
| 288 | } |
| 289 | |
| 290 | /** @var QueueDispatcher $queueDispatcher */ |
| 291 | $queueDispatcher = $this->getServiceManager()->get(QueueDispatcher::SERVICE_ID); |
| 292 | |
| 293 | $task = new DeliveryDeleteTask(); |
| 294 | $task->setServiceLocator($this->getServiceLocator()); |
| 295 | $taskParameters = ['deliveryId' => $uri]; |
| 296 | |
| 297 | $task = $queueDispatcher->createTask( |
| 298 | $task, |
| 299 | $taskParameters, |
| 300 | __('Deleting of "%s"', $delivery->getLabel()), |
| 301 | null, |
| 302 | true |
| 303 | ); |
| 304 | |
| 305 | $data = $this->getTaskLogReturnData( |
| 306 | $task->getId(), |
| 307 | DeliveryDeleteTask::class |
| 308 | ); |
| 309 | $this->returnSuccess($data); |
| 310 | } catch (\Exception $e) { |
| 311 | $this->returnFailure($e); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * List all deliveries or paginated range |
| 317 | */ |
| 318 | public function get() |
| 319 | { |
| 320 | try { |
| 321 | if ($this->getRequestMethod() !== Request::HTTP_GET) { |
| 322 | throw new \common_exception_NotImplemented('Only get method is accepted to getting deliveries'); |
| 323 | } |
| 324 | |
| 325 | $limit = 0; |
| 326 | if ($this->hasRequestParameter('limit')) { |
| 327 | $limit = $this->getRequestParameter('limit'); |
| 328 | if (!is_numeric($limit) || (int)$limit != $limit || $limit < 0) { |
| 329 | throw new \common_exception_ValidationFailed('limit', '\'Limit\' should be a positive integer'); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | $offset = 0; |
| 334 | if ($this->hasRequestParameter('offset')) { |
| 335 | $offset = $this->getRequestParameter('offset'); |
| 336 | if (!is_numeric($offset) || (int)$offset != $offset || $offset < 0) { |
| 337 | throw new \common_exception_ValidationFailed('offset', '\'Offset\' should be a positive integer'); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | $service = DeliveryAssemblyService::singleton(); |
| 342 | |
| 343 | /** @var kernelResource[] $deliveries */ |
| 344 | $deliveries = $service->getAllAssemblies(); |
| 345 | $overallCount = count($deliveries); |
| 346 | if ($offset || $limit) { |
| 347 | if ($overallCount <= $offset) { |
| 348 | throw new \common_exception_ValidationFailed('offset', '\'Offset\' is too large'); |
| 349 | } |
| 350 | $deliveries = array_slice($deliveries, $offset, $limit); |
| 351 | } |
| 352 | |
| 353 | $mappedDeliveries = []; |
| 354 | foreach ($deliveries as $delivery) { |
| 355 | $mappedDeliveries[] = [ |
| 356 | 'uri' => $delivery->getUri(), |
| 357 | 'label' => $delivery->getLabel(), |
| 358 | ]; |
| 359 | } |
| 360 | |
| 361 | $response = [ |
| 362 | 'items' => $mappedDeliveries, |
| 363 | 'overallCount' => $overallCount, |
| 364 | ]; |
| 365 | $this->returnSuccess($response); |
| 366 | } catch (\Exception $e) { |
| 367 | $this->returnFailure($e); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Action to retrieve test compilation task status from queue |
| 373 | */ |
| 374 | public function getStatus() |
| 375 | { |
| 376 | try { |
| 377 | if (!$this->hasRequestParameter(self::TASK_ID_PARAM)) { |
| 378 | throw new \common_exception_MissingParameter(self::TASK_ID_PARAM, $this->getRequestURI()); |
| 379 | } |
| 380 | |
| 381 | $data = $this->getTaskLogReturnData( |
| 382 | $this->getRequestParameter(self::TASK_ID_PARAM), |
| 383 | CompileDelivery::class |
| 384 | ); |
| 385 | $children = $this->getStatusesForChildren($this->getRequestParameter(self::TASK_ID_PARAM)); |
| 386 | $data['children'] = $children; |
| 387 | $this->returnSuccess($data); |
| 388 | } catch (\Exception $e) { |
| 389 | $this->returnFailure($e); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * @param $taskId |
| 395 | * @return array |
| 396 | */ |
| 397 | protected function getStatusesForChildren($taskId) |
| 398 | { |
| 399 | /** @var TaskLogInterface $taskLog */ |
| 400 | $taskLog = $this->getServiceManager()->get(TaskLogInterface::SERVICE_ID); |
| 401 | $filter = (new TaskLogFilter()) |
| 402 | ->eq(TaskLogBrokerInterface::COLUMN_PARENT_ID, $taskId); |
| 403 | $collection = $taskLog->search($filter); |
| 404 | $response = []; |
| 405 | if ($collection->isEmpty()) { |
| 406 | return $response; |
| 407 | } |
| 408 | /** @var EntityInterface $item */ |
| 409 | foreach ($collection as $item) { |
| 410 | $response[] = [ |
| 411 | 'id' => $this->getTaskId($item), |
| 412 | 'label' => $item->getLabel(), |
| 413 | 'status' => $this->getTaskStatus($item), |
| 414 | 'report' => $item->getReport() ? $this->getTaskReport($item) : [] |
| 415 | ]; |
| 416 | } |
| 417 | return $response; |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Return 'Success' instead of 'Completed', required by the specified API. |
| 422 | * |
| 423 | * @param EntityInterface $taskLogEntity |
| 424 | * @return string |
| 425 | */ |
| 426 | protected function getTaskStatus(EntityInterface $taskLogEntity) |
| 427 | { |
| 428 | if ($taskLogEntity->getStatus()->isCreated()) { |
| 429 | return 'In Progress'; |
| 430 | } elseif ($taskLogEntity->getStatus()->isCompleted()) { |
| 431 | return 'Success'; |
| 432 | } |
| 433 | |
| 434 | return $taskLogEntity->getStatus()->getLabel(); |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * @param EntityInterface $taskLogEntity |
| 439 | * @return array |
| 440 | */ |
| 441 | protected function addExtraReturnData(EntityInterface $taskLogEntity) |
| 442 | { |
| 443 | $data = []; |
| 444 | |
| 445 | if ($taskLogEntity->getReport()) { |
| 446 | $plainReport = $this->getPlainReport($taskLogEntity->getReport()); |
| 447 | |
| 448 | //the second report is the report of the compilation test |
| 449 | if (isset($plainReport[1]) && isset($plainReport[1]->getData()['uriResource'])) { |
| 450 | $data['delivery'] = $plainReport[1]->getData()['uriResource']; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | return $data; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | /** |
| 459 | * Create a Delivery Class |
| 460 | * |
| 461 | * Label parameter is mandatory |
| 462 | * If parent class parameter is an uri of valid delivery class, new class will be created under it |
| 463 | * If not parent class parameter is provided, class will be created under root class |
| 464 | * Comment parameter is not mandatory, used to describe new created class |
| 465 | * |
| 466 | * @return KernelClass |
| 467 | */ |
| 468 | public function createClass() |
| 469 | { |
| 470 | try { |
| 471 | $class = $this->createSubClass($this->getDeliveryRootClass()); |
| 472 | |
| 473 | $result = [ |
| 474 | 'message' => __('Class successfully created.'), |
| 475 | 'delivery-uri' => $class->getUri(), |
| 476 | ]; |
| 477 | |
| 478 | $this->returnSuccess($result); |
| 479 | } catch (\common_exception_ClassAlreadyExists $e) { |
| 480 | $result = [ |
| 481 | 'message' => $e->getMessage(), |
| 482 | 'delivery-uri' => $e->getClass()->getUri(), |
| 483 | ]; |
| 484 | $this->returnSuccess($result); |
| 485 | } catch (\Exception $e) { |
| 486 | $this->returnFailure($e); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Get a delivery class based on parameters |
| 492 | * |
| 493 | * If an uri parameter is provided, and it is a delivery class, this delivery class is returned |
| 494 | * If a label parameter is provided, and only one delivery class has this label, this delivery class is returned |
| 495 | * |
| 496 | * @return KernelClass |
| 497 | * @throws SearchGateWayExeption |
| 498 | * @throws BadRequestException |
| 499 | */ |
| 500 | protected function getDeliveryClassByParameters() |
| 501 | { |
| 502 | $rootDeliveryClass = $this->getDeliveryRootClass(); |
| 503 | |
| 504 | // If an uri is provided, check if it's an existing delivery class |
| 505 | if ($this->hasRequestParameter(self::REST_DELIVERY_CLASS_URI)) { |
| 506 | $deliveryClass = $this->getClass($this->getRequestParameter(self::REST_DELIVERY_CLASS_URI)); |
| 507 | if ( |
| 508 | $deliveryClass == $rootDeliveryClass |
| 509 | || ($deliveryClass->exists() && $deliveryClass->isSubClassOf($rootDeliveryClass)) |
| 510 | ) { |
| 511 | return $deliveryClass; |
| 512 | } |
| 513 | throw new BadRequestException(__('Delivery class uri provided is not a valid delivery class.')); |
| 514 | } |
| 515 | |
| 516 | if ($this->hasRequestParameter(self::REST_DELIVERY_CLASS_LABEL)) { |
| 517 | $label = $this->getRequestParameter(self::REST_DELIVERY_CLASS_LABEL); |
| 518 | |
| 519 | $deliveryClasses = $rootDeliveryClass->getSubClasses(true); |
| 520 | $classes = [$rootDeliveryClass->getUri()]; |
| 521 | foreach ($deliveryClasses as $class) { |
| 522 | $classes[] = $class->getUri(); |
| 523 | } |
| 524 | |
| 525 | /** @var ComplexSearchService $search */ |
| 526 | $search = $this->getServiceManager()->get(ComplexSearchService::SERVICE_ID); |
| 527 | $queryBuilder = $search->query(); |
| 528 | $criteria = $queryBuilder->newQuery() |
| 529 | ->add(OntologyRdfs::RDFS_LABEL)->equals($label) |
| 530 | ->add(OntologyRdfs::RDFS_SUBCLASSOF)->in($classes) |
| 531 | ; |
| 532 | $queryBuilder->setCriteria($criteria); |
| 533 | $result = $search->getGateway()->search($queryBuilder); |
| 534 | |
| 535 | switch ($result->count()) { |
| 536 | case 0: |
| 537 | throw new BadRequestException(__('Delivery with label "%s" not found', $label)); |
| 538 | case 1: |
| 539 | return $this->getClass($result->current()->getUri()); |
| 540 | default: |
| 541 | $availableClasses = []; |
| 542 | foreach ($result as $raw) { |
| 543 | $availableClasses[] = $raw->getUri(); |
| 544 | } |
| 545 | throw new BadRequestException(__( |
| 546 | 'Multiple delivery class found for label "%s": %s', |
| 547 | $label, |
| 548 | implode(',', $availableClasses) |
| 549 | )); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | return $rootDeliveryClass; |
| 554 | } |
| 555 | |
| 556 | protected function getDeliveryRootClass(): KernelClass |
| 557 | { |
| 558 | return $this->getClass(DeliveryAssemblyService::CLASS_URI); |
| 559 | } |
| 560 | |
| 561 | private function getDeliveryService(): DeliveryService |
| 562 | { |
| 563 | return $this->getPsrContainer()->get(DeliveryService::class); |
| 564 | } |
| 565 | |
| 566 | private function getDeliveryPatchRequestHandler(): DeliveryPatchRequestHandler |
| 567 | { |
| 568 | return $this->getPsrContainer()->get(DeliveryPatchRequestHandler::class); |
| 569 | } |
| 570 | } |