directory = $directory; $this->pageQuery = $pageQuery; $this->params = $params; $this->logService = $logService; } /** * {@inheritDoc} */ public function getGeneration(int $pageId, int $generationType, string $generationOutPath, string $generationInPath, ?int $pageDuplicateId, string $dirName) { $this->logService->setLog('GenerationProcessService : Entry-point.'); if ($generationType != 2) { $files = []; $finder = new Finder(); $finder->files()->in($generationOutPath); if ($finder->hasResults()) { foreach ($finder as $file) { $files['type'] = $generationType; $name = $file->getRelativePathname(); $extension = $file->getExtension(); $content = $file->getContents(); $encoded = base64_encode($content); $files['files'][] = [ 'name' => $name, 'extension' => $extension, 'content' => $encoded ]; } $files['pageDuplicateId'] = $pageDuplicateId; $result = $this->saveGenerateFiles($pageId, $files); $this->removeArchiveAndDirectories($generationOutPath, $generationInPath); } else { $this->removeArchiveAndDirectories($generationOutPath, $generationInPath); $error = "No file to save found !"; $result = new UnrecoverableMessageHandlingException($error); } } else { $this->logService->setLog('GenerationProcessService : Getting Package...'); $absoluteOutPath = $this->directory->getAbsolutePath($this->params->get('arborescence_generation_out'), 'directory'); $localPath = $absoluteOutPath . $dirName . '.tgz'; $command = sprintf( "sshpass -p %s ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null %s@%s \"cd %s && tar czf - %s\" > %s", escapeshellarg($this->params->get('indesign_pass')), escapeshellarg($this->params->get('indesign_user')), escapeshellarg($this->params->get('indesign_server')), escapeshellarg($this->params->get('indesign_package')), escapeshellarg($dirName), escapeshellarg($localPath) ); $process = new Process($command); try { $process->mustRun(); $this->logService->setLog('GenerationProcessService : RUN Package... ' . $process->getOutput()); } catch (ProcessFailedException $exception) { $this->logService->setLog('GenerationProcessService : Failed Package EXCEPTION ERROR MESSAGE : ' . $exception->getMessage() . "\n"); $this->logService->setLog('GenerationProcessService : Failed Package PROCESS ERROR EXIT CODE : ' . $process->getExitCode() . "\n"); $this->logService->setLog('GenerationProcessService : Failed Package PROCESS ERROR OUTPUT : ' . $process->getErrorOutput() . "\n"); } if (file_exists($localPath)) { $this->logService->setLog('GenerationProcessService : PACKAGE CREATED'); return $localPath; } else { throw new NotFoundMaestroException("File Not found !"); } } return $result; } /** * @param string $generationOutPath * @param string $generationInPath * * @return void */ private function removeArchiveAndDirectories(string $generationOutPath, string $generationInPath): void { if (!$this->params->get('generation_keep_files')) { $removedOut = $this->directory->removeFromPath($generationOutPath); $removedIn = $this->directory->removeFromPath($generationInPath); $error = ""; if ($removedOut instanceof Exception) { $error .= "Error when trying to delete file at '$generationOutPath' : " . $removedOut->getMessage(); } if ($removedIn instanceof Exception) { $error .= "Error when trying to delete file at '$generationInPath' : " . $removedIn->getMessage(); } if ($error) { $this->logService->setLog("$generationOutPath : $error"); throw new UnrecoverableMessageHandlingException($error); } $this->logService->setLog('Archive removed.'); } } /** * @param int $pageId * @param array $files */ public function saveGenerateFiles(int $pageId, array $files) { try { $this->logService->setLog('Start save generation files'); $viewId = new CommonIdView( $pageId ); $page = $this->pageQuery->read($viewId); $files['files'] = $this->pageQuery->reorderFilesByName($files['files'], 'name'); $viewData = new CommonArrayView( $files ); $pageOption = $page->getOption(); $result = $this->pageQuery->addGeneratedFiles($pageOption, $viewData); $this->logService->setLog('End save generation files'); } catch (Exception $e) { $this->logService->setLog('Error save generation files ' . $e->getMessage()); $result = $e; } return $result; } }