*/ class RestTest extends RestController { public const REST_IMPORTER_ID = 'importerId'; public const REST_FILE_NAME = 'testPackage'; public const REST_DELIVERY_PARAMS = 'delivery-params'; public const REST_DELIVERY_CLASS_LABELS = 'delivery-class-labels'; /** * @deprecated * @see \oat\taoDeliveryRdf\controller\RestTest::REST_DELIVERY_CLASS_LABELS */ public const REST_DELIVERY_CLASS_LABEL = 'delivery-class-label'; /** * Import test and compile it into delivery * * @throws Error * @throws MissingParameterException * @throws NotImplementedException */ public function compileDeferred(): void { if ($this->getRequestMethod() !== Request::HTTP_POST) { throw new NotImplementedException('Only post method is accepted to compile test'); } if (!$this->hasRequestParameter(self::REST_IMPORTER_ID)) { throw new MissingParameterException(self::REST_IMPORTER_ID, $this->getRequestURI()); } if (HttpHelper::hasUploadedFile(self::REST_FILE_NAME)) { try { $importerId = $this->getParameter(self::REST_IMPORTER_ID); $file = HttpHelper::getUploadedFile(self::REST_FILE_NAME); $customParams = $this->getDecodedParameter(self::REST_DELIVERY_PARAMS); $deliveryClassLabels = $this->getParameter(self::REST_DELIVERY_CLASS_LABELS) === null ? [$this->getParameter(self::REST_DELIVERY_CLASS_LABEL)] : $this->getDecodedParameter(self::REST_DELIVERY_CLASS_LABELS); $deliveryClassLabels = array_filter( $deliveryClassLabels, static function ($deliveryClassLabel): bool { return null !== $deliveryClassLabel; } ); $task = ImportAndCompile::createTask($importerId, $file, $customParams, $deliveryClassLabels); } catch (ImporterNotFound $e) { $this->returnFailure(new NotFoundException($e->getMessage())); } $result = ['reference_id' => $task->getId()]; /** @var TaskLogInterface $taskLog */ $taskLog = $this->getServiceLocator()->get(TaskLogInterface::SERVICE_ID); $report = $taskLog->getReport($task->getId()); if (!empty($report)) { if ($report instanceof Report) { // Serialize report to array $report = json_decode(json_encode($report), true); } $result['common_report_Report'] = $report; } $this->returnSuccess($result); } else { $this->returnFailure(new BadRequestException('Test package file was not given')); } } /** * @param string $name * @param mixed|null $default * * @return mixed|null */ private function getParameter(string $name, $default = null) { $bodyParameters = $this->getPsrRequest()->getParsedBody(); if (is_array($bodyParameters) && isset($bodyParameters[$name])) { return $bodyParameters[$name]; } if (is_object($bodyParameters) && property_exists($bodyParameters, $name)) { return $bodyParameters->{$name}; } return $default; } /** * @param string $name * * @return array */ private function getDecodedParameter(string $name): array { $data = $this->getParameter($name, []); return is_array($data) ? $data : json_decode(html_entity_decode($data), true); } }