Skip to content

Commit 54b36fb

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-61907' into 2.1.8-develop-pr8
2 parents 02fde6e + 624a245 commit 54b36fb

File tree

3 files changed

+291
-116
lines changed

3 files changed

+291
-116
lines changed

app/code/Magento/Customer/Model/ResourceModel/CustomerRepository.php

Lines changed: 129 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
/**
1616
* Customer repository.
17+
*
1718
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1819
*/
1920
class CustomerRepository implements \Magento\Customer\Api\CustomerRepositoryInterface
@@ -131,15 +132,19 @@ public function __construct(
131132

132133
/**
133134
* {@inheritdoc}
134-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
135-
* @SuppressWarnings(PHPMD.NPathComplexity)
136135
*/
137136
public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $passwordHash = null)
138137
{
139138
$prevCustomerData = null;
139+
$prevCustomerDataArr = null;
140+
140141
if ($customer->getId()) {
141142
$prevCustomerData = $this->getById($customer->getId());
143+
$prevCustomerDataArr = $prevCustomerData->__toArray();
142144
}
145+
146+
/** @var $customer \Magento\Customer\Model\Data\Customer */
147+
$customerArr = $customer->__toArray();
143148
$customer = $this->imageProcessor->save(
144149
$customer,
145150
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
@@ -151,15 +156,17 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
151156
$customerData = $this->extensibleDataObjectConverter->toNestedArray(
152157
$customer,
153158
[],
154-
'\Magento\Customer\Api\Data\CustomerInterface'
159+
\Magento\Customer\Api\Data\CustomerInterface::class
155160
);
156161

157162
$customer->setAddresses($origAddresses);
158163
$customerModel = $this->customerFactory->create(['data' => $customerData]);
159164
$storeId = $customerModel->getStoreId();
165+
160166
if ($storeId === null) {
161167
$customerModel->setStoreId($this->storeManager->getStore()->getId());
162168
}
169+
163170
$customerModel->setId($customer->getId());
164171

165172
// Need to use attribute set or future updates can cause data loss
@@ -169,19 +176,7 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
169176
);
170177
}
171178
// Populate model with secure data
172-
if ($customer->getId()) {
173-
$customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId());
174-
$customerModel->setRpToken($customerSecure->getRpToken());
175-
$customerModel->setRpTokenCreatedAt($customerSecure->getRpTokenCreatedAt());
176-
$customerModel->setPasswordHash($customerSecure->getPasswordHash());
177-
$customerModel->setFailuresNum($customerSecure->getFailuresNum());
178-
$customerModel->setFirstFailure($customerSecure->getFirstFailure());
179-
$customerModel->setLockExpires($customerSecure->getLockExpires());
180-
} else {
181-
if ($passwordHash) {
182-
$customerModel->setPasswordHash($passwordHash);
183-
}
184-
}
179+
$this->populateCustomerModelWithSecureData($customer, $passwordHash, $customerModel);
185180

186181
// If customer email was changed, reset RpToken info
187182
if ($prevCustomerData
@@ -190,36 +185,16 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
190185
$customerModel->setRpToken(null);
191186
$customerModel->setRpTokenCreatedAt(null);
192187
}
188+
189+
$this->setDefaultBilling($customerArr, $prevCustomerDataArr, $customerModel);
190+
191+
$this->setDefaultShipping($customerArr, $prevCustomerDataArr, $customerModel);
192+
193193
$customerModel->save();
194194
$this->customerRegistry->push($customerModel);
195195
$customerId = $customerModel->getId();
196196

197-
if ($customer->getAddresses() !== null) {
198-
if ($customer->getId()) {
199-
$existingAddresses = $this->getById($customer->getId())->getAddresses();
200-
$getIdFunc = function ($address) {
201-
return $address->getId();
202-
};
203-
$existingAddressIds = array_map($getIdFunc, $existingAddresses);
204-
} else {
205-
$existingAddressIds = [];
206-
}
207-
208-
$savedAddressIds = [];
209-
foreach ($customer->getAddresses() as $address) {
210-
$address->setCustomerId($customerId)
211-
->setRegion($address->getRegion());
212-
$this->addressRepository->save($address);
213-
if ($address->getId()) {
214-
$savedAddressIds[] = $address->getId();
215-
}
216-
}
217-
218-
$addressIdsToDelete = array_diff($existingAddressIds, $savedAddressIds);
219-
foreach ($addressIdsToDelete as $addressId) {
220-
$this->addressRepository->deleteById($addressId);
221-
}
222-
}
197+
$this->updateAddresses($customer, $customerId);
223198

