Skip to content

Commit 01dfa6a

Browse files
committed
ACP2E-2129: customer is reporting their table rates are not updating via CSV any longer
- implemented solution
1 parent 2abc466 commit 01dfa6a

File tree

3 files changed

+171
-18
lines changed

3 files changed

+171
-18
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
if (!empty($files['groups']['tablerate']['fields']['import']['value'])) {
61+
$varDir = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
62+
$randomizedName = $this->rand->getRandomString(6) . '_' .
63+
$configData['groups']['tablerate']['fields']['import']['value']['name'];
64+
if (!$varDir->getDriver()
65+
->copy(
66+
$files['groups']['tablerate']['fields']['import']['value']['tmp_name'],
67+
$varDir->getAbsolutePath($randomizedName)
68+
)) {
69+
throw new FileSystemException(__('Filesystem is not writable.'));
70+
}
71+
72+
$configData['groups']['tablerate']['fields']['import']['value']['name'] = $randomizedName;
73+
$configData['groups']['tablerate']['fields']['import']['value']['full_path'] = $varDir->getAbsolutePath();
74+
}
75+
76+
return [$configData];
77+
}
78+
}

app/code/Magento/OfflineShipping/Model/ResourceModel/Carrier/Tablerate.php

Lines changed: 79 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,22 @@
99
*
1010
* @author Magento Core Team <core@magentocommerce.com>
1111
*/
12+
1213
namespace Magento\OfflineShipping\Model\ResourceModel\Carrier;
1314

15+
use Magento\AsyncConfig\Setup\ConfigOptionsList;
16+
use Magento\Framework\App\Config\ScopeConfigInterface;
17+
use Magento\Framework\App\DeploymentConfig;
18+
use Magento\Framework\App\ObjectManager;
19+
use Magento\Framework\App\RequestFactory;
20+
use Magento\Framework\Exception\FileSystemException;
1421
use Magento\Framework\Filesystem;
22+
use Magento\Framework\Model\ResourceModel\Db\Context;
1523
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\Import;
1624
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\RateQuery;
1725
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\RateQueryFactory;
26+
use Magento\Store\Model\StoreManagerInterface;
27+
use Psr\Log\LoggerInterface;
1828

1929
/**
2030
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -131,28 +141,42 @@ class Tablerate extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
131141
*/
132142
private $rateQueryFactory;
133143

