Skip to content

Commit 369d426

Browse files
authored
ENGCOM-5575: Minimize complexity for customer address import model #23085
2 parents c79be14 + 8a87635 commit 369d426

File tree

1 file changed

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

1 file changed

+123
-91
lines changed

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

Lines changed: 123 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
*
@@ -741,6 +761,7 @@ protected function _saveCustomerDefaults(array $defaults)
741761
{
742762
foreach ($defaults as $tableName => $data) {
743763
foreach ($data as $customerId => $defaultsData) {
764+
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
744765
$data = array_merge(
745766
['entity_id' => $customerId],
746767
$defaultsData
@@ -781,11 +802,13 @@ public function getEntityTypeCode()
781802
*
782803
* @static
783804
* @return array
805+
* phpcs:disable Magento2.Functions.StaticFunction
784806
*/
785807
public static function getDefaultAddressAttributeMapping()
786808
{
787809
return self::$_defaultAddressAttributeMapping;
788810
}
811+
// phpcs:enable
789812

790813
/**
791814
* Check if address for import is empty (for customer composite mode)
@@ -820,7 +843,6 @@ protected function _isOptionalAddressEmpty(array $rowData)
820843
* @param int $rowNumber
821844
* @return void
822845
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
823-
* @SuppressWarnings(PHPMD.NPathComplexity)
824846
*/
825847
protected function _validateRowForUpdate(array $rowData, $rowNumber)
826848
{
@@ -833,38 +855,36 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
833855

834856
if ($customerId === false) {
835857
$this->addRowError(self::ERROR_CUSTOMER_NOT_FOUND, $rowNumber);
858+
} elseif ($this->_checkRowDuplicate($customerId, $addressId)) {
859+
$this->addRowError(self::ERROR_DUPLICATE_PK, $rowNumber);
836860
} 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);
861+
// check simple attributes
862+
foreach ($this->_attributes as $attributeCode => $attributeParams) {
863+
$websiteId = $this->_websiteCodeToId[$website];
864+
$attributeParams = $this->adjustAttributeDataForWebsite($attributeParams, $websiteId);
844865

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-
}
866+
if (in_array($attributeCode, $this->_ignoredAttributes)) {
867+
continue;
868+
} elseif (isset($rowData[$attributeCode]) && strlen($rowData[$attributeCode])) {
869+
$this->isAttributeValid(
870+
$attributeCode,
871+
$attributeParams,
872+
$rowData,
873+
$rowNumber,
874+
$multiSeparator
875+
);
876+
} elseif ($attributeParams['is_required']
877+
&& !$this->addressStorage->doesExist(
878+
(string)$addressId,
879+
(string)$customerId
880+
)
881+
) {
882+
$this->addRowError(self::ERROR_VALUE_IS_REQUIRED, $rowNumber, $attributeCode);
864883
}
884+
}
865885

886+
if (isset($rowData[self::COLUMN_COUNTRY_ID])) {
866887
if (isset($rowData[self::COLUMN_POSTCODE])
867-
&& isset($rowData[self::COLUMN_COUNTRY_ID])
868888
&& !$this->postcodeValidator->isValid(
869889
$rowData[self::COLUMN_COUNTRY_ID],
870890
$rowData[self::COLUMN_POSTCODE]
@@ -873,19 +893,14 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
873893
$this->addRowError(self::ERROR_VALUE_IS_REQUIRED, $rowNumber, self::COLUMN_POSTCODE);
874894
}
875895

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])]
896+
if (isset($rowData[self::COLUMN_REGION])
897+
&& !empty($rowData[self::COLUMN_REGION])
898+
&& false === $this->getCountryRegionId(
899+
$rowData[self::COLUMN_COUNTRY_ID],
900+
$rowData[self::COLUMN_REGION]
885901
)
886-
) {
887-
$this->addRowError(self::ERROR_INVALID_REGION, $rowNumber, self::COLUMN_REGION);
888-
}
902+
) {
903+
$this->addRowError(self::ERROR_INVALID_REGION, $rowNumber, self::COLUMN_REGION);
889904
}
890905
}
891906
}
@@ -909,15 +924,13 @@ protected function _validateRowForDelete(array $rowData, $rowNumber)
909924
$customerId = $this->_getCustomerId($email, $website);
910925
if ($customerId === false) {
911926
$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-
}
927+
} elseif (!strlen($addressId)) {
928+
$this->addRowError(self::ERROR_ADDRESS_ID_IS_EMPTY, $rowNumber);
929+
} elseif (!$this->addressStorage->doesExist(
930+
(string)$addressId,
931+
(string)$customerId
932+
)) {
933+
$this->addRowError(self::ERROR_ADDRESS_NOT_FOUND, $rowNumber);
921934
}
922935
}
923936
}
@@ -931,19 +944,18 @@ protected function _validateRowForDelete(array $rowData, $rowNumber)
931944
*/
932945
protected function _checkRowDuplicate($customerId, $addressId)
933946
{
934-
if ($this->addressStorage->doesExist(
947+
$isAddressExists = $this->addressStorage->doesExist(
935948
(string)$addressId,
936949
(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;
950+
);
951+
952+
$isPkRowSet = isset($this->_importedRowPks[$customerId][$addressId]);
953+
954+
if ($isAddressExists && !$isPkRowSet) {
955+
$this->_importedRowPks[$customerId][$addressId] = true;
946956
}
957+
958+
return $isAddressExists && $isPkRowSet;
947959
}
948960

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

0 commit comments

Comments
 (0)