Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReportWriter.php
Go to the documentation of this file.
1 <?php
7 
10 
16 {
22  private $errorsFileName = 'errors.csv';
23 
27  private $config;
28 
32  private $providerFactory;
33 
37  private $reportValidator;
38 
46  public function __construct(
47  ConfigInterface $config,
48  ReportValidator $reportValidator,
49  ProviderFactory $providerFactory
50  ) {
51  $this->config = $config;
52  $this->reportValidator = $reportValidator;
53  $this->providerFactory = $providerFactory;
54  }
55 
59  public function write(WriteInterface $directory, $path)
60  {
61  $errorsList = [];
62  foreach ($this->config->get() as $file) {
63  $provider = reset($file['providers']);
64  if (isset($provider['parameters']['name'])) {
65  $error = $this->reportValidator->validate($provider['parameters']['name']);
66  if ($error) {
67  $errorsList[] = $error;
68  continue;
69  }
70  }
72  $providerObject = $this->providerFactory->create($provider['class']);
73  $fileName = $provider['parameters'] ? $provider['parameters']['name'] : $provider['name'];
74  $fileFullPath = $path . $fileName . '.csv';
75  $fileData = $providerObject->getReport(...array_values($provider['parameters']));
76  $stream = $directory->openFile($fileFullPath, 'w+');
77  $stream->lock();
78  $headers = [];
79  foreach ($fileData as $row) {
80  if (!$headers) {
81  $headers = array_keys($row);
82  $stream->writeCsv($headers);
83  }
84  $stream->writeCsv($row);
85  }
86  $stream->unlock();
87  $stream->close();
88  }
89  if ($errorsList) {
90  $errorStream = $directory->openFile($path . $this->errorsFileName, 'w+');
91  foreach ($errorsList as $error) {
92  $errorStream->lock();
93  $errorStream->writeCsv($error);
94  $errorStream->unlock();
95  }
96  $errorStream->close();
97  }
98 
99  return true;
100  }
101 }
write(WriteInterface $directory, $path)
$config
Definition: fraud_order.php:17
$fileName
Definition: translate.phtml:15
__construct(ConfigInterface $config, ReportValidator $reportValidator, ProviderFactory $providerFactory)