9
9
*
10
10
* @author Magento Core Team <core@magentocommerce.com>
11
11
*/
12
+
12
13
namespace Magento \OfflineShipping \Model \ResourceModel \Carrier ;
13
14
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 ;
14
21
use Magento \Framework \Filesystem ;
22
+ use Magento \Framework \Model \ResourceModel \Db \Context ;
15
23
use Magento \OfflineShipping \Model \ResourceModel \Carrier \Tablerate \Import ;
16
24
use Magento \OfflineShipping \Model \ResourceModel \Carrier \Tablerate \RateQuery ;
17
25
use Magento \OfflineShipping \Model \ResourceModel \Carrier \Tablerate \RateQueryFactory ;
26
+ use Magento \Store \Model \StoreManagerInterface ;
27
+ use Psr \Log \LoggerInterface ;
18
28
19
29
/**
20
30
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -131,28 +141,42 @@ class Tablerate extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
131
141
*/
132
142
private $ rateQueryFactory ;
133
143
144
+ /**
145
+ * @var DeploymentConfig
146
+ */
147
+ private $ deploymentConfig ;
148
+
149
+ /**
150
+ * @var RequestFactory
151
+ */
152
+ private RequestFactory $ requestFactory ;
153
+
134
154
/**
135
155
* 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
140
160
* @param \Magento\OfflineShipping\Model\Carrier\Tablerate $carrierTablerate
141
161
* @param Filesystem $filesystem
142
- * @param RateQueryFactory $rateQueryFactory
143
162
* @param Import $import
163
+ * @param RateQueryFactory $rateQueryFactory
144
164
* @param null $connectionName
165
+ * @param DeploymentConfig|null $deploymentConfig
166
+ * @param RequestFactory|null $requestFactory
145
167
*/
146
168
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 ,
149
171
\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
156
180
) {
157
181
parent ::__construct ($ context , $ connectionName );
158
182
$ this ->coreConfig = $ coreConfig ;
@@ -162,6 +186,8 @@ public function __construct(
162
186
$ this ->filesystem = $ filesystem ;
163
187
$ this ->import = $ import ;
164
188
$ this ->rateQueryFactory = $ rateQueryFactory ;
189
+ $ this ->deploymentConfig = $ deploymentConfig ?: ObjectManager::getInstance ()->get (DeploymentConfig::class);
190
+ $ this ->requestFactory = $ requestFactory ?: ObjectManager::getInstance ()->get (RequestFactory::class);
165
191
}
166
192
167
193
/**
@@ -217,8 +243,8 @@ private function deleteByCondition(array $condition)
217
243
/**
218
244
* @param array $fields
219
245
* @param array $values
220
- * @throws \Magento\Framework\Exception\LocalizedException
221
246
* @return void
247
+ * @throws \Magento\Framework\Exception\LocalizedException
222
248
*/
223
249
private function importData (array $ fields , array $ values )
224
250
{
@@ -247,8 +273,8 @@ private function importData(array $fields, array $values)
247
273
* Upload table rate file and import data from it
248
274
*
249
275
* @param \Magento\Framework\DataObject $object
250
- * @throws \Magento\Framework\Exception\LocalizedException
251
276
* @return \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate
277
+ * @throws \Magento\Framework\Exception\LocalizedException
252
278
* @todo: this method should be refactored as soon as updated design will be provided
253
279
* @see https://wiki.corp.x.com/display/MCOMS/Magento+Filesystem+Decisions
254
280
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
@@ -259,10 +285,25 @@ public function uploadAndImport(\Magento\Framework\DataObject $object)
259
285
/**
260
286
* @var \Magento\Framework\App\Config\Value $object
261
287
*/
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
+ }
264
306
}
265
- $ filePath = $ _FILES ['groups ' ]['tmp_name ' ]['tablerate ' ]['fields ' ]['import ' ]['value ' ];
266
307
267
308
$ websiteId = $ this ->storeManager ->getWebsite ($ object ->getScopeId ())->getId ();
268
309
$ conditionName = $ this ->getConditionName ($ object );
@@ -288,6 +329,7 @@ public function uploadAndImport(\Magento\Framework\DataObject $object)
288
329
);
289
330
} finally {
290
331
$ file ->close ();
332
+ $ this ->removeFile ($ filePath );
291
333
}
292
334
293
335
if ($ this ->import ->hasErrors ()) {
@@ -317,6 +359,7 @@ public function getConditionName(\Magento\Framework\DataObject $object)
317
359
/**
318
360
* @param string $filePath
319
361
* @return \Magento\Framework\Filesystem\File\ReadInterface
362
+ * @throws FileSystemException
320
363
*/
321
364
private function getCsvFile ($ filePath )
322
365
{
@@ -329,6 +372,24 @@ private function getCsvFile($filePath)
329
372
return $ directoryRead ->openFile ($ fileName );
330
373
}
331
374
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
+
332
393
/**
333
394
* Return import condition full name by condition name code
334
395
*
0 commit comments