![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/magento/module-analytics/Model/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ declare(strict_types=1); namespace Magento\Analytics\Model; use Magento\Analytics\ReportXml\DB\ReportValidator; use Magento\Framework\Filesystem\Directory\WriteInterface; /** * Writes reports in files in csv format */ class ReportWriter implements ReportWriterInterface { /** * File name for error reporting file in archive * * @var string */ private $errorsFileName = 'errors.csv'; /** * @var Config */ private $config; /** * @var ProviderFactory */ private $providerFactory; /** * @var ReportValidator */ private $reportValidator; /** * ReportWriter constructor. * * @param ConfigInterface $config * @param ReportValidator $reportValidator * @param ProviderFactory $providerFactory */ public function __construct( ConfigInterface $config, ReportValidator $reportValidator, ProviderFactory $providerFactory ) { $this->config = $config; $this->reportValidator = $reportValidator; $this->providerFactory = $providerFactory; } /** * @inheritdoc */ public function write(WriteInterface $directory, $path) { $errorsList = []; foreach ($this->config->get() as $file) { $provider = reset($file['providers']); if (isset($provider['parameters']['name'])) { $error = $this->reportValidator->validate($provider['parameters']['name']); if ($error) { $errorsList[] = $error; continue; } } /** @var $providerObject */ $providerObject = $this->providerFactory->create($provider['class']); $fileName = $provider['parameters'] ? $provider['parameters']['name'] : $provider['name']; $fileFullPath = $path . $fileName . '.csv'; $fileData = $providerObject->getReport(...array_values($provider['parameters'])); $stream = $directory->openFile($fileFullPath, 'w+'); $stream->lock(); $headers = []; foreach ($fileData as $row) { if (!$headers) { $headers = array_keys($row); $stream->writeCsv($headers); } $stream->writeCsv($this->prepareRow($row)); } $stream->unlock(); $stream->close(); } if ($errorsList) { $errorStream = $directory->openFile($path . $this->errorsFileName, 'w+'); foreach ($errorsList as $error) { $errorStream->lock(); $errorStream->writeCsv($error); $errorStream->unlock(); } $errorStream->close(); } return true; } /** * Replace wrong symbols in row * * Strip backslashes before double quotes so they will be properly escaped in the generated csv * * @see fputcsv() * @param array $row * @return array */ private function prepareRow(array $row): array { return preg_replace('/\\\+(?=\")/', '', $row); } }