Skip to content

Commit f735a92

Browse files
Minimize nPath Complexity for _validateRowForUpdate()
Reduce Cyclomatic & NPath complexity for _prepareDataForUpdate() Move Row Data formatting to a new method Remove empty ElseIf statement Remove excessive Else-If branching Utilize an existing value to get entity table name Marked Unused fields as deprecated Breaking longer line into multiples to fix static tests failure Eliminate IfElse branching on _checkRowDuplicate()
1 parent b81986b commit f735a92

File tree

1 file changed

+120
-91
lines changed
  • app/code/Magento/CustomerImportExport/Model/Import

1 file changed

+120
-91
lines changed

app/code/Magento/CustomerImportExport/Model/Import/Address.php

Lines changed: 120 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ class Address extends AbstractCustomer
165165
* Array of region parameters
166166
*
167167
* @var array
168+
* @deprecated field not in use
168169
*/
169170
protected $_regionParameters;
170171

@@ -194,16 +195,19 @@ class Address extends AbstractCustomer
194195

195196
/**
196197
* @var \Magento\Eav\Model\Config
198+
* @deprecated field not-in use
197199
*/
198200
protected $_eavConfig;
199201

200202
/**
201203
* @var \Magento\Customer\Model\AddressFactory
204+
* @deprecated not utilized anymore
202205
*/
203206
protected $_addressFactory;
204207

205208
/**
206209
* @var \Magento\Framework\Stdlib\DateTime
210+
* @deprecated the property isn't used
207211
*/
208212
protected $dateTime;
209213

