@@ -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
*
@@ -741,6 +761,7 @@ protected function _saveCustomerDefaults(array $defaults)
741
761
{
742
762
foreach ($ defaults as $ tableName => $ data ) {
743
763
foreach ($ data as $ customerId => $ defaultsData ) {
764
+ // phpcs:ignore Magento2.Performance.ForeachArrayMerge
744
765
$ data = array_merge (
745
766
['entity_id ' => $ customerId ],
746
767
$ defaultsData
@@ -781,11 +802,13 @@ public function getEntityTypeCode()
781
802
*
782
803
* @static
783
804
* @return array
805
+ * phpcs:disable Magento2.Functions.StaticFunction
784
806
*/
785
807
public static function getDefaultAddressAttributeMapping ()
786
808
{
787
809
return self ::$ _defaultAddressAttributeMapping ;
788
810
}
811
+ // phpcs:enable
789
812
790
813
/**
791
814
* Check if address for import is empty (for customer composite mode)
@@ -820,7 +843,6 @@ protected function _isOptionalAddressEmpty(array $rowData)
820
843
* @param int $rowNumber
821
844
* @return void
822
845
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
823
- * @SuppressWarnings(PHPMD.NPathComplexity)
824
846
*/
825
847
protected function _validateRowForUpdate (array $ rowData , $ rowNumber )
826
848
{
@@ -833,38 +855,36 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
833
855
834
856
if ($ customerId === false ) {
835
857
$ this ->addRowError (self ::ERROR_CUSTOMER_NOT_FOUND , $ rowNumber );
858
+ } elseif ($ this ->_checkRowDuplicate ($ customerId , $ addressId )) {
859
+ $ this ->addRowError (self ::ERROR_DUPLICATE_PK , $ rowNumber );
836
860
} 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 );
844
865
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 );
864
883
}
884
+ }
865
885
886
+ if (isset ($ rowData [self ::COLUMN_COUNTRY_ID ])) {
866
887
if (isset ($ rowData [self ::COLUMN_POSTCODE ])
867
- && isset ($ rowData [self ::COLUMN_COUNTRY_ID ])
868
888
&& !$ this ->postcodeValidator ->isValid (
869
889
$ rowData [self ::COLUMN_COUNTRY_ID ],
870
890
$ rowData [self ::COLUMN_POSTCODE ]
@@ -873,19 +893,14 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
873
893
$ this ->addRowError (self ::ERROR_VALUE_IS_REQUIRED , $ rowNumber , self ::COLUMN_POSTCODE );
874
894
}
875
895
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 ]
885
901
)
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 );
889
904
}
890
905
}
891
906
}
@@ -909,15 +924,13 @@ protected function _validateRowForDelete(array $rowData, $rowNumber)
909
924
$ customerId = $ this ->_getCustomerId ($ email , $ website );
910
925
if ($ customerId === false ) {
911
926
$ 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 );
921
934
}
922
935
}
923
936
}
@@ -931,19 +944,18 @@ protected function _validateRowForDelete(array $rowData, $rowNumber)
931
944
*/
932
945
protected function _checkRowDuplicate ($ customerId , $ addressId )
933
946
{
934
- if ( $ this ->addressStorage ->doesExist (
947
+ $ isAddressExists = $ this ->addressStorage ->doesExist (
935
948
(string )$ addressId ,
936
949
(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 ;
946
956
}
957
+
958
+ return $ isAddressExists && $ isPkRowSet ;
947
959
}
948
960
949
961
/**
@@ -957,4 +969,24 @@ public function setCustomerAttributes($customerAttributes)
957
969
$ this ->_customerAttributes = $ customerAttributes ;
958
970
return $ this ;
959
971
}
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
+ }
960
992
}
0 commit comments