Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ExportDataHandler.php
Go to the documentation of this file.
1 <?php
8 
14 
19 {
25  private $subdirectoryPath = 'analytics/';
26 
32  private $archiveName = 'data.tgz';
33 
37  private $filesystem;
38 
42  private $archive;
43 
49  private $reportWriter;
50 
56  private $cryptographer;
57 
63  private $fileRecorder;
64 
72  public function __construct(
73  Filesystem $filesystem,
74  Archive $archive,
75  ReportWriterInterface $reportWriter,
76  Cryptographer $cryptographer,
77  FileRecorder $fileRecorder
78  ) {
79  $this->filesystem = $filesystem;
80  $this->archive = $archive;
81  $this->reportWriter = $reportWriter;
82  $this->cryptographer = $cryptographer;
83  $this->fileRecorder = $fileRecorder;
84  }
85 
89  public function prepareExportData()
90  {
91  try {
92  $tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
93 
94  $this->prepareDirectory($tmpDirectory, $this->getTmpFilesDirRelativePath());
95  $this->reportWriter->write($tmpDirectory, $this->getTmpFilesDirRelativePath());
96 
97  $tmpFilesDirectoryAbsolutePath = $this->validateSource($tmpDirectory, $this->getTmpFilesDirRelativePath());
98  $archiveAbsolutePath = $this->prepareFileDirectory($tmpDirectory, $this->getArchiveRelativePath());
99  $this->pack(
100  $tmpFilesDirectoryAbsolutePath,
101  $archiveAbsolutePath
102  );
103 
104  $this->validateSource($tmpDirectory, $this->getArchiveRelativePath());
105  $this->fileRecorder->recordNewFile(
106  $this->cryptographer->encode($tmpDirectory->readFile($this->getArchiveRelativePath()))
107  );
108  } finally {
109  $tmpDirectory->delete($this->getTmpFilesDirRelativePath());
110  $tmpDirectory->delete($this->getArchiveRelativePath());
111  }
112 
113  return true;
114  }
115 
121  private function getTmpFilesDirRelativePath()
122  {
123  return $this->subdirectoryPath . 'tmp/';
124  }
125 
131  private function getArchiveRelativePath()
132  {
133  return $this->subdirectoryPath . $this->archiveName;
134  }
135 
143  private function prepareDirectory(WriteInterface $directory, $path)
144  {
145  $directory->delete($path);
146 
147  return $directory->getAbsolutePath($path);
148  }
149 
157  private function prepareFileDirectory(WriteInterface $directory, $path)
158  {
159  $directory->delete($path);
160  if (dirname($path) !== '.') {
161  $directory->create(dirname($path));
162  }
163 
164  return $directory->getAbsolutePath($path);
165  }
166 
174  private function pack($source, $destination)
175  {
176  $this->archive->pack(
177  $source,
178  $destination,
179  is_dir($source) ?: false
180  );
181 
182  return true;
183  }
184 
195  private function validateSource(WriteInterface $directory, $path)
196  {
197  if (!$directory->isExist($path)) {
198  throw new LocalizedException(__('The "%1" source doesn\'t exist.', $directory->getAbsolutePath($path)));
199  }
200 
201  return $directory->getAbsolutePath($path);
202  }
203 }
$source
Definition: source.php:23
__()
Definition: __.php:13
$tmpDirectory
__construct(Filesystem $filesystem, Archive $archive, ReportWriterInterface $reportWriter, Cryptographer $cryptographer, FileRecorder $fileRecorder)
$filesystem