17
17
use Magento \Framework \App \DeploymentConfig ;
18
18
use Magento \Framework \App \ObjectManager ;
19
19
use Magento \Framework \App \RequestFactory ;
20
+ use Magento \Framework \DataObject ;
20
21
use Magento \Framework \Exception \FileSystemException ;
22
+ use Magento \Framework \Exception \LocalizedException ;
21
23
use Magento \Framework \Filesystem ;
22
24
use Magento \Framework \Model \ResourceModel \Db \Context ;
23
25
use Magento \OfflineShipping \Model \ResourceModel \Carrier \Tablerate \Import ;
24
26
use Magento \OfflineShipping \Model \ResourceModel \Carrier \Tablerate \RateQuery ;
25
27
use Magento \OfflineShipping \Model \ResourceModel \Carrier \Tablerate \RateQueryFactory ;
26
28
use Magento \Store \Model \StoreManagerInterface ;
27
29
use Psr \Log \LoggerInterface ;
30
+ use Magento \Framework \Filesystem \Io \File as IoFile ;
28
31
29
32
/**
30
33
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -118,7 +121,7 @@ class Tablerate extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
118
121
protected $ storeManager ;
119
122
120
123
/**
121
- * @var \Magento\OfflineShipping\Model\ResourceModel\Carrier\ Tablerate
124
+ * @var Tablerate
122
125
* @since 100.1.0
123
126
*/
124
127
protected $ carrierTablerate ;
@@ -151,6 +154,11 @@ class Tablerate extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
151
154
*/
152
155
private RequestFactory $ requestFactory ;
153
156
157
+ /**
158
+ * @var IoFile
159
+ */
160
+ private IoFile $ ioFile ;
161
+
154
162
/**
155
163
* Tablerate constructor.
156
164
* @param Context $context
@@ -161,9 +169,12 @@ class Tablerate extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
161
169
* @param Filesystem $filesystem
162
170
* @param Import $import
163
171
* @param RateQueryFactory $rateQueryFactory
164
- * @param null $connectionName
172
+ * @param string| null $connectionName
165
173
* @param DeploymentConfig|null $deploymentConfig
166
174
* @param RequestFactory|null $requestFactory
175
+ * @param IoFile|null $ioFile
176
+ *
177
+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
167
178
*/
168
179
public function __construct (
169
180
\Magento \Framework \Model \ResourceModel \Db \Context $ context ,
@@ -174,9 +185,10 @@ public function __construct(
174
185
\Magento \Framework \Filesystem $ filesystem ,
175
186
Import $ import ,
176
187
RateQueryFactory $ rateQueryFactory ,
177
- $ connectionName = null ,
188
+ ? string $ connectionName = null ,
178
189
?DeploymentConfig $ deploymentConfig = null ,
179
- ?RequestFactory $ requestFactory = null
190
+ ?RequestFactory $ requestFactory = null ,
191
+ ?IoFile $ ioFile = null
180
192
) {
181
193
parent ::__construct ($ context , $ connectionName );
182
194
$ this ->coreConfig = $ coreConfig ;
@@ -188,6 +200,7 @@ public function __construct(
188
200
$ this ->rateQueryFactory = $ rateQueryFactory ;
189
201
$ this ->deploymentConfig = $ deploymentConfig ?: ObjectManager::getInstance ()->get (DeploymentConfig::class);
190
202
$ this ->requestFactory = $ requestFactory ?: ObjectManager::getInstance ()->get (RequestFactory::class);
203
+ $ this ->ioFile = $ ioFile ?: ObjectManager::getInstance ()->get (IoFile::class);
191
204
}
192
205
193
206
/**
@@ -205,6 +218,7 @@ protected function _construct()
205
218
*
206
219
* @param \Magento\Quote\Model\Quote\Address\RateRequest $request
207
220
* @return array|bool
221
+ * @throws LocalizedException
208
222
*/
209
223
public function getRate (\Magento \Quote \Model \Quote \Address \RateRequest $ request )
210
224
{
@@ -227,9 +241,11 @@ public function getRate(\Magento\Quote\Model\Quote\Address\RateRequest $request)
227
241
}
228
242
229
243
/**
244
+ * Delete elements from database using condition
245
+ *
230
246
* @param array $condition
231
247
* @return $this
232
- * @throws \Magento\Framework\Exception\ LocalizedException
248
+ * @throws LocalizedException
233
249
*/
234
250
private function deleteByCondition (array $ condition )
235
251
{
@@ -241,10 +257,12 @@ private function deleteByCondition(array $condition)
241
257
}
242
258
243
259
/**
260
+ * Insert import data
261
+ *
244
262
* @param array $fields
245
263
* @param array $values
246
264
* @return void
247
- * @throws \Magento\Framework\Exception\ LocalizedException
265
+ * @throws LocalizedException
248
266
*/
249
267
private function importData (array $ fields , array $ values )
250
268
{
@@ -256,13 +274,13 @@ private function importData(array $fields, array $values)
256
274
$ this ->getConnection ()->insertArray ($ this ->getMainTable (), $ fields , $ values );
257
275
$ this ->_importedRows += count ($ values );
258
276
}
259
- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
277
+ } catch (LocalizedException $ e ) {
260
278
$ connection ->rollBack ();
261
- throw new \ Magento \ Framework \ Exception \ LocalizedException (__ ('Unable to import data ' ), $ e );
279
+ throw new LocalizedException (__ ('Unable to import data ' ), $ e );
262
280
} catch (\Exception $ e ) {
263
281
$ connection ->rollBack ();
264
282
$ this ->logger ->critical ($ e );
265
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
283
+ throw new LocalizedException (
266
284
__ ('Something went wrong while importing table rates. ' )
267
285
);
268
286
}
@@ -272,39 +290,15 @@ private function importData(array $fields, array $values)
272
290
/**
273
291
* Upload table rate file and import data from it
274
292
*
275
- * @param \Magento\Framework\ DataObject $object
276
- * @return \Magento\OfflineShipping\Model\ResourceModel\Carrier\ Tablerate
277
- * @throws \Magento\Framework\Exception\ LocalizedException
293
+ * @param DataObject $object
294
+ * @return Tablerate
295
+ * @throws LocalizedException
278
296
* @todo: this method should be refactored as soon as updated design will be provided
279
297
* @see https://wiki.corp.x.com/display/MCOMS/Magento+Filesystem+Decisions
280
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
281
- * @SuppressWarnings(PHPMD.NPathComplexity)
282
298
*/
283
- public function uploadAndImport (\ Magento \ Framework \ DataObject $ object )
299
+ public function uploadAndImport (DataObject $ object ): Tablerate
284
300
{
285
- /**
286
- * @var \Magento\Framework\App\Config\Value $object
287
- */
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
-
302
- if (empty ($ files ['groups ' ]['tablerate ' ]['fields ' ]['import ' ]['value ' ])) {
303
- return $ this ;
304
- } else {
305
- $ filePath = $ files ['groups ' ]['tablerate ' ]['fields ' ]['import ' ]['value ' ]['tmp_name ' ];
306
- }
307
- }
301
+ $ filePath = $ this ->getFilePath ($ object );
308
302
if (!$ filePath ) {
309
303
return $ this ;
310
304
}
@@ -313,7 +307,6 @@ public function uploadAndImport(\Magento\Framework\DataObject $object)
313
307
$ conditionName = $ this ->getConditionName ($ object );
314
308
$ file = $ this ->getCsvFile ($ filePath );
315
309
try {
316
- // delete old data by website and condition name
317
310
$ condition = [
318
311
'website_id = ? ' => $ websiteId ,
319
312
'condition_name = ? ' => $ conditionName ,
@@ -327,7 +320,7 @@ public function uploadAndImport(\Magento\Framework\DataObject $object)
327
320
}
328
321
} catch (\Exception $ e ) {
329
322
$ this ->logger ->critical ($ e );
330
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
323
+ throw new LocalizedException (
331
324
__ ('Something went wrong while importing table rates. ' )
332
325
);
333
326
} finally {
@@ -340,16 +333,20 @@ public function uploadAndImport(\Magento\Framework\DataObject $object)
340
333
'We couldn \'t import this file because of these errors: %1 ' ,
341
334
implode (" \n" , $ this ->import ->getErrors ())
342
335
);
343
- throw new \ Magento \ Framework \ Exception \ LocalizedException ($ error );
336
+ throw new LocalizedException ($ error );
344
337
}
338
+
339
+ return $ this ;
345
340
}
346
341
347
342
/**
348
- * @param \Magento\Framework\DataObject $object
343
+ * Extract condition name
344
+ *
345
+ * @param DataObject $object
349
346
* @return mixed|string
350
347
* @since 100.1.0
351
348
*/
352
- public function getConditionName (\ Magento \ Framework \ DataObject $ object )
349
+ public function getConditionName (DataObject $ object )
353
350
{
354
351
if ($ object ->getData ('groups/tablerate/fields/condition_name/inherit ' ) == '1 ' ) {
355
352
$ conditionName = (string )$ this ->coreConfig ->getValue ('carriers/tablerate/condition_name ' , 'default ' );
@@ -360,13 +357,49 @@ public function getConditionName(\Magento\Framework\DataObject $object)
360
357
}
361
358
362
359
/**
360
+ * Determine table rate upload file path
361
+ *
362
+ * @param DataObject $object
363
+ * @return string
364
+ * @throws FileSystemException
365
+ * @throws \Magento\Framework\Exception\RuntimeException
366
+ */
367
+ private function getFilePath (DataObject $ object ): string
368
+ {
369
+ $ filePath = '' ;
370
+
371
+ /**
372
+ * @var \Magento\Framework\App\Config\Value $object
373
+ */
374
+ if ($ this ->deploymentConfig ->get (ConfigOptionsList::CONFIG_PATH_ASYNC_CONFIG_SAVE )) {
375
+ if (!empty ($ object ->getFieldsetData ()['import ' ]['name ' ]) &&
376
+ !empty ($ object ->getFieldsetData ()['import ' ]['full_path ' ])
377
+ ) {
378
+ $ filePath = $ object ->getFieldsetData ()['import ' ]['full_path ' ]
379
+ . $ object ->getFieldsetData ()['import ' ]['name ' ];
380
+ }
381
+ } else {
382
+ $ request = $ this ->requestFactory ->create ();
383
+ $ files = (array )$ request ->getFiles ();
384
+
385
+ if (!empty ($ files ['groups ' ]['tablerate ' ]['fields ' ]['import ' ]['value ' ])) {
386
+ $ filePath = $ files ['groups ' ]['tablerate ' ]['fields ' ]['import ' ]['value ' ]['tmp_name ' ];
387
+ }
388
+ }
389
+
390
+ return $ filePath ;
391
+ }
392
+
393
+ /**
394
+ * Open CSV file for reading
395
+ *
363
396
* @param string $filePath
364
397
* @return \Magento\Framework\Filesystem\File\ReadInterface
365
398
* @throws FileSystemException
366
399
*/
367
400
private function getCsvFile ($ filePath )
368
401
{
369
- $ pathInfo = pathinfo ($ filePath );
402
+ $ pathInfo = $ this -> ioFile -> getPathInfo ($ filePath );
370
403
$ dirName = $ pathInfo ['dirname ' ] ?? '' ;
371
404
$ fileName = $ pathInfo ['basename ' ] ?? '' ;
372
405
@@ -376,12 +409,14 @@ private function getCsvFile($filePath)
376
409
}
377
410
378
411
/**
412
+ * Remove file
413
+ *
379
414
* @param string $filePath
380
415
* @return bool
381
416
*/
382
417
private function removeFile (string $ filePath ): bool
383
418
{
384
- $ pathInfo = pathinfo ($ filePath );
419
+ $ pathInfo = $ this -> ioFile -> getPathInfo ($ filePath );
385
420
$ dirName = $ pathInfo ['dirname ' ] ?? '' ;
386
421
$ fileName = $ pathInfo ['basename ' ] ?? '' ;
387
422
@@ -398,6 +433,7 @@ private function removeFile(string $filePath): bool
398
433
*
399
434
* @param string $conditionName
400
435
* @return string
436
+ * @throws LocalizedException
401
437
*/
402
438
protected function _getConditionFullName ($ conditionName )
403
439
{
@@ -413,7 +449,8 @@ protected function _getConditionFullName($conditionName)
413
449
* Save import data batch
414
450
*
415
451
* @param array $data
416
- * @return \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate
452
+ * @return Tablerate
453
+ * @throws LocalizedException
417
454
*/
418
455
protected function _saveImportData (array $ data )
419
456
{
0 commit comments