|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
6 | 9 | namespace Magento\Customer\Model\ResourceModel;
|
7 | 10 |
|
8 | 11 | use Magento\Customer\Api\CustomerMetadataInterface;
|
|
27 | 30 | use Magento\Framework\Api\SearchCriteriaInterface;
|
28 | 31 | use Magento\Framework\App\ObjectManager;
|
29 | 32 | use Magento\Framework\Event\ManagerInterface;
|
| 33 | +use Magento\Framework\Exception\InputException; |
30 | 34 | use Magento\Framework\Exception\LocalizedException;
|
31 | 35 | use Magento\Framework\Exception\NoSuchEntityException;
|
32 | 36 | use Magento\Store\Model\StoreManagerInterface;
|
@@ -195,12 +199,19 @@ public function __construct(
|
195 | 199 | * @throws \Magento\Framework\Exception\LocalizedException
|
196 | 200 | * @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
197 | 201 | * @SuppressWarnings(PHPMD.NPathComplexity)
|
| 202 | + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
198 | 203 | */
|
199 | 204 | public function save(CustomerInterface $customer, $passwordHash = null)
|
200 | 205 | {
|
201 | 206 | /** @var NewOperation|null $delegatedNewOperation */
|
202 | 207 | $delegatedNewOperation = !$customer->getId() ? $this->delegatedStorage->consumeNewOperation() : null;
|
203 | 208 | $prevCustomerData = $prevCustomerDataArr = null;
|
| 209 | + if ($customer->getDefaultBilling()) { |
| 210 | + $this->validateDefaultAddress($customer, CustomerInterface::DEFAULT_BILLING); |
| 211 | + } |
| 212 | + if ($customer->getDefaultShipping()) { |
| 213 | + $this->validateDefaultAddress($customer, CustomerInterface::DEFAULT_SHIPPING); |
| 214 | + } |
204 | 215 | if ($customer->getId()) {
|
205 | 216 | $prevCustomerData = $this->getById($customer->getId());
|
206 | 217 | $prevCustomerDataArr = $this->prepareCustomerData($prevCustomerData->__toArray());
|
@@ -228,7 +239,7 @@ public function save(CustomerInterface $customer, $passwordHash = null)
|
228 | 239 | $prevCustomerData ? $prevCustomerData->getStoreId() : $this->storeManager->getStore()->getId()
|
229 | 240 | );
|
230 | 241 | }
|
231 |
| - $this->validateGroupId($customer->getGroupId()); |
| 242 | + $this->validateGroupId((int)$customer->getGroupId()); |
232 | 243 | $this->setCustomerGroupId($customerModel, $customerArr, $prevCustomerDataArr);
|
233 | 244 | // Need to use attribute set or future updates can cause data loss
|
234 | 245 | if (!$customerModel->getAttributeSetId()) {
|
@@ -553,4 +564,34 @@ private function prepareCustomerData(array $customerData): array
|
553 | 564 | }
|
554 | 565 | return $customerData;
|
555 | 566 | }
|
| 567 | + |
| 568 | + /** |
| 569 | + * To validate default address |
| 570 | + * |
| 571 | + * @param CustomerInterface $customer |
| 572 | + * @param string $defaultAddressType |
| 573 | + * @return void |
| 574 | + * @throws InputException |
| 575 | + */ |
| 576 | + private function validateDefaultAddress( |
| 577 | + CustomerInterface $customer, |
| 578 | + string $defaultAddressType |
| 579 | + ): void { |
| 580 | + $addressId = $defaultAddressType === CustomerInterface::DEFAULT_BILLING ? $customer->getDefaultBilling() |
| 581 | + : $customer->getDefaultShipping(); |
| 582 | + if ($customer->getAddresses()) { |
| 583 | + foreach ($customer->getAddresses() as $address) { |
| 584 | + if ((int) $addressId === (int) $address->getId()) { |
| 585 | + return; |
| 586 | + } |
| 587 | + } |
| 588 | + |
| 589 | + throw new InputException( |
| 590 | + __( |
| 591 | + 'The %fieldName value is invalid. Set the correct value and try again.', |
| 592 | + ['fieldName' => $defaultAddressType] |
| 593 | + ) |
| 594 | + ); |
| 595 | + } |
| 596 | + } |
556 | 597 | }
|
0 commit comments