prepareData($params); $report = new Report(Report::TYPE_SUCCESS); if ($params['count'] < $params[DeliveryMetadataListener::OPTION_MAX_TRIES]) { $params['count']++; try { $this->getPersistDataService()->persist($params); $report->setMessage('Success MetaData syncing for delivery: ' . $params['deliveryId']); } catch (Throwable $exception) { $this->logError(sprintf( 'Failing MetaData syncing for delivery: %s with message: %s', $params['deliveryId'], $exception->getMessage() )); $report->setType(Report::TYPE_ERROR); $report->setMessage('Failing MetaData syncing for delivery: ' . $params['deliveryId']); $this->requeueTask($params); } } return $report; } public function jsonSerialize() { return self::class; } /** * @throws InvalidServiceManagerException */ private function getQueueDispatcher(): QueueDispatcher { return $this->getServiceLocator()->get(QueueDispatcher::SERVICE_ID); } /** * @param $params * @throws InvalidServiceManagerException */ private function requeueTask($params): void { /** @var QueueDispatcher $queueDispatcher */ $queueDispatcher = $this->getQueueDispatcher(); $queueDispatcher->createTask( $this, $params, __('DataStore sync retry for delivery "%s".', $params['deliveryId']) ); } private function getPersistDataService(): PersistDataService { return $this->getServiceLocator()->get(PersistDataService::class); } /** * @throws common_exception_Error * @throws core_kernel_persistence_Exception */ private function prepareData($params) { $params['count'] = $params['count'] ?? 0; if (!isset($params['deliveryMetaData'], $params['testMetaData'], $params['testUri'], $params['itemMetaData'])) { $compiler = $this->getMetaDataCompiler(); //DeliveryMetaData $deliveryResource = $this->getResource($params['deliveryId']); $params['deliveryMetaData'] = $compiler->compile($deliveryResource); //test MetaData $test = $this->getTest($deliveryResource); $params['testUri'] = $this->getTestUri($deliveryResource); $params['testMetaData'] = $compiler->compile($test); //Item MetaData $params['itemMetaData'] = $this->getItemMetaData($test, $compiler); } return $params; } private function getItemMetaData(core_kernel_classes_Resource $test, ResourceJsonMetadataCompiler $compiler): array { /** @var taoQtiTest_models_classes_QtiTestService $testService */ $testService = $this->getServiceLocator()->get(taoQtiTest_models_classes_QtiTestService::class); $items = $testService->getItems($test); $itemMetaData = []; foreach ($items as $item) { $itemMetaData[] = $compiler->compile($item); } return $itemMetaData; } /** * @throws common_exception_Error * @throws core_kernel_persistence_Exception */ private function getTest(core_kernel_classes_Resource $deliveryResource): core_kernel_classes_Resource { $testUri = $this->getTestUri($deliveryResource); return $this->getResource($testUri); } /** * @throws core_kernel_persistence_Exception */ private function getTestUri(core_kernel_classes_Resource $deliveryResource): ?string { $testProperty = $this->getProperty(DeliveryAssemblyService::PROPERTY_ORIGIN); $test = $deliveryResource->getOnePropertyValue($testProperty); return $test ? $test->getUri() : null; } private function getMetaDataCompiler(): ResourceJsonMetadataCompiler { return $this->getServiceLocator()->get(ResourceJsonMetadataCompiler::SERVICE_ID); } }