@@ -104,27 +104,79 @@ private function loadCustomersData(array $customerIdentifiers): void
104104 };
105105 $ offset = 0 ;
106106 for ($ chunk = $ getChuck ($ offset ); !empty ($ chunk ); $ offset += $ pageSize , $ chunk = $ getChuck ($ offset )) {
107- $ customerWebsites = array_reduce ($ chunk , function ($ customerWebsiteByEmail , $ customer ) {
108- $ customerWebsiteByEmail [$ customer ['email ' ]][] = $ customer ['website_id ' ];
109- return $ customerWebsiteByEmail ;
110- }, []);
107+ $ customerWebsites = $ this ->buildCustomerWebsitesMap ($ chunk );
111108 $ chunkSelect = clone $ select ;
112109 $ chunkSelect ->where ($ customerTableId . '.email IN (?) ' , array_keys ($ customerWebsites ));
113110 $ customers = $ collection ->getConnection ()->fetchAll ($ chunkSelect );
114111 foreach ($ customers as $ customer ) {
115- $ this ->addCustomerByArray ($ customer );
116- if ($ this ->configShare ->isGlobalScope () &&
117- !in_array ((int ) $ customer ['website_id ' ], $ customerWebsites [$ customer ['email ' ]], true )
118- ) {
119- foreach ($ customerWebsites [$ customer ['email ' ]] as $ websiteId ) {
120- $ customer ['website_id ' ] = $ websiteId ;
121- $ this ->addCustomerByArray ($ customer );
122- }
123- }
112+ $ this ->processCustomerData ($ customer , $ customerWebsites );
124113 }
125114 }
126115 }
127116
117+ /**
118+ * Build customer websites map from chunk of customer identifiers.
119+ *
120+ * @param array $chunk
121+ * @return array
122+ */
123+ private function buildCustomerWebsitesMap (array $ chunk ): array
124+ {
125+ return array_reduce ($ chunk , function ($ customerWebsiteByEmail , $ customer ) {
126+ $ email = isset ($ customer ['email ' ]) ? mb_strtolower (trim ($ customer ['email ' ])) : '' ;
127+ $ customerWebsiteByEmail [$ email ][] = $ customer ['website_id ' ];
128+ return $ customerWebsiteByEmail ;
129+ }, []);
130+ }
131+
132+ /**
133+ * Process customer data from database and handle global scope logic.
134+ *
135+ * @param array $customer
136+ * @param array $customerWebsites
137+ * @return void
138+ */
139+ private function processCustomerData (array $ customer , array $ customerWebsites ): void
140+ {
141+ $ email = isset ($ customer ['email ' ]) ? mb_strtolower (trim ($ customer ['email ' ])) : '' ;
142+ $ customer ['email ' ] = $ email ;
143+ $ this ->addCustomerByArray ($ customer );
144+
145+ if ($ this ->shouldProcessGlobalScope ($ email , $ customer , $ customerWebsites )) {
146+ $ this ->processGlobalScopeCustomer ($ customer , $ customerWebsites [$ email ]);
147+ }
148+ }
149+
150+ /**
151+ * Check if customer should be processed for global scope.
152+ *
153+ * @param string $email
154+ * @param array $customer
155+ * @param array $customerWebsites
156+ * @return bool
157+ */
158+ private function shouldProcessGlobalScope (string $ email , array $ customer , array $ customerWebsites ): bool
159+ {
160+ return $ this ->configShare ->isGlobalScope ()
161+ && isset ($ customerWebsites [$ email ])
162+ && !in_array ((int ) $ customer ['website_id ' ], $ customerWebsites [$ email ], true );
163+ }
164+
165+ /**
166+ * Process customer for all websites in global scope.
167+ *
168+ * @param array $customer
169+ * @param array $websiteIds
170+ * @return void
171+ */
172+ private function processGlobalScopeCustomer (array $ customer , array $ websiteIds ): void
173+ {
174+ foreach ($ websiteIds as $ websiteId ) {
175+ $ customer ['website_id ' ] = $ websiteId ;
176+ $ this ->addCustomerByArray ($ customer );
177+ }
178+ }
179+
128180 /**
129181 * Add a customer by an array
130182 *
0 commit comments