1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Analytics\Model;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Archive;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\WriteInterface;
/**
* Class for the handling of a new data collection for MBI.
*/
class ExportDataHandler implements ExportDataHandlerInterface
{
/**
* Subdirectory path for all temporary files.
*
* @var string
*/
private $subdirectoryPath = 'analytics/';
/**
* Filename of archive with collected data.
*
* @var string
*/
private $archiveName = 'data.tgz';
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var Archive
*/
private $archive;
/**
* Resource for write data of reports into separate files.
*
* @var ReportWriterInterface
*/
private $reportWriter;
/**
* Resource for encrypting data.
*
* @var Cryptographer
*/
private $cryptographer;
/**
* Resource for registration a new file.
*
* @var FileRecorder
*/
private $fileRecorder;
/**
* @param Filesystem $filesystem
* @param Archive $archive
* @param ReportWriterInterface $reportWriter
* @param Cryptographer $cryptographer
* @param FileRecorder $fileRecorder
*/
public function __construct(
Filesystem $filesystem,
Archive $archive,
ReportWriterInterface $reportWriter,
Cryptographer $cryptographer,
FileRecorder $fileRecorder
) {
$this->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);
}
}