hasOption(static::OPTION_DELETE_DELIVERY_DATA_SERVICES)) { throw new \common_exception_Error('Invalid Option provided: ' . static::OPTION_DELETE_DELIVERY_DATA_SERVICES); } } /** * @param DeliveryDeleteRequest $request * @param boolean $byChunk * @return bool * @throws \Exception */ public function execute(DeliveryDeleteRequest $request, $byChunk = false) { $this->request = $request; $delivery = $this->request->getDeliveryResource(); $this->report = common_report_Report::createInfo('Deleting Delivery: ' . $delivery->getUri()); $executions = $this->getDeliveryExecutions($delivery); $executionsLimit = $this->hasOption(self::OPTION_LIMIT_DELIVERY_EXECUTIONS) ? $this->getOption(self::OPTION_LIMIT_DELIVERY_EXECUTIONS) : count($executions); if ($byChunk && $executions && count($executions) > $executionsLimit) { $executionsForDeleting = array_slice($executions, 0, $executionsLimit); $this->deleteDeliveryExecutions($executionsForDeleting); } else { $this->deleteDeliveryExecutions($executions); $this->deleteLinkedResources(); return $this->deleteDelivery(); } return true; } /** * @param core_kernel_classes_Resource $deliveryResource * * @return array|DeliveryExecution[] * @throws \common_exception_Error * @throws \common_exception_NoImplementation * @throws \oat\oatbox\service\exception\InvalidServiceManagerException */ protected function getDeliveryExecutions(core_kernel_classes_Resource $deliveryResource) { $serviceProxy = $this->getServiceProxy(); /** @var ExtensionsManager $extensionsManager */ $extensionsManager = $this->getServiceManager()->get(ExtensionsManager::SERVICE_ID); if ($serviceProxy instanceof Monitoring) { $executions = $serviceProxy->getExecutionsByDelivery($deliveryResource); } else if ($extensionsManager->isEnabled('taoResultServer')) { $resultStorage = $this->getResultStorage($deliveryResource->getUri()); $results = $resultStorage->getResultByDelivery([$deliveryResource->getUri()]); $executions = []; foreach ($results as $result) { $executions[] = $serviceProxy->getDeliveryExecution($result['deliveryResultIdentifier']); } } else { throw new \common_exception_Error('Cannot retrieve delivery executions by delivery'); } return $executions; } /** * @return common_report_Report */ public function getReport() { return $this->report; } /** * @return bool * @throws \Exception */ protected function deleteDelivery() { if (!$this->request->isDeliveryRemovalRequested()) { return true; } $services = $this->getDeliveryDeleteService(); foreach ($services as $service) { try { $deleted = $service->deleteDeliveryData($this->request); if ($deleted) { $this->report->add(common_report_Report::createSuccess( 'Delete delivery Service: ' . get_class($service) . ' data has been deleted.' )); } else { $this->report->add(common_report_Report::createInfo( 'Delete delivery Service: ' . get_class($service) . ' data has nothing to delete.' )); } } catch (\Exception $exception) { $this->report->add(common_report_Report::createInfo( $exception->getMessage() )); } } return true; } /** * @param $deliveryId * @return ResultManagement * @throws \common_exception_Error */ protected function getResultStorage() { /** @var ResultServerService $resultService */ $resultService = $this->getServiceLocator()->get(ResultServerService::SERVICE_ID); return $resultService->getResultStorage(); } /** * @return array|ServiceProxy|object */ protected function getServiceProxy() { return $this->getServiceLocator()->get(ServiceProxy::SERVICE_ID); } protected function getQtiTestService(): QtiTestService { return $this->getServiceLocator()->get(QtiTestService::class); } protected function getItemService(): ItemService { return $this->getServiceLocator()->get(ItemService::class); } protected function getTestService(): TestService { return $this->getServiceLocator()->get(TestService::class); } /** * @return DeliveryDelete[] * @throws \common_exception_Error */ private function getDeliveryDeleteService() { $services = []; $servicesStrings = $this->getOption(static::OPTION_DELETE_DELIVERY_DATA_SERVICES); foreach ($servicesStrings as $serviceString) { $deleteService = $this->getServiceLocator()->get($serviceString); if (!$deleteService instanceof DeliveryDelete) { throw new \common_exception_Error('Invalid Delete Service provided: ' . $serviceString); } $services[] = $deleteService; } return $services; } /** * @param $deliveryResource * @param $execution * @return DeliveryExecutionDeleteRequest * @throws \Exception */ private function buildDeliveryExecutionDeleteRequest($deliveryResource, $execution) { /** @var TestSessionService $testSessionService */ $testSessionService = $this->getServiceLocator()->get(TestSessionService::SERVICE_ID); try { $session = $testSessionService->getTestSession($execution, false); } catch (\Exception $exception) { $session = null; } return new DeliveryExecutionDeleteRequest( $deliveryResource, $execution, $session ); } private function deleteLinkedResources(): void { if (!$this->request->isRecursive()) { return; } $delivery = $this->request->getDeliveryResource(); if (!$delivery->exists()) { return; } $test = $delivery->getUniquePropertyValue( $delivery->getProperty(DeliveryAssemblyService::PROPERTY_ORIGIN) ); $itemService = $this->getItemService(); foreach ($this->getQtiTestService()->getItems($test) as $item) { $itemService->deleteResource($item); } $this->getTestService()->deleteResource($test); } /** * @param DeliveryExecution[] $executions * @throws \common_exception_Error */ private function deleteDeliveryExecutions(array $executions): void { if (!$this->request->isExecutionsRemovalRequested()) { return; } $delivery = $this->request->getDeliveryResource(); /** @var DeliveryExecutionDeleteService $deliveryExecutionDeleteService */ $deliveryExecutionDeleteService = $this->getServiceLocator()->get(DeliveryExecutionDeleteService::SERVICE_ID); foreach ($executions as $execution) { try { $requestDeleteExecution = $this->buildDeliveryExecutionDeleteRequest( $delivery, $execution ); $deliveryExecutionDeleteService->execute($requestDeleteExecution); $this->report->add($deliveryExecutionDeleteService->getReport()); } catch (\Exception $exception) { $this->report->add(common_report_Report::createFailure('Failing deleting execution: ' . $execution->getIdentifier())); $this->report->add(common_report_Report::createFailure($exception->getMessage())); } } } /** * @param core_kernel_classes_Resource $delivery * @return bool * @throws \common_exception_Error */ public function hasDeliveryExecutions(KernelResource $delivery) { $executions = $this->getDeliveryExecutions($delivery); return !empty($executions); } }