10
10
use Magento \Customer \Api \CustomerRepositoryInterface ;
11
11
use Magento \Customer \Api \Data \CustomerInterface ;
12
12
use Magento \Customer \Api \Data \CustomerSearchResultsInterfaceFactory ;
13
+ use Magento \Customer \Api \GroupRepositoryInterface ;
13
14
use Magento \Customer \Model \Customer as CustomerModel ;
14
15
use Magento \Customer \Model \Customer \NotificationStorage ;
15
16
use Magento \Customer \Model \CustomerFactory ;
27
28
use Magento \Framework \Api \SearchCriteriaInterface ;
28
29
use Magento \Framework \App \ObjectManager ;
29
30
use Magento \Framework \Event \ManagerInterface ;
31
+ use Magento \Framework \Exception \LocalizedException ;
32
+ use Magento \Framework \Exception \NoSuchEntityException ;
30
33
use Magento \Store \Model \StoreManagerInterface ;
31
34
32
35
/**
@@ -119,6 +122,11 @@ class CustomerRepository implements CustomerRepositoryInterface
119
122
*/
120
123
private $ delegatedStorage ;
121
124
125
+ /**
126
+ * @var GroupRepositoryInterface
127
+ */
128
+ private $ groupRepository ;
129
+
122
130
/**
123
131
* @param CustomerFactory $customerFactory
124
132
* @param CustomerSecureFactory $customerSecureFactory
@@ -136,6 +144,7 @@ class CustomerRepository implements CustomerRepositoryInterface
136
144
* @param CollectionProcessorInterface $collectionProcessor
137
145
* @param NotificationStorage $notificationStorage
138
146
* @param DelegatedStorage|null $delegatedStorage
147
+ * @param GroupRepositoryInterface|null $groupRepository
139
148
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
140
149
*/
141
150
public function __construct (
@@ -154,7 +163,8 @@ public function __construct(
154
163
JoinProcessorInterface $ extensionAttributesJoinProcessor ,
155
164
CollectionProcessorInterface $ collectionProcessor ,
156
165
NotificationStorage $ notificationStorage ,
157
- DelegatedStorage $ delegatedStorage = null
166
+ DelegatedStorage $ delegatedStorage = null ,
167
+ ?GroupRepositoryInterface $ groupRepository = null
158
168
) {
159
169
$ this ->customerFactory = $ customerFactory ;
160
170
$ this ->customerSecureFactory = $ customerSecureFactory ;
@@ -172,6 +182,7 @@ public function __construct(
172
182
$ this ->collectionProcessor = $ collectionProcessor ;
173
183
$ this ->notificationStorage = $ notificationStorage ;
174
184
$ this ->delegatedStorage = $ delegatedStorage ?? ObjectManager::getInstance ()->get (DelegatedStorage::class);
185
+ $ this ->groupRepository = $ groupRepository ?: ObjectManager::getInstance ()->get (GroupRepositoryInterface::class);
175
186
}
176
187
177
188
/**
@@ -216,6 +227,7 @@ public function save(CustomerInterface $customer, $passwordHash = null)
216
227
$ prevCustomerData ? $ prevCustomerData ->getStoreId () : $ this ->storeManager ->getStore ()->getId ()
217
228
);
218
229
}
230
+ $ this ->validateGroupId ($ customer ->getGroupId ());
219
231
$ this ->setCustomerGroupId ($ customerModel , $ customerArr , $ prevCustomerDataArr );
220
232
// Need to use attribute set or future updates can cause data loss
221
233
if (!$ customerModel ->getAttributeSetId ()) {
@@ -268,10 +280,7 @@ public function save(CustomerInterface $customer, $passwordHash = null)
268
280
$ savedAddressIds [] = $ address ->getId ();
269
281
}
270
282
}
271
- $ addressIdsToDelete = array_diff ($ existingAddressIds , $ savedAddressIds );
272
- foreach ($ addressIdsToDelete as $ addressId ) {
273
- $ this ->addressRepository ->deleteById ($ addressId );
274
- }
283
+ $ this ->deleteAddressesByIds (array_diff ($ existingAddressIds , $ savedAddressIds ));
275
284
}
276
285
$ this ->customerRegistry ->remove ($ customerId );
277
286
$ savedCustomer = $ this ->get ($ customer ->getEmail (), $ customer ->getWebsiteId ());
@@ -286,6 +295,39 @@ public function save(CustomerInterface $customer, $passwordHash = null)
286
295
return $ savedCustomer ;
287
296
}
288
297
298
+ /**
299
+ * Delete addresses by ids
300
+ *
301
+ * @param array $addressIds
302
+ * @return void
303
+ */
304
+ private function deleteAddressesByIds (array $ addressIds ): void
305
+ {
306
+ foreach ($ addressIds as $ id ) {
307
+ $ this ->addressRepository ->deleteById ($ id );
308
+ }
309
+ }
310
+
311
+ /**
312
+ * Validate customer group id if exist
313
+ *
314
+ * @param int|null $groupId
315
+ * @return bool
316
+ * @throws LocalizedException
317
+ */
318
+ private function validateGroupId (?int $ groupId ): bool
319
+ {
320
+ if ($ groupId ) {
321
+ try {
322
+ $ this ->groupRepository ->getById ($ groupId );
323
+ } catch (NoSuchEntityException $ e ) {
324
+ throw new LocalizedException (__ ('The specified customer group id does not exist. ' ));
325
+ }
326
+ }
327
+
328
+ return true ;
329
+ }
330
+
289
331
/**
290
332
* Set secure data to customer model
291
333
*
0 commit comments