@@ -419,10 +423,7 @@ protected function _getCustomerEntity()
419423
protected function _getNextEntityId()
420424
{
421425
if (!$this->_nextEntityId) {
422-
/** @var $addressResource \Magento\Customer\Model\ResourceModel\Address */
423-
$addressResource = $this->_addressFactory->create()->getResource();
424-
$addressTable = $addressResource->getEntityTable();
425-
$this->_nextEntityId = $this->_resourceHelper->getNextAutoincrement($addressTable);
426+
$this->_nextEntityId = $this->_resourceHelper->getNextAutoincrement($this->_entityTable);
426427
}
427428
return $this->_nextEntityId++;
428429
}
@@ -587,7 +588,6 @@ protected function _mergeEntityAttributes(array $newAttributes, array $attribute
587588
*/
588589
protected function _prepareDataForUpdate(array $rowData):array
589590
{
590-
$multiSeparator = $this->getMultipleValueSeparator();
591591
$email = strtolower($rowData[self::COLUMN_EMAIL]);
592592
$customerId = $this->_getCustomerId($email, $rowData[self::COLUMN_WEBSITE]);
593593
// entity table data
@@ -621,27 +621,18 @@ protected function _prepareDataForUpdate(array $rowData):array
621621
if (array_key_exists($attributeAlias, $rowData)) {
622622
$attributeParams = $this->adjustAttributeDataForWebsite($attributeParams, $websiteId);
623623

624+
$value = $rowData[$attributeAlias];
625+
624626
if (!strlen($rowData[$attributeAlias])) {
625-
if ($newAddress) {
626-
$value = null;
627-
} else {
627+
if (!$newAddress) {
628628
continue;
629629
}
630-
} elseif ($newAddress && !strlen($rowData[$attributeAlias])) {
631-
} elseif (in_array($attributeParams['type'], ['select', 'boolean'])) {
632-
$value = $this->getSelectAttrIdByValue($attributeParams, mb_strtolower($rowData[$attributeAlias]));
633-
} elseif ('datetime' == $attributeParams['type']) {
634-
$value = (new \DateTime())->setTimestamp(strtotime($rowData[$attributeAlias]));
635-
$value = $value->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
636-
} elseif ('multiselect' == $attributeParams['type']) {
637-
$ids = [];
638-
foreach (explode($multiSeparator, mb_strtolower($rowData[$attributeAlias])) as $subValue) {
639-
$ids[] = $this->getSelectAttrIdByValue($attributeParams, $subValue);
640-
}
641-
$value = implode(',', $ids);
642-
} else {
643-
$value = $rowData[$attributeAlias];
630+
631+
$value = null;
632+
} elseif (in_array($attributeParams['type'], ['select', 'boolean', 'datetime', 'multiselect'])) {
633+
$value = $this->getValueByAttributeType($rowData[$attributeAlias], $attributeParams);
644634
}
635+
645636
if ($attributeParams['is_static']) {
646637
$entityRow[$attributeAlias] = $value;
647638
} else {
@@ -651,22 +642,18 @@ protected function _prepareDataForUpdate(array $rowData):array
651642
}
652643
foreach (self::getDefaultAddressAttributeMapping() as $columnName => $attributeCode) {
653644
if (!empty($rowData[$columnName])) {
654-
/** @var $attribute \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */
655645
$table = $this->_getCustomerEntity()->getResource()->getTable('customer_entity');
656646
$defaults[$table][$customerId][$attributeCode] = $addressId;
657647
}
658648
}
659649
// let's try to find region ID
660650
$entityRow['region_id'] = null;
661-
if (!empty($rowData[self::COLUMN_REGION])) {
662-
$countryNormalized = strtolower($rowData[self::COLUMN_COUNTRY_ID]);
663-
$regionNormalized = strtolower($rowData[self::COLUMN_REGION]);
664-
665-
if (isset($this->_countryRegions[$countryNormalized][$regionNormalized])) {
666-
$regionId = $this->_countryRegions[$countryNormalized][$regionNormalized];
667-
$entityRow[self::COLUMN_REGION] = $this->_regions[$regionId];
668-
$entityRow['region_id'] = $regionId;
669-
}
651+
652+
if (!empty($rowData[self::COLUMN_REGION])
653+
&& $this->getCountryRegionId($rowData[self::COLUMN_COUNTRY_ID], $rowData[self::COLUMN_REGION]) !== false) {
654+
$regionId = $this->getCountryRegionId($rowData[self::COLUMN_COUNTRY_ID], $rowData[self::COLUMN_REGION]);
655+
$entityRow[self::COLUMN_REGION] = $this->_regions[$regionId];
656+
$entityRow['region_id'] = $regionId;
670657
}
671658
if ($newAddress) {
672659
$entityRowNew = $entityRow;
@@ -684,6 +671,39 @@ protected function _prepareDataForUpdate(array $rowData):array
684671
];
685672
}
686673

674+
/**
675+
* Process row data, based on attirbute type
676+
*
677+
* @param string $rowAttributeData
678+
* @param array $attributeParams
679+
* @return \DateTime|int|string
680+
* @throws \Exception
681+
*/
682+
protected function getValueByAttributeType(string $rowAttributeData, array $attributeParams)
683+
{
684+
$multiSeparator = $this->getMultipleValueSeparator();
685+
$value = $rowAttributeData;
686+
switch ($attributeParams['type']) {
687+
case 'select':
688+
case 'boolean':
689+
$value = $this->getSelectAttrIdByValue($attributeParams, mb_strtolower($rowAttributeData));
690+
break;
691+
case 'datetime':
692+
$value = (new \DateTime())->setTimestamp(strtotime($rowAttributeData));
693+
$value = $value->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
694+
break;
695+
case 'multiselect':
696+
$ids = [];
697+
foreach (explode($multiSeparator, mb_strtolower($rowAttributeData)) as $subValue) {
698+
$ids[] = $this->getSelectAttrIdByValue($attributeParams, $subValue);
699+
}
700+
$value = implode(',', $ids);
701+
break;
702+
}
703+
704+
return $value;
705+
}
706+
687707
/**
688708
* Update and insert data in entity table
689709
*
@@ -820,7 +840,6 @@ protected function _isOptionalAddressEmpty(array $rowData)
820840
* @param int $rowNumber
821841
* @return void
822842
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
823-
* @SuppressWarnings(PHPMD.NPathComplexity)
824843
*/
825844
protected function _validateRowForUpdate(array $rowData, $rowNumber)
826845
{
@@ -833,38 +852,36 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
833852

834853
if ($customerId === false) {
835854
$this->addRowError(self::ERROR_CUSTOMER_NOT_FOUND, $rowNumber);
855+
} elseif ($this->_checkRowDuplicate($customerId, $addressId)) {
856+
$this->addRowError(self::ERROR_DUPLICATE_PK, $rowNumber);
836857
} else {
837-
if ($this->_checkRowDuplicate($customerId, $addressId)) {
838-
$this->addRowError(self::ERROR_DUPLICATE_PK, $rowNumber);
839-
} else {
840-
// check simple attributes
841-
foreach ($this->_attributes as $attributeCode => $attributeParams) {
842-
$websiteId = $this->_websiteCodeToId[$website];
843-
$attributeParams = $this->adjustAttributeDataForWebsite($attributeParams, $websiteId);
858+
// check simple attributes
859+
foreach ($this->_attributes as $attributeCode => $attributeParams) {
860+
$websiteId = $this->_websiteCodeToId[$website];
861+
$attributeParams = $this->adjustAttributeDataForWebsite($attributeParams, $websiteId);
844862

845-
if (in_array($attributeCode, $this->_ignoredAttributes)) {
846-
continue;
847-
}
848-
if (isset($rowData[$attributeCode]) && strlen($rowData[$attributeCode])) {
849-
$this->isAttributeValid(
850-
$attributeCode,
851-
$attributeParams,
852-
$rowData,
853-
$rowNumber,
854-
$multiSeparator
855-
);
856-
} elseif ($attributeParams['is_required']
857-
&& !$this->addressStorage->doesExist(
858-
(string)$addressId,
859-
(string)$customerId
860-
)
861-
) {
862-
$this->addRowError(self::ERROR_VALUE_IS_REQUIRED, $rowNumber, $attributeCode);
863-
}
863+
if (in_array($attributeCode, $this->_ignoredAttributes)) {
864+
continue;
865+
} elseif (isset($rowData[$attributeCode]) && strlen($rowData[$attributeCode])) {
866+
$this->isAttributeValid(
867+
$attributeCode,
868+
$attributeParams,
869+
$rowData,
870+
$rowNumber,
871+
$multiSeparator
872+
);
873+
} elseif ($attributeParams['is_required']
874+
&& !$this->addressStorage->doesExist(
875+
(string)$addressId,
876+
(string)$customerId
877+
)
878+
) {
879+
$this->addRowError(self::ERROR_VALUE_IS_REQUIRED, $rowNumber, $attributeCode);
864880
}
881+
}
865882

883+
if (isset($rowData[self::COLUMN_COUNTRY_ID])) {
866884
if (isset($rowData[self::COLUMN_POSTCODE])
867-
&& isset($rowData[self::COLUMN_COUNTRY_ID])
868885
&& !$this->postcodeValidator->isValid(
869886
$rowData[self::COLUMN_COUNTRY_ID],
870887
$rowData[self::COLUMN_POSTCODE]
@@ -873,19 +890,14 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
873890
$this->addRowError(self::ERROR_VALUE_IS_REQUIRED, $rowNumber, self::COLUMN_POSTCODE);
874891
}
875892

876-
if (isset($rowData[self::COLUMN_COUNTRY_ID]) && isset($rowData[self::COLUMN_REGION])) {
877-
$countryRegions = isset(
878-
$this->_countryRegions[strtolower($rowData[self::COLUMN_COUNTRY_ID])]
879-
) ? $this->_countryRegions[strtolower(
880-
$rowData[self::COLUMN_COUNTRY_ID]
881-
)] : [];
882-
883-
if (!empty($rowData[self::COLUMN_REGION]) && !empty($countryRegions) && !isset(
884-
$countryRegions[strtolower($rowData[self::COLUMN_REGION])]
893+
if (isset($rowData[self::COLUMN_REGION])
894+
&& !empty($rowData[self::COLUMN_REGION])
895+
&& false === $this->getCountryRegionId(
896+
$rowData[self::COLUMN_COUNTRY_ID],
897+
$rowData[self::COLUMN_REGION]
885898
)
886-
) {
887-
$this->addRowError(self::ERROR_INVALID_REGION, $rowNumber, self::COLUMN_REGION);
888-
}
899+
) {
900+
$this->addRowError(self::ERROR_INVALID_REGION, $rowNumber, self::COLUMN_REGION);
889901
}
890902
}
891903
}
@@ -909,15 +921,13 @@ protected function _validateRowForDelete(array $rowData, $rowNumber)
909921
$customerId = $this->_getCustomerId($email, $website);
910922
if ($customerId === false) {
911923
$this->addRowError(self::ERROR_CUSTOMER_NOT_FOUND, $rowNumber);
912-
} else {
913-
if (!strlen($addressId)) {
914-
$this->addRowError(self::ERROR_ADDRESS_ID_IS_EMPTY, $rowNumber);
915-
} elseif (!$this->addressStorage->doesExist(
916-
(string)$addressId,
917-
(string)$customerId
918-
)) {
919-
$this->addRowError(self::ERROR_ADDRESS_NOT_FOUND, $rowNumber);
920-
}
924+
} elseif (!strlen($addressId)) {
925+
$this->addRowError(self::ERROR_ADDRESS_ID_IS_EMPTY, $rowNumber);
926+
} elseif (!$this->addressStorage->doesExist(
927+
(string)$addressId,
928+
(string)$customerId
929+
)) {
930+
$this->addRowError(self::ERROR_ADDRESS_NOT_FOUND, $rowNumber);
921931
}
922932
}
923933
}
@@ -931,19 +941,18 @@ protected function _validateRowForDelete(array $rowData, $rowNumber)
931941
*/
932942
protected function _checkRowDuplicate($customerId, $addressId)
933943
{
934-
if ($this->addressStorage->doesExist(
944+
$isAddressExists = $this->addressStorage->doesExist(
935945
(string)$addressId,
936946
(string)$customerId
937-
)) {
938-
if (!isset($this->_importedRowPks[$customerId][$addressId])) {
939-
$this->_importedRowPks[$customerId][$addressId] = true;
940-
return false;
941-
} else {
942-
return true;
943-
}
944-
} else {
945-
return false;
947+
);
948+
949+
$isPkRowSet = isset($this->_importedRowPks[$customerId][$addressId]);
950+
951+
if ($isAddressExists && !$isPkRowSet) {
952+
$this->_importedRowPks[$customerId][$addressId] = true;
946953
}
954+
955+
return $isAddressExists && $isPkRowSet;
947956
}
948957

949958
/**
@@ -957,4 +966,24 @@ public function setCustomerAttributes($customerAttributes)
957966
$this->_customerAttributes = $customerAttributes;
958967
return $this;
959968
}
969+
970+
/**
971+
* Get RegionID from the initialized data
972+
*
973+
* @param string $countryId
974+
* @param string $region
975+
* @return bool|int
976+
*/
977+
private function getCountryRegionId(string $countryId, string $region)
978+
{
979+
$countryNormalized = strtolower($countryId);
980+
$regionNormalized = strtolower($region);
981+
982+
if (isset($this->_countryRegions[$countryNormalized])
983+
&& isset($this->_countryRegions[$countryNormalized][$regionNormalized])) {
984+
return $this->_countryRegions[$countryNormalized][$regionNormalized];
985+
}
986+
987+
return false;
988+
}
960989
}

0 commit comments

Comments
 (0)