Skip to content

Commit b483ded

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-56806' into 2.0-develop-pr3
2 parents 4bf9576 + 033a8d6 commit b483ded

File tree

16 files changed

+438
-142
lines changed

16 files changed

+438
-142
lines changed

app/code/Magento/Customer/Model/AccountManagement.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,12 @@ class AccountManagement implements AccountManagementInterface
168168
* @var CustomerRepositoryInterface
169169
*/
170170
private $customerRepository;
171+
171172
/**
172173
* @var ScopeConfigInterface
173174
*/
174175
private $scopeConfig;
176+
175177
/**
176178
* @var TransportBuilder
177179
*/
@@ -502,6 +504,7 @@ public function createAccountWithPasswordHash(CustomerInterface $customer, $hash
502504
}
503505
// Existing password hash will be used from secured customer data registry when saving customer
504506
}
507+
505508
// Make sure we have a storeId to associate this customer with.
506509
if (!$customer->getStoreId()) {
507510
if ($customer->getWebsiteId()) {
@@ -534,9 +537,17 @@ public function createAccountWithPasswordHash(CustomerInterface $customer, $hash
534537
}
535538
try {
536539
foreach ($customerAddresses as $address) {
537-
$address->setCustomerId($customer->getId());
538-
$this->addressRepository->save($address);
540+
if ($address->getId()) {
541+
$newAddress = clone $address;
542+
$newAddress->setId(null);
543+
$newAddress->setCustomerId($customer->getId());
544+
$this->addressRepository->save($newAddress);
545+
} else {
546+
$address->setCustomerId($customer->getId());
547+
$this->addressRepository->save($address);
548+
}
539549
}
550+
$this->customerRegistry->remove($customer->getId());
540551
} catch (InputException $e) {
541552
$this->customerRepository->delete($customer);
542553
throw $e;
@@ -618,9 +629,9 @@ public function changePasswordById($customerId, $currentPassword, $newPassword)
618629
}
619630

620631
/**
621-
* Change customer password.
632+
* Change customer password
622633
*
623-
* @param CustomerModel $customer
634+
* @param CustomerInterface $customer
624635
* @param string $currentPassword
625636
* @param string $newPassword
626637
* @return bool true on success

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
1818
use Magento\Framework\Exception\AuthenticationException;
1919
use Magento\Framework\Indexer\StateInterface;
20+
use Magento\Store\Model\ScopeInterface;
2021

2122
/**
2223
* Customer model
@@ -320,7 +321,7 @@ public function updateData($customer)
320321
{
321322
$customerDataAttributes = $this->dataObjectProcessor->buildOutputDataArray(
322323
$customer,
323-
'\Magento\Customer\Api\Data\CustomerInterface'
324+
\Magento\Customer\Api\Data\CustomerInterface::class
324325
);
325326

326327
foreach ($customerDataAttributes as $attributeCode => $attributeData) {
@@ -823,15 +824,15 @@ protected function _sendEmailTemplate($template, $sender, $templateParams = [],
823824
{
824825
/** @var \Magento\Framework\Mail\TransportInterface $transport */
825826
$transport = $this->_transportBuilder->setTemplateIdentifier(
826-
$this->_scopeConfig->getValue($template, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId)
827+
$this->_scopeConfig->getValue($template, ScopeInterface::SCOPE_STORE, $storeId)
827828
)->setTemplateOptions(
828829
['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId]
829830
)->setTemplateVars(
830831
$templateParams
831832
)->setScopeId(
832833
$storeId
833834
)->setFrom(
834-
$this->_scopeConfig->getValue($sender, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId)
835+
$this->_scopeConfig->getValue($sender, ScopeInterface::SCOPE_STORE, $storeId)
835836
)->addTo(
836837
$this->getEmail(),
837838
$this->getName()
@@ -874,7 +875,7 @@ public function getGroupId()
874875
$storeId = $this->getStoreId() ? $this->getStoreId() : $this->_storeManager->getStore()->getId();
875876
$groupId = $this->_scopeConfig->getValue(
876877
GroupManagement::XML_PATH_DEFAULT_ID,
877-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
878+
ScopeInterface::SCOPE_STORE,
878879
$storeId
879880
);
880881
$this->setData('group_id', $groupId);

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

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
*/
88
namespace Magento\Customer\Model\ResourceModel;
99

10+
use Magento\Customer\Model\CustomerRegistry;
11+
use Magento\Customer\Model\ResourceModel\Address\DeleteRelation;
12+
use Magento\Framework\App\ObjectManager;
13+
14+
/**
15+
* Class Address
16+
* @package Magento\Customer\Model\ResourceModel
17+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
18+
*/
1019
class Address extends \Magento\Eav\Model\Entity\VersionControl\AbstractEntity
1120
{
1221
/**
@@ -19,6 +28,16 @@ class Address extends \Magento\Eav\Model\Entity\VersionControl\AbstractEntity
1928
*/
2029
protected $customerRepository;
2130

31+
/**
32+
* @var DeleteRelation
33+
*/
34+
protected $deleteRelation;
35+
36+
/**
37+
* @var CustomerRegistry
38+
*/
39+
protected $customerRegistry;
40+
2241
/**
2342
* @param \Magento\Eav\Model\Entity\Context $context
2443
* @param \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot,
@@ -110,20 +129,38 @@ public function delete($object)
110129
}
111130

112131
/**
113-
* {@inheritdoc}
132+
* @deprecated
133+
* @return DeleteRelation
114134
*/
115-
protected function _afterDelete(\Magento\Framework\DataObject $address)
135+
private function getDeleteRelation()
116136
{
117-
if ($address->getId()) {
118-
$customer = $this->customerRepository->getById($address->getCustomerId());
119-
if ($customer->getDefaultBilling() == $address->getId()) {
120-
$customer->setDefaultBilling(null);
121-
}
122-
if ($customer->getDefaultShipping() == $address->getId()) {
123-
$customer->setDefaultShipping(null);
124-
}
125-
$this->customerRepository->save($customer);
137+
if ($this->deleteRelation === null) {
138+
$this->deleteRelation = ObjectManager::getInstance()->get(DeleteRelation::class);
126139
}
140+
return $this->deleteRelation;
141+
}
142+
143+
/**
144+
* @deprecated
145+
* @return CustomerRegistry
146+
*/
147+
private function getCustomerRegistry()
148+
{
149+
if ($this->customerRegistry === null) {
150+
$this->customerRegistry = ObjectManager::getInstance()->get(CustomerRegistry::class);
151+
}
152+
return $this->customerRegistry;
153+
}
154+
155+
/**
156+
* @param \Magento\Customer\Model\Address $address
157+
* @return $this
158+
*/
159+
protected function _afterDelete(\Magento\Framework\DataObject $address)
160+
{
161+
$customer = $this->getCustomerRegistry()->retrieve($address->getCustomerId());
162+
163+
$this->getDeleteRelation()->deleteRelation($address, $customer);
127164
return parent::_afterDelete($address);
128165
}
129166
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Model\ResourceModel\Address;
7+
8+
use Magento\Customer\Api\Data\CustomerInterface;
9+
10+
/**
11+
* Class DeleteRelation
12+
* @package Magento\Customer\Model\ResourceModel\Address
13+
*/
14+
class DeleteRelation
15+
{
16+
/**
17+
* Delete relation (billing and shipping) between customer and address
18+
*
19+
* @param \Magento\Framework\Model\AbstractModel $address
20+
* @param \Magento\Customer\Model\Customer $customer
21+
* @return void
22+
*/
23+
public function deleteRelation(
24+
\Magento\Framework\Model\AbstractModel $address,
25+
\Magento\Customer\Model\Customer $customer
26+
) {
27+
$toUpdate = $this->getDataToUpdate($address, $customer);
28+
29+
if (!$address->getIsCustomerSaveTransaction() && !empty($toUpdate)) {
30+
$address->getResource()->getConnection()->update(
31+
$address->getResource()->getTable('customer_entity'),
32+
$toUpdate,
33+
$address->getResource()->getConnection()->quoteInto('entity_id = ?', $customer->getId())
34+
);
35+
}
36+
}
37+
38+
/**
39+
* Return address type (billing or shipping), or null if address is not default
40+
*
41+
* @param \Magento\Customer\Api\Data\AddressInterface $address
42+
* @param \Magento\Customer\Api\Data\CustomerInterface $customer
43+
* @return array
44+
*/
45+
private function getDataToUpdate(
46+
\Magento\Framework\Model\AbstractModel $address,
47+
\Magento\Customer\Model\Customer $customer
48+
) {
49+
$toUpdate = [];
50+
if ($address->getId()) {
51+
if ($customer->getDefaultBilling() == $address->getId()) {
52+
$toUpdate[CustomerInterface::DEFAULT_BILLING] = null;
53+
}
54+
55+
if ($customer->getDefaultShipping() == $address->getId()) {
56+
$toUpdate[CustomerInterface::DEFAULT_SHIPPING] = null;
57+
}
58+
}
59+
60+
return $toUpdate;
61+
}
62+
}

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\Customer\Model\ResourceModel;
99

1010
use Magento\Customer\Model\Address as CustomerAddressModel;
11+
use Magento\Customer\Model\Customer as CustomerModel;
1112
use Magento\Customer\Model\ResourceModel\Address\Collection;
1213
use Magento\Framework\Api\Search\FilterGroup;
1314
use Magento\Framework\Api\SearchCriteriaInterface;
@@ -107,6 +108,7 @@ public function save(\Magento\Customer\Api\Data\AddressInterface $address)
107108
}
108109

109110
if ($addressModel === null) {
111+
/** @var \Magento\Customer\Model\Address $addressModel */
110112
$addressModel = $this->addressFactory->create();
111113
$addressModel->updateData($address);
112114
$addressModel->setCustomer($customerModel);
@@ -119,15 +121,27 @@ public function save(\Magento\Customer\Api\Data\AddressInterface $address)
119121
throw $inputException;
120122
}
121123
$addressModel->save();
124+
$address->setId($addressModel->getId());
122125
// Clean up the customer registry since the Address save has a
123126
// side effect on customer : \Magento\Customer\Model\ResourceModel\Address::_afterSave
124-
$this->customerRegistry->remove($address->getCustomerId());
125127
$this->addressRegistry->push($addressModel);
126-
$customerModel->getAddressesCollection()->clear();
128+
$this->updateAddressCollection($customerModel, $addressModel);
127129

128130
return $addressModel->getDataModel();
129131
}
130132

133+
/**
134+
* @param Customer $customer
135+
* @param Address $address
136+
* @throws \Magento\Framework\Exception\LocalizedException
137+
* @return void
138+
*/
139+
private function updateAddressCollection(CustomerModel $customer, CustomerAddressModel $address)
140+
{
141+
$customer->getAddressesCollection()->removeItemByKey($address->getId());
142+
$customer->getAddressesCollection()->addItem($address);
143+
}
144+
131145
/**
132146
* Retrieve customer address.
133147
*
@@ -234,7 +248,7 @@ public function deleteById($addressId)
234248
{
235249
$address = $this->addressRegistry->retrieve($addressId);
236250
$customerModel = $this->customerRegistry->retrieve($address->getCustomerId());
237-
$customerModel->getAddressesCollection()->clear();
251+
$customerModel->getAddressesCollection()->removeItemByKey($addressId);
238252
$this->addressResource->delete($address);
239253
$this->addressRegistry->remove($addressId);
240254
return true;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
222222
$this->addressRepository->deleteById($addressId);
223223
}
224224
}
225-
225+
$this->customerRegistry->remove($customerId);
226226
$savedCustomer = $this->get($customer->getEmail(), $customer->getWebsiteId());
227227
$this->eventManager->dispatch(
228228
'customer_save_after_data_object',

0 commit comments

Comments
 (0)