224199
$savedCustomer = $this->get($customer->getEmail(), $customer->getWebsiteId());
225200
$this->eventManager->dispatch(
@@ -256,7 +231,10 @@ public function getList(SearchCriteriaInterface $searchCriteria)
256231
$searchResults->setSearchCriteria($searchCriteria);
257232
/** @var \Magento\Customer\Model\ResourceModel\Customer\Collection $collection */
258233
$collection = $this->customerFactory->create()->getCollection();
259-
$this->extensionAttributesJoinProcessor->process($collection, 'Magento\Customer\Api\Data\CustomerInterface');
234+
$this->extensionAttributesJoinProcessor->process(
235+
$collection,
236+
\Magento\Customer\Api\Data\CustomerInterface::class
237+
);
260238
// This is needed to make sure all the attributes are properly loaded
261239
foreach ($this->customerMetadata->getAllAttributesMetadata() as $metadata) {
262240
$collection->addAttributeToSelect($metadata->getAttributeCode());
@@ -336,4 +314,112 @@ protected function addFilterGroupToCollection(
336314
$collection->addFieldToFilter($fields);
337315
}
338316
}
317+
318+
/**
319+
* Update customer addresses.
320+
*
321+
* @param \Magento\Framework\Api\CustomAttributesDataInterface $customer
322+
* @param int $customerId
323+
* @return void
324+
* @throws \Magento\Framework\Exception\InputException
325+
*/
326+
private function updateAddresses(\Magento\Framework\Api\CustomAttributesDataInterface $customer, int $customerId)
327+
{
328+
if ($customer->getAddresses() !== null) {
329+
if ($customer->getId()) {
330+
$existingAddresses = $this->getById($customer->getId())->getAddresses();
331+
$getIdFunc = function ($address) {
332+
return $address->getId();
333+
};
334+
$existingAddressIds = array_map($getIdFunc, $existingAddresses);
335+
} else {
336+
$existingAddressIds = [];
337+
}
338+
339+
$savedAddressIds = [];
340+
foreach ($customer->getAddresses() as $address) {
341+
$address->setCustomerId($customerId)
342+
->setRegion($address->getRegion());
343+
$this->addressRepository->save($address);
344+
if ($address->getId()) {
345+
$savedAddressIds[] = $address->getId();
346+
}
347+
}
348+
349+
$addressIdsToDelete = array_diff($existingAddressIds, $savedAddressIds);
350+
foreach ($addressIdsToDelete as $addressId) {
351+
$this->addressRepository->deleteById($addressId);
352+
}
353+
}
354+
}
355+
356+
/**
357+
* Populate customer model with secure data.
358+
*
359+
* @param \Magento\Framework\Api\CustomAttributesDataInterface $customer
360+
* @param string $passwordHash
361+
* @param \Magento\Customer\Model\Customer\Interceptor $customerModel
362+
* @return void
363+
*/
364+
private function populateCustomerModelWithSecureData(
365+
\Magento\Framework\Api\CustomAttributesDataInterface $customer,
366+
$passwordHash,
367+
$customerModel
368+
) {
369+
if ($customer->getId()) {
370+
$customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId());
371+
$customerModel->setRpToken($customerSecure->getRpToken());
372+
$customerModel->setRpTokenCreatedAt($customerSecure->getRpTokenCreatedAt());
373+
$customerModel->setPasswordHash($customerSecure->getPasswordHash());
374+
$customerModel->setFailuresNum($customerSecure->getFailuresNum());
375+
$customerModel->setFirstFailure($customerSecure->getFirstFailure());
376+
$customerModel->setLockExpires($customerSecure->getLockExpires());
377+
} else {
378+
if ($passwordHash) {
379+
$customerModel->setPasswordHash($passwordHash);
380+
}
381+
}
382+
}
383+
384+
/**
385+
* Set default billing.
386+
*
387+
* @param array $customerArr
388+
* @param array $prevCustomerDataArr
389+
* @param \Magento\Customer\Model\Customer\Interceptor $customerModel
390+
* @return void
391+
*/
392+
private function setDefaultBilling(
393+
$customerArr,
394+
$prevCustomerDataArr,
395+
$customerModel
396+
) {
397+
if (!array_key_exists('default_billing', $customerArr) &&
398+
null !== $prevCustomerDataArr &&
399+
array_key_exists('default_billing', $prevCustomerDataArr)
400+
) {
401+
$customerModel->setDefaultBilling($prevCustomerDataArr['default_billing']);
402+
}
403+
}
404+
405+
/**
406+
* Set default shipping.
407+
*
408+
* @param array $customerArr
409+
* @param array $prevCustomerDataArr
410+
* @param \Magento\Customer\Model\Customer\Interceptor $customerModel
411+
* @return void
412+
*/
413+
private function setDefaultShipping(
414+
$customerArr,
415+
$prevCustomerDataArr,
416+
$customerModel
417+
) {
418+
if (!array_key_exists('default_shipping', $customerArr) &&
419+
null !== $prevCustomerDataArr &&
420+
array_key_exists('default_shipping', $prevCustomerDataArr)
421+
) {
422+
$customerModel->setDefaultShipping($prevCustomerDataArr['default_shipping']);
423+
}
424+
}
339425
}

0 commit comments

Comments
 (0)