Skip to content

Commit 062266e

Browse files
author
Oleksandr Dubovyk
committed
MAGETWO-66066: Remove usages of unserialize in module Magento/ImportExport
- Refactor as getter
1 parent 66b4dd2 commit 062266e

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

app/code/Magento/ImportExport/Model/Import/AbstractEntity.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ abstract class AbstractEntity
278278
*
279279
* @var Json
280280
*/
281-
private $json;
281+
private $serializer;
282282

283283
/**
284284
* @param \Magento\Framework\Stdlib\StringUtils $string
@@ -298,11 +298,9 @@ public function __construct(
298298
\Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper,
299299
ResourceConnection $resource,
300300
ProcessingErrorAggregatorInterface $errorAggregator,
301-
array $data = [],
302-
Json $json = null
301+
array $data = []
303302
) {
304303
$this->_scopeConfig = $scopeConfig;
305-
$this->json = $json ?: ObjectManager::getInstance()->get(Json::class);
306304
$this->_dataSourceModel = isset(
307305
$data['data_source_model']
308306
) ? $data['data_source_model'] : $importFactory->create()->getDataSourceModel();
@@ -459,7 +457,7 @@ protected function _saveValidatedBunches()
459457
foreach ($entityGroup as $key => $value) {
460458
$bunchRows[$key] = $value;
461459
}
462-
$productDataSize = strlen($this->json->serialize($bunchRows));
460+
$productDataSize = strlen($this->getSerializer()->serialize($bunchRows));
463461

464462
/* Check if the new bunch should be started */
465463
$isBunchSizeExceeded = ($this->_bunchSize > 0 && count($bunchRows) >= $this->_bunchSize);
@@ -485,6 +483,22 @@ protected function _saveValidatedBunches()
485483
return $this;
486484
}
487485

486+
/**
487+
* Get Serializer instance
488+
*
489+
* Workaround. Only way to implement dependency and not to break inherited child classes
490+
*
491+
* @return Json
492+
* @deprecated
493+
*/
494+
protected function getSerializer()
495+
{
496+
if (null === $this->serializer) {
497+
$this->serializer = ObjectManager::getInstance()->get(Json::class);
498+
}
499+
return $this->serializer;
500+
}
501+
488502
/**
489503
* Add error with corresponding current data source row number.
490504
*

app/code/Magento/ImportExport/Model/Import/Entity/AbstractEntity.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ abstract class AbstractEntity
253253
*
254254
* @var Json
255255
*/
256-
private $json;
256+
protected $serializer;
257257

258258
/**
259259
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
@@ -264,7 +264,6 @@ abstract class AbstractEntity
264264
* @param \Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper
265265
* @param \Magento\Framework\Stdlib\StringUtils $string
266266
* @param ProcessingErrorAggregatorInterface $errorAggregator
267-
* @param Json|null $json
268267
* @throws \Magento\Framework\Exception\LocalizedException
269268
*/
270269
public function __construct(
@@ -275,15 +274,13 @@ public function __construct(
275274
ResourceConnection $resource,
276275
\Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper,
277276
\Magento\Framework\Stdlib\StringUtils $string,
278-
ProcessingErrorAggregatorInterface $errorAggregator,
279-
Json $json = null
277+
ProcessingErrorAggregatorInterface $errorAggregator
280278
) {
281279
$this->jsonHelper = $jsonHelper;
282280
$this->_importExportData = $importExportData;
283281
$this->_resourceHelper = $resourceHelper;
284282
$this->string = $string;
285283
$this->errorAggregator = $errorAggregator;
286-
$this->json = $json ?: ObjectManager::getInstance()->get(Json::class);
287284

288285
foreach ($this->errorMessageTemplates as $errorCode => $message) {
289286
$this->getErrorAggregator()->addErrorMessageTemplate($errorCode, $message);
@@ -394,7 +391,7 @@ protected function _saveValidatedBunches()
394391
$this->_dataSourceModel->saveBunch($this->getEntityTypeCode(), $this->getBehavior(), $bunchRows);
395392

396393
$bunchRows = $nextRowBackup;
397-
$currentDataSize = strlen($this->json->serialize($bunchRows));
394+
$currentDataSize = strlen($this->getSerializer()->serialize($bunchRows));
398395
$startNewBunch = false;
399396
$nextRowBackup = [];
400397
}
@@ -431,6 +428,22 @@ protected function _saveValidatedBunches()
431428
return $this;
432429
}
433430

431+
/**
432+
* Get Serializer instance
433+
*
434+
* Workaround. Only way to implement dependency and not to break inherited child classes
435+
*
436+
* @return Json
437+
* @deprecated
438+
*/
439+
protected function getSerializer()
440+
{
441+
if (null === $this->serializer) {
442+
$this->serializer = ObjectManager::getInstance()->get(Json::class);
443+
}
444+
return $this->serializer;
445+
}
446+
434447
/**
435448
* Add error with corresponding current data source row number.
436449
*

dev/tests/integration/testsuite/Magento/ImportExport/Model/Import/EntityAbstractTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public function testSaveValidatedBunches()
4343
$objectManager->get(\Magento\Framework\App\ResourceConnection::class),
4444
$objectManager->get(
4545
\Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface::class
46-
),
47-
[],
48-
$objectManager->get(\Magento\Framework\Serialize\Serializer\Json::class)
46+
)
4947
],
5048
'',
5149
true,

0 commit comments

Comments
 (0)