|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Sylius Sp. z o.o. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Sylius\GridImportExport\Manager; |
| 15 | + |
| 16 | +use Sylius\GridImportExport\Entity\ExportProcessInterface; |
| 17 | +use Sylius\GridImportExport\Exporter\CsvExporter; |
| 18 | +use Sylius\GridImportExport\Exporter\JsonExporter; |
| 19 | +use Sylius\Resource\Doctrine\Persistence\RepositoryInterface; |
| 20 | + |
| 21 | +final class BatchedExportDataManager implements BatchedExportDataManagerInterface |
| 22 | +{ |
| 23 | + private const FILE_BASED_FORMATS = [ |
| 24 | + CsvExporter::FORMAT, |
| 25 | + JsonExporter::FORMAT, |
| 26 | + ]; |
| 27 | + |
| 28 | + private string $temporaryExportsDirectory; |
| 29 | + |
| 30 | + /** @param RepositoryInterface<ExportProcessInterface> $processRepository */ |
| 31 | + public function __construct( |
| 32 | + protected RepositoryInterface $processRepository, |
| 33 | + string $temporaryDirectory, |
| 34 | + ) { |
| 35 | + $this->temporaryExportsDirectory = sprintf('%s/ongoing/', $temporaryDirectory); |
| 36 | + } |
| 37 | + |
| 38 | + public function createStorage(ExportProcessInterface $exportProcess): void |
| 39 | + { |
| 40 | + if (in_array($exportProcess->getFormat(), self::FILE_BASED_FORMATS)) { |
| 41 | + $ongoingProcessDirectory = $this->temporaryExportsDirectory . '/' . $exportProcess->getUuid(); |
| 42 | + $exportProcess->setTemporaryDataStorage($ongoingProcessDirectory); |
| 43 | + |
| 44 | + if (!is_dir($ongoingProcessDirectory)) { |
| 45 | + mkdir($ongoingProcessDirectory, recursive: true); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public function getStorage(ExportProcessInterface $exportProcess): ?string |
| 51 | + { |
| 52 | + return $exportProcess->getTemporaryDataStorage(); |
| 53 | + } |
| 54 | + |
| 55 | + public function setStorage(ExportProcessInterface $exportProcess): void |
| 56 | + { |
| 57 | + $exportProcess->setTemporaryDataStorage( |
| 58 | + $this->temporaryExportsDirectory . '/' . $exportProcess->getUuid(), |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + public function resetStorage(ExportProcessInterface $exportProcess): void |
| 63 | + { |
| 64 | + $exportProcess->setTemporaryDataStorage(null); |
| 65 | + } |
| 66 | + |
| 67 | + public function saveBatch(ExportProcessInterface $exportProcess, array $data): void |
| 68 | + { |
| 69 | + if (null === $this->getStorage($exportProcess)) { |
| 70 | + throw new \InvalidArgumentException( |
| 71 | + sprintf( |
| 72 | + 'No storage set on process with uuid "%s"', |
| 73 | + $exportProcess->getUuid(), |
| 74 | + ), |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + file_put_contents( |
| 79 | + rtrim($this->getStorage($exportProcess), '/') . '/' . uniqid() . '.json', |
| 80 | + json_encode($data, \JSON_THROW_ON_ERROR), |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + public function getBatchedData(ExportProcessInterface $exportProcess): iterable |
| 85 | + { |
| 86 | + if (null === $storage = $this->getStorage($exportProcess)) { |
| 87 | + throw new \InvalidArgumentException(sprintf('No storage set on process with uuid "%s"', $exportProcess->getUuid())); |
| 88 | + } |
| 89 | + |
| 90 | + foreach ($this->getFilesIn($storage) as $file) { |
| 91 | + $filePath = $storage . '/' . $file; |
| 92 | + if (is_file($filePath)) { |
| 93 | + yield json_decode((string) file_get_contents($filePath), true); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public function deleteBatchedData(ExportProcessInterface $exportProcess): void |
| 99 | + { |
| 100 | + $storage = $this->getStorage($exportProcess); |
| 101 | + if (null === $storage || !is_dir($storage)) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + foreach ($this->getFilesIn($storage) as $file) { |
| 106 | + unlink($storage . '/' . $file); |
| 107 | + } |
| 108 | + |
| 109 | + rmdir($storage); |
| 110 | + } |
| 111 | + |
| 112 | + private function getFilesIn(string $directory): array |
| 113 | + { |
| 114 | + $files = scandir($directory); |
| 115 | + |
| 116 | + return array_filter($files ?: [], static function ($file) { |
| 117 | + return str_ends_with($file, '.json'); |
| 118 | + }); |
| 119 | + } |
| 120 | +} |
0 commit comments