filesystem = $filesystem; $this->archive = $archive; $this->reportWriter = $reportWriter; $this->cryptographer = $cryptographer; $this->fileRecorder = $fileRecorder; } /** * @inheritdoc */ public function prepareExportData() { try { $tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP); $this->prepareDirectory($tmpDirectory, $this->getTmpFilesDirRelativePath()); $this->reportWriter->write($tmpDirectory, $this->getTmpFilesDirRelativePath()); $tmpFilesDirectoryAbsolutePath = $this->validateSource($tmpDirectory, $this->getTmpFilesDirRelativePath()); $archiveAbsolutePath = $this->prepareFileDirectory($tmpDirectory, $this->getArchiveRelativePath()); $this->pack( $tmpFilesDirectoryAbsolutePath, $archiveAbsolutePath ); $this->validateSource($tmpDirectory, $this->getArchiveRelativePath()); $this->fileRecorder->recordNewFile( $this->cryptographer->encode($tmpDirectory->readFile($this->getArchiveRelativePath())) ); } finally { $tmpDirectory->delete($this->getTmpFilesDirRelativePath()); $tmpDirectory->delete($this->getArchiveRelativePath()); } return true; } /** * Return relative path to a directory for temporary files with reports data. * * @return string */ private function getTmpFilesDirRelativePath() { return $this->subdirectoryPath . 'tmp/'; } /** * Return relative path to a directory for an archive. * * @return string */ private function getArchiveRelativePath() { return $this->subdirectoryPath . $this->archiveName; } /** * Clean up a directory. * * @param WriteInterface $directory * @param string $path * @return string */ private function prepareDirectory(WriteInterface $directory, $path) { $directory->delete($path); return $directory->getAbsolutePath($path); } /** * Remove a file and a create parent directory a file. * * @param WriteInterface $directory * @param string $path * @return string */ private function prepareFileDirectory(WriteInterface $directory, $path) { $directory->delete($path); if (dirname($path) !== '.') { $directory->create(dirname($path)); } return $directory->getAbsolutePath($path); } /** * Packing data into an archive. * * @param string $source * @param string $destination * @return bool */ private function pack($source, $destination) { $this->archive->pack( $source, $destination, is_dir($source) ?: false ); return true; } /** * Validate that data source exist. * * Return absolute path in a validated data source. * * @param WriteInterface $directory * @param string $path * @return string * @throws LocalizedException If source is not exist. */ private function validateSource(WriteInterface $directory, $path) { if (!$directory->isExist($path)) { throw new LocalizedException(__('The "%1" source doesn\'t exist.', $directory->getAbsolutePath($path))); } return $directory->getAbsolutePath($path); } }