@@ -165,6 +165,7 @@ class Address extends AbstractCustomer
165
165
* Array of region parameters
166
166
*
167
167
* @var array
168
+ * @deprecated field not in use
168
169
*/
169
170
protected $ _regionParameters ;
170
171
@@ -194,16 +195,19 @@ class Address extends AbstractCustomer
194
195
195
196
/**
196
197
* @var \Magento\Eav\Model\Config
198
+ * @deprecated field not-in use
197
199
*/
198
200
protected $ _eavConfig ;
199
201
200
202
/**
201
203
* @var \Magento\Customer\Model\AddressFactory
204
+ * @deprecated not utilized anymore
202
205
*/
203
206
protected $ _addressFactory ;
204
207
205
208
/**
206
209
* @var \Magento\Framework\Stdlib\DateTime
210
+ * @deprecated the property isn't used
207
211
*/
208
212
protected $ dateTime ;
209
213
@@ -419,10 +423,7 @@ protected function _getCustomerEntity()
419
423
protected function _getNextEntityId ()
420
424
{
421
425
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 );
426
427
}
427
428
return $ this ->_nextEntityId ++;
428
429
}
@@ -587,7 +588,6 @@ protected function _mergeEntityAttributes(array $newAttributes, array $attribute
587
588
*/
588
589
protected function _prepareDataForUpdate (array $ rowData ):array
589
590
{
590
- $ multiSeparator = $ this ->getMultipleValueSeparator ();
591
591
$ email = strtolower ($ rowData [self ::COLUMN_EMAIL ]);
592
592
$ customerId = $ this ->_getCustomerId ($ email , $ rowData [self ::COLUMN_WEBSITE ]);
593
593
// entity table data
@@ -621,27 +621,18 @@ protected function _prepareDataForUpdate(array $rowData):array
621
621
if (array_key_exists ($ attributeAlias , $ rowData )) {
622
622
$ attributeParams = $ this ->adjustAttributeDataForWebsite ($ attributeParams , $ websiteId );
623
623
624
+ $ value = $ rowData [$ attributeAlias ];
625
+
624
626
if (!strlen ($ rowData [$ attributeAlias ])) {
625
- if ($ newAddress ) {
626
- $ value = null ;
627
- } else {
627
+ if (!$ newAddress ) {
628
628
continue ;
629
629
}
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 );
644
634
}
635
+
645
636
if ($ attributeParams ['is_static ' ]) {
646
637
$ entityRow [$ attributeAlias ] = $ value ;
647
638
} else {
@@ -651,22 +642,18 @@ protected function _prepareDataForUpdate(array $rowData):array
651
642
}
652
643
foreach (self ::getDefaultAddressAttributeMapping () as $ columnName => $ attributeCode ) {
653
644
if (!empty ($ rowData [$ columnName ])) {
654
- /** @var $attribute \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */
655
645
$ table = $ this ->_getCustomerEntity ()->getResource ()->getTable ('customer_entity ' );
656
646
$ defaults [$ table ][$ customerId ][$ attributeCode ] = $ addressId ;
657
647
}
658
648
}
659
649
// let's try to find region ID
660
650
$ 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 ;
670
657
}
671
658
if ($ newAddress ) {
672
659
$ entityRowNew = $ entityRow ;
@@ -684,6 +671,39 @@ protected function _prepareDataForUpdate(array $rowData):array
684
671
];
685
672
}
686
673
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
+
687
707
/**
688
708
* Update and insert data in entity table
689
709
*
@@ -820,7 +840,6 @@ protected function _isOptionalAddressEmpty(array $rowData)
820
840
* @param int $rowNumber
821
841
* @return void
822
842
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
823
- * @SuppressWarnings(PHPMD.NPathComplexity)
824
843
*/
825
844
protected function _validateRowForUpdate (array $ rowData , $ rowNumber )
826
845
{
@@ -833,38 +852,36 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
833
852
834
853
if ($ customerId === false ) {
835
854
$ this ->addRowError (self ::ERROR_CUSTOMER_NOT_FOUND , $ rowNumber );
855
+ } elseif ($ this ->_checkRowDuplicate ($ customerId , $ addressId )) {
856
+ $ this ->addRowError (self ::ERROR_DUPLICATE_PK , $ rowNumber );
836
857
} 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 );
844
862
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 );
864
880
}
881
+ }
865
882
883
+ if (isset ($ rowData [self ::COLUMN_COUNTRY_ID ])) {
866
884
if (isset ($ rowData [self ::COLUMN_POSTCODE ])
867
- && isset ($ rowData [self ::COLUMN_COUNTRY_ID ])
868
885
&& !$ this ->postcodeValidator ->isValid (
869
886
$ rowData [self ::COLUMN_COUNTRY_ID ],
870
887
$ rowData [self ::COLUMN_POSTCODE ]
@@ -873,19 +890,14 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
873
890
$ this ->addRowError (self ::ERROR_VALUE_IS_REQUIRED , $ rowNumber , self ::COLUMN_POSTCODE );
874
891
}
875
892
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 ]
885
898
)
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 );
889
901
}
890
902
}
891
903
}
@@ -909,15 +921,13 @@ protected function _validateRowForDelete(array $rowData, $rowNumber)
909
921
$ customerId = $ this ->_getCustomerId ($ email , $ website );
910
922
if ($ customerId === false ) {
911
923
$ 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 );
921
931
}
922
932
}
923
933
}
@@ -931,19 +941,18 @@ protected function _validateRowForDelete(array $rowData, $rowNumber)
931
941
*/
932
942
protected function _checkRowDuplicate ($ customerId , $ addressId )
933
943
{
934
- if ( $ this ->addressStorage ->doesExist (
944
+ $ isAddressExists = $ this ->addressStorage ->doesExist (
935
945
(string )$ addressId ,
936
946
(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 ;
946
953
}
954
+
955
+ return $ isAddressExists && $ isPkRowSet ;
947
956
}
948
957
949
958
/**
@@ -957,4 +966,24 @@ public function setCustomerAttributes($customerAttributes)
957
966
$ this ->_customerAttributes = $ customerAttributes ;
958
967
return $ this ;
959
968
}
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
+ }
960
989
}
0 commit comments