144+
/**
145+
* @var DeploymentConfig
146+
*/
147+
private $deploymentConfig;
148+
149+
/**
150+
* @var RequestFactory
151+
*/
152+
private RequestFactory $requestFactory;
153+
134154
/**
135155
* Tablerate constructor.
136-
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
137-
* @param \Psr\Log\LoggerInterface $logger
138-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig
139-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
156+
* @param Context $context
157+
* @param LoggerInterface $logger
158+
* @param ScopeConfigInterface $coreConfig
159+
* @param StoreManagerInterface $storeManager
140160
* @param \Magento\OfflineShipping\Model\Carrier\Tablerate $carrierTablerate
141161
* @param Filesystem $filesystem
142-
* @param RateQueryFactory $rateQueryFactory
143162
* @param Import $import
163+
* @param RateQueryFactory $rateQueryFactory
144164
* @param null $connectionName
165+
* @param DeploymentConfig|null $deploymentConfig
166+
* @param RequestFactory|null $requestFactory
145167
*/
146168
public function __construct(
147-
\Magento\Framework\Model\ResourceModel\Db\Context $context,
148-
\Psr\Log\LoggerInterface $logger,
169+
\Magento\Framework\Model\ResourceModel\Db\Context $context,
170+
\Psr\Log\LoggerInterface $logger,
149171
\Magento\Framework\App\Config\ScopeConfigInterface $coreConfig,
150-
\Magento\Store\Model\StoreManagerInterface $storeManager,
151-
\Magento\OfflineShipping\Model\Carrier\Tablerate $carrierTablerate,
152-
\Magento\Framework\Filesystem $filesystem,
153-
Import $import,
154-
RateQueryFactory $rateQueryFactory,
155-
$connectionName = null
172+
\Magento\Store\Model\StoreManagerInterface $storeManager,
173+
\Magento\OfflineShipping\Model\Carrier\Tablerate $carrierTablerate,
174+
\Magento\Framework\Filesystem $filesystem,
175+
Import $import,
176+
RateQueryFactory $rateQueryFactory,
177+
$connectionName = null,
178+
?DeploymentConfig $deploymentConfig = null,
179+
?RequestFactory $requestFactory = null
156180
) {
157181
parent::__construct($context, $connectionName);
158182
$this->coreConfig = $coreConfig;
@@ -162,6 +186,8 @@ public function __construct(
162186
$this->filesystem = $filesystem;
163187
$this->import = $import;
164188
$this->rateQueryFactory = $rateQueryFactory;
189+
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
190+
$this->requestFactory = $requestFactory ?: ObjectManager::getInstance()->get(RequestFactory::class);
165191
}
166192

167193
/**
@@ -217,8 +243,8 @@ private function deleteByCondition(array $condition)
217243
/**
218244
* @param array $fields
219245
* @param array $values
220-
* @throws \Magento\Framework\Exception\LocalizedException
221246
* @return void
247+
* @throws \Magento\Framework\Exception\LocalizedException
222248
*/
223249
private function importData(array $fields, array $values)
224250
{
@@ -247,8 +273,8 @@ private function importData(array $fields, array $values)
247273
* Upload table rate file and import data from it
248274
*
249275
* @param \Magento\Framework\DataObject $object
250-
* @throws \Magento\Framework\Exception\LocalizedException
251276
* @return \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate
277+
* @throws \Magento\Framework\Exception\LocalizedException
252278
* @todo: this method should be refactored as soon as updated design will be provided
253279
* @see https://wiki.corp.x.com/display/MCOMS/Magento+Filesystem+Decisions
254280
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
@@ -259,10 +285,25 @@ public function uploadAndImport(\Magento\Framework\DataObject $object)
259285
/**
260286
* @var \Magento\Framework\App\Config\Value $object
261287
*/
262-
if (empty($_FILES['groups']['tmp_name']['tablerate']['fields']['import']['value'])) {
263-
return $this;
288+
if ($this->deploymentConfig->get(ConfigOptionsList::CONFIG_PATH_ASYNC_CONFIG_SAVE)) {
289+
if (
290+
empty($object->getFieldsetData()['import']['name']) ||
291+
empty($object->getFieldsetData()['import']['full_path'])
292+
) {
293+
return $this;
294+
} else {
295+
$filePath = $object->getFieldsetData()['import']['full_path']
296+
. $object->getFieldsetData()['import']['name'];
297+
}
298+
} else {
299+
$request = $this->requestFactory->create();
300+
$files = (array)$request->getFiles();
301+
if (empty($files['groups']['tablerate']['fields']['import']['value'])) {
302+
return $this;
303+
} else {
304+
$filePath = $files['groups']['tablerate']['fields']['import']['value']['tmp_name'];
305+
}
264306
}
265-
$filePath = $_FILES['groups']['tmp_name']['tablerate']['fields']['import']['value'];
266307

267308
$websiteId = $this->storeManager->getWebsite($object->getScopeId())->getId();
268309
$conditionName = $this->getConditionName($object);
@@ -288,6 +329,7 @@ public function uploadAndImport(\Magento\Framework\DataObject $object)
288329
);
289330
} finally {
290331
$file->close();
332+
$this->removeFile($filePath);
291333
}
292334

293335
if ($this->import->hasErrors()) {
@@ -317,6 +359,7 @@ public function getConditionName(\Magento\Framework\DataObject $object)
317359
/**
318360
* @param string $filePath
319361
* @return \Magento\Framework\Filesystem\File\ReadInterface
362+
* @throws FileSystemException
320363
*/
321364
private function getCsvFile($filePath)
322365
{
@@ -329,6 +372,24 @@ private function getCsvFile($filePath)
329372
return $directoryRead->openFile($fileName);
330373
}
331374

375+
/**
376+
* @param string $filePath
377+
* @return bool
378+
*/
379+
private function removeFile(string $filePath): bool
380+
{
381+
$pathInfo = pathinfo($filePath);
382+
$dirName = $pathInfo['dirname'] ?? '';
383+
$fileName = $pathInfo['basename'] ?? '';
384+
385+
try {
386+
$directoryWrite = $this->filesystem->getDirectoryWrite($dirName);
387+
return $directoryWrite->delete($fileName);
388+
} catch (FileSystemException $exception) {
389+
return false;
390+
}
391+
}
392+
332393
/**
333394
* Return import condition full name by condition name code
334395
*
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config>
9+
<type name="Magento\AsyncConfig\Model\AsyncConfigPublisher">
10+
<plugin name="async_config_file_upload_management"
11+
type="Magento\OfflineShipping\Model\Plugin\AsyncConfig\Model\AsyncConfigPublisherPlugin" sortOrder="1"
12+
disabled="false"/>
13+
</type>
14+
</config>

0 commit comments

Comments
 (0)