|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\OfflineShipping\Model\Plugin\AsyncConfig\Model; |
| 9 | + |
| 10 | +use Magento\AsyncConfig\Model\AsyncConfigPublisher; |
| 11 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 12 | +use Magento\Framework\App\RequestFactory; |
| 13 | +use Magento\Framework\Exception\FileSystemException; |
| 14 | +use Magento\Framework\Exception\LocalizedException; |
| 15 | +use Magento\Framework\Filesystem; |
| 16 | +use Magento\Framework\Math\Random; |
| 17 | + |
| 18 | +class AsyncConfigPublisherPlugin |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var Filesystem |
| 22 | + */ |
| 23 | + private Filesystem $filesystem; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var Random |
| 27 | + */ |
| 28 | + private Random $rand; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var RequestFactory |
| 32 | + */ |
| 33 | + private RequestFactory $requestFactory; |
| 34 | + |
| 35 | + /** |
| 36 | + * @param Filesystem $filesystem |
| 37 | + * @param Random $rand |
| 38 | + * @param RequestFactory $requestFactory |
| 39 | + */ |
| 40 | + public function __construct(Filesystem $filesystem, Random $rand, RequestFactory $requestFactory) |
| 41 | + { |
| 42 | + $this->filesystem = $filesystem; |
| 43 | + $this->rand = $rand; |
| 44 | + $this->requestFactory = $requestFactory; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Save table rate import file for async processing |
| 49 | + * |
| 50 | + * @param AsyncConfigPublisher $subject |
| 51 | + * @param array $configData |
| 52 | + * @return array |
| 53 | + * @throws FileSystemException|LocalizedException |
| 54 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 55 | + */ |
| 56 | + public function beforeSaveConfigData(AsyncConfigPublisher $subject, array $configData): array |
| 57 | + { |
| 58 | + $request = $this->requestFactory->create(); |
| 59 | + $files = (array)$request->getFiles(); |
| 60 | + |
| 61 | + if (!empty($files['groups']['tablerate']['fields']['import']['value']['name'])) { |
| 62 | + $varDir = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT); |
| 63 | + $randomizedName = $this->rand->getRandomString(6) . '_' . |
| 64 | + $configData['groups']['tablerate']['fields']['import']['value']['name']; |
| 65 | + if (!$varDir->getDriver() |
| 66 | + ->copy( |
| 67 | + $files['groups']['tablerate']['fields']['import']['value']['tmp_name'], |
| 68 | + $varDir->getAbsolutePath($randomizedName) |
| 69 | + )) { |
| 70 | + throw new FileSystemException(__('Filesystem is not writable.')); |
| 71 | + } |
| 72 | + |
| 73 | + $configData['groups']['tablerate']['fields']['import']['value']['name'] = $randomizedName; |
| 74 | + $configData['groups']['tablerate']['fields']['import']['value']['full_path'] = $varDir->getAbsolutePath(); |
| 75 | + } |
| 76 | + |
| 77 | + return [$configData]; |
| 78 | + } |
| 79 | +} |
0 commit comments