1010use Magento \Customer \Api \CustomerRepositoryInterface ;
1111use Magento \Customer \Api \Data \CustomerInterface ;
1212use Magento \Customer \Api \Data \CustomerSearchResultsInterfaceFactory ;
13+ use Magento \Customer \Api \GroupRepositoryInterface ;
1314use Magento \Customer \Model \Customer as CustomerModel ;
1415use Magento \Customer \Model \Customer \NotificationStorage ;
1516use Magento \Customer \Model \CustomerFactory ;
2728use Magento \Framework \Api \SearchCriteriaInterface ;
2829use Magento \Framework \App \ObjectManager ;
2930use Magento \Framework \Event \ManagerInterface ;
31+ use Magento \Framework \Exception \LocalizedException ;
32+ use Magento \Framework \Exception \NoSuchEntityException ;
3033use Magento \Store \Model \StoreManagerInterface ;
3134
3235/**
@@ -119,6 +122,11 @@ class CustomerRepository implements CustomerRepositoryInterface
119122 */
120123 private $ delegatedStorage ;
121124
125+ /**
126+ * @var GroupRepositoryInterface
127+ */
128+ private $ groupRepository ;
129+
122130 /**
123131 * @param CustomerFactory $customerFactory
124132 * @param CustomerSecureFactory $customerSecureFactory
@@ -136,6 +144,7 @@ class CustomerRepository implements CustomerRepositoryInterface
136144 * @param CollectionProcessorInterface $collectionProcessor
137145 * @param NotificationStorage $notificationStorage
138146 * @param DelegatedStorage|null $delegatedStorage
147+ * @param GroupRepositoryInterface|null $groupRepository
139148 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
140149 */
141150 public function __construct (
@@ -154,7 +163,8 @@ public function __construct(
154163 JoinProcessorInterface $ extensionAttributesJoinProcessor ,
155164 CollectionProcessorInterface $ collectionProcessor ,
156165 NotificationStorage $ notificationStorage ,
157- DelegatedStorage $ delegatedStorage = null
166+ DelegatedStorage $ delegatedStorage = null ,
167+ ?GroupRepositoryInterface $ groupRepository = null
158168 ) {
159169 $ this ->customerFactory = $ customerFactory ;
160170 $ this ->customerSecureFactory = $ customerSecureFactory ;
@@ -172,6 +182,7 @@ public function __construct(
172182 $ this ->collectionProcessor = $ collectionProcessor ;
173183 $ this ->notificationStorage = $ notificationStorage ;
174184 $ this ->delegatedStorage = $ delegatedStorage ?? ObjectManager::getInstance ()->get (DelegatedStorage::class);
185+ $ this ->groupRepository = $ groupRepository ?: ObjectManager::getInstance ()->get (GroupRepositoryInterface::class);
175186 }
176187
177188 /**
@@ -216,6 +227,7 @@ public function save(CustomerInterface $customer, $passwordHash = null)
216227 $ prevCustomerData ? $ prevCustomerData ->getStoreId () : $ this ->storeManager ->getStore ()->getId ()
217228 );
218229 }
230+ $ this ->validateGroupId ($ customer ->getGroupId ());
219231 $ this ->setCustomerGroupId ($ customerModel , $ customerArr , $ prevCustomerDataArr );
220232 // Need to use attribute set or future updates can cause data loss
221233 if (!$ customerModel ->getAttributeSetId ()) {
@@ -268,10 +280,7 @@ public function save(CustomerInterface $customer, $passwordHash = null)
268280 $ savedAddressIds [] = $ address ->getId ();
269281 }
270282 }
271- $ addressIdsToDelete = array_diff ($ existingAddressIds , $ savedAddressIds );
272- foreach ($ addressIdsToDelete as $ addressId ) {
273- $ this ->addressRepository ->deleteById ($ addressId );
274- }
283+ $ this ->deleteAddressesByIds (array_diff ($ existingAddressIds , $ savedAddressIds ));
275284 }
276285 $ this ->customerRegistry ->remove ($ customerId );
277286 $ savedCustomer = $ this ->get ($ customer ->getEmail (), $ customer ->getWebsiteId ());
@@ -286,6 +295,39 @@ public function save(CustomerInterface $customer, $passwordHash = null)
286295 return $ savedCustomer ;
287296 }
288297
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+
289331 /**
290332 * Set secure data to customer model
291333 *
0 commit comments