Skip to content

Commit 95c0a37

Browse files
author
Mike Weis
committed
MAGETWO-33112: Refactor code that uses customer builders in Sales module
- refactored
1 parent 690f2de commit 95c0a37

File tree

2 files changed

+55
-52
lines changed

2 files changed

+55
-52
lines changed

app/code/Magento/Sales/Model/AdminOrder/Create.php

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ class Create extends \Magento\Framework\Object implements \Magento\Checkout\Mode
143143
protected $addressRepository;
144144

145145
/**
146-
* @var \Magento\Customer\Api\Data\AddressDataBuilder
146+
* @var \Magento\Customer\Api\Data\AddressInterfaceFactory
147147
*/
148-
protected $addressBuilder;
148+
protected $addressFactory;
149149

150150
/**
151151
* @var \Magento\Customer\Model\Metadata\FormFactory
@@ -188,9 +188,9 @@ class Create extends \Magento\Framework\Object implements \Magento\Checkout\Mode
188188
protected $accountManagement;
189189

190190
/**
191-
* @var \Magento\Customer\Api\Data\CustomerDataBuilder
191+
* @var \Magento\Customer\Api\Data\CustomerInterfaceFactory
192192
*/
193-
protected $customerBuilder;
193+
protected $customerFactory;
194194

195195
/**
196196
* Constructor
@@ -209,6 +209,11 @@ class Create extends \Magento\Framework\Object implements \Magento\Checkout\Mode
209209
*/
210210
protected $quoteManagement;
211211

212+
/**
213+
* @var \Magento\Framework\Api\DataObjectHelper
214+
*/
215+
protected $dataObjectHelper;
216+
212217
/**
213218
* @param \Magento\Framework\ObjectManagerInterface $objectManager
214219
* @param \Magento\Framework\Event\ManagerInterface $eventManager
@@ -221,7 +226,7 @@ class Create extends \Magento\Framework\Object implements \Magento\Checkout\Mode
221226
* @param Product\Quote\Initializer $quoteInitializer
222227
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
223228
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
224-
* @param \Magento\Customer\Api\Data\AddressDataBuilder $addressBuilder
229+
* @param \Magento\Customer\Api\Data\AddressInterfaceFactory $addressFactory
225230
* @param \Magento\Customer\Model\Metadata\FormFactory $metadataFormFactory
226231
* @param \Magento\Customer\Api\GroupRepositoryInterface $groupRepository
227232
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
@@ -231,9 +236,10 @@ class Create extends \Magento\Framework\Object implements \Magento\Checkout\Mode
231236
* @param \Magento\Framework\Object\Factory $objectFactory
232237
* @param \Magento\Quote\Model\QuoteRepository $quoteRepository
233238
* @param \Magento\Customer\Api\AccountManagementInterface $accountManagement
234-
* @param \Magento\Customer\Api\Data\CustomerDataBuilder $customerBuilder
239+
* @param \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory
235240
* @param \Magento\Customer\Model\Customer\Mapper $customerMapper
236241
* @param \Magento\Quote\Model\QuoteManagement $quoteManagement
242+
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
237243
* @param array $data
238244
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
239245
*/
@@ -249,7 +255,7 @@ public function __construct(
249255
Product\Quote\Initializer $quoteInitializer,
250256
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
251257
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
252-
\Magento\Customer\Api\Data\AddressDataBuilder $addressBuilder,
258+
\Magento\Customer\Api\Data\AddressInterfaceFactory $addressFactory,
253259
\Magento\Customer\Model\Metadata\FormFactory $metadataFormFactory,
254260
\Magento\Customer\Api\GroupRepositoryInterface $groupRepository,
255261
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
@@ -259,9 +265,10 @@ public function __construct(
259265
\Magento\Framework\Object\Factory $objectFactory,
260266
\Magento\Quote\Model\QuoteRepository $quoteRepository,
261267
\Magento\Customer\Api\AccountManagementInterface $accountManagement,
262-
\Magento\Customer\Api\Data\CustomerDataBuilder $customerBuilder,
268+
\Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory,
263269
\Magento\Customer\Model\Customer\Mapper $customerMapper,
264270
\Magento\Quote\Model\QuoteManagement $quoteManagement,
271+
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
265272
array $data = []
266273
) {
267274
$this->_objectManager = $objectManager;
@@ -275,9 +282,9 @@ public function __construct(
275282
$this->messageManager = $messageManager;
276283
$this->customerRepository = $customerRepository;
277284
$this->addressRepository = $addressRepository;
278-
$this->addressBuilder = $addressBuilder;
285+
$this->addressFactory = $addressFactory;
279286
$this->_metadataFormFactory = $metadataFormFactory;
280-
$this->customerBuilder = $customerBuilder;
287+
$this->customerFactory = $customerFactory;
281288
$this->groupRepository = $groupRepository;
282289
$this->_scopeConfig = $scopeConfig;
283290
$this->emailSender = $emailSender;
@@ -288,6 +295,7 @@ public function __construct(
288295
$this->accountManagement = $accountManagement;
289296
$this->customerMapper = $customerMapper;
290297
$this->quoteManagement = $quoteManagement;
298+
$this->dataObjectHelper = $dataObjectHelper;
291299
parent::__construct($data);
292300
}
293301

@@ -1539,8 +1547,8 @@ public function setAccountData($accountData)
15391547
$request = $form->prepareRequest($accountData);
15401548
$data = $form->extractData($request);
15411549
$data = $form->restoreData($data);
1542-
$customer = $this->customerBuilder->mergeDataObjectWithArray($customer, $data)
1543-
->create();
1550+
$customer = $this->customerFactory->create();
1551+
$this->dataObjectHelper->populateWithArray($customer, $data);
15441552
$this->getQuote()->updateCustomerData($customer);
15451553
$data = [];
15461554

@@ -1658,8 +1666,8 @@ protected function _validateCustomerData(\Magento\Customer\Api\Data\CustomerInte
16581666
}
16591667
}
16601668

1661-
return $this->customerBuilder->mergeDataObjectWithArray($customer, $data)
1662-
->create();
1669+
$this->dataObjectHelper->populateWithArray($customer, $data);
1670+
return $customer;
16631671
}
16641672

16651673
/**
@@ -1683,24 +1691,21 @@ public function _prepareCustomer()
16831691
if ($customer->getId() && !$this->_customerIsInStore($store)) {
16841692
/** Create a new customer record if it is not available in the specified store */
16851693
/** Unset customer ID to ensure that new customer will be created */
1686-
$customer = $this->customerBuilder->populate($customer)
1687-
->setId(null)
1694+
$customer->setId(null)
16881695
->setStoreId($store->getId())
16891696
->setWebsiteId($store->getWebsiteId())
1690-
->setCreatedAt(null)->create();
1697+
->setCreatedAt(null);
16911698
$customer = $this->_validateCustomerData($customer);
16921699
} else if (!$customer->getId()) {
16931700
/** Create new customer */
16941701
$customerBillingAddressDataObject = $this->getBillingAddress()->exportCustomerAddress();
1695-
$customer = $this->customerBuilder->populate($customer)
1696-
->setSuffix($customerBillingAddressDataObject->getSuffix())
1702+
$customer->setSuffix($customerBillingAddressDataObject->getSuffix())
16971703
->setFirstname($customerBillingAddressDataObject->getFirstname())
16981704
->setLastname($customerBillingAddressDataObject->getLastname())
16991705
->setMiddlename($customerBillingAddressDataObject->getMiddlename())
17001706
->setPrefix($customerBillingAddressDataObject->getPrefix())
17011707
->setStoreId($store->getId())
1702-
->setEmail($this->_getNewCustomerEmail())
1703-
->create();
1708+
->setEmail($this->_getNewCustomerEmail());
17041709
$customer = $this->_validateCustomerData($customer);
17051710
}
17061711
$this->getQuote()->setCustomer($customer);
@@ -1713,10 +1718,12 @@ public function _prepareCustomer()
17131718
}
17141719
$this->getQuote()->updateCustomerData($this->getQuote()->getCustomer());
17151720

1716-
$customerData = $this->customerMapper->toFlatArray(
1717-
$this->customerBuilder->populate($this->getQuote()->getCustomer())->setAddresses([])->create()
1718-
);
1719-
foreach ($this->_createCustomerForm($this->getQuote()->getCustomer())->getUserAttributes() as $attribute) {
1721+
$customer = $this->getQuote()->getCustomer();
1722+
$origAddresses = $customer->getAddresses(); // save original addresses
1723+
$customer->setAddresses([]);
1724+
$customerData = $this->customerMapper->toFlatArray($customer);
1725+
$customer->setAddresses($origAddresses); // restore original addresses
1726+
foreach ($this->_createCustomerForm($customer)->getUserAttributes() as $attribute) {
17201727
if (isset($customerData[$attribute->getAttributeCode()])) {
17211728
$quoteCode = sprintf('customer_%s', $attribute->getAttributeCode());
17221729
$this->getQuote()->setData($quoteCode, $customerData[$attribute->getAttributeCode()]);
@@ -1746,36 +1753,35 @@ protected function _prepareCustomerAddress($customer, $quoteCustomerAddress)
17461753
/** Update existing address */
17471754
$existingAddressDataObject = $this->addressRepository->getById($quoteAddressId);
17481755
/** Update customer address data */
1749-
$customerAddress = $this->addressBuilder->mergeDataObjects($existingAddressDataObject, $customerAddress)
1750-
->create();
1756+
$this->dataObjectHelper->mergeDataObjects(
1757+
get_class($existingAddressDataObject),
1758+
$existingAddressDataObject,
1759+
$customerAddress
1760+
);
1761+
$customerAddress = $existingAddressDataObject;
17511762
} elseif ($addressType == \Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_SHIPPING) {
17521763
try {
1753-
$billingAddressDataObject = $customer->getDefaultBilling();
1764+
$billingAddressDataObject = $this->accountManagement->getDefaultBillingAddress($customer->getId());
17541765
} catch (\Exception $e) {
17551766
/** Billing address does not exist. */
17561767
}
17571768
$isShippingAsBilling = $quoteCustomerAddress->getSameAsBilling();
17581769
if (isset($billingAddressDataObject) && $isShippingAsBilling) {
17591770
/** Set existing billing address as default shipping */
1760-
$customerAddress = $this->addressBuilder->populate($billingAddressDataObject)
1761-
->setDefaultShipping(true)
1762-
->create();
1771+
$customerAddress = $billingAddressDataObject;
1772+
$customerAddress->setDefaultShipping(true);
17631773
}
17641774
}
17651775

17661776
switch ($addressType) {
17671777
case \Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_BILLING:
17681778
if (is_null($customer->getDefaultBilling())) {
1769-
$customerAddress = $this->addressBuilder->populate($customerAddress)
1770-
->setDefaultBilling(true)
1771-
->create();
1779+
$customerAddress->setDefaultBilling(true);
17721780
}
17731781
break;
17741782
case \Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_SHIPPING:
17751783
if (is_null($customer->getDefaultShipping())) {
1776-
$customerAddress = $this->addressBuilder->populate($customerAddress)
1777-
->setDefaultShipping(true)
1778-
->create();
1784+
$customerAddress->setDefaultShipping(true);
17791785
}
17801786
break;
17811787
default:

dev/tests/unit/testsuite/Magento/Sales/Model/AdminOrder/CreateTest.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class CreateTest extends \PHPUnit_Framework_TestCase
2626
/** @var \Magento\Customer\Model\Metadata\FormFactory|\PHPUnit_Framework_MockObject_MockObject */
2727
protected $formFactoryMock;
2828

29-
/** @var \Magento\Customer\Api\Data\CustomerDataBuilder|\PHPUnit_Framework_MockObject_MockObject */
30-
protected $customerBuilderMock;
29+
/** @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject */
30+
protected $customerFactoryMock;
3131

3232
/** @var \Magento\Quote\Model\Quote\Item\Updater|\PHPUnit_Framework_MockObject_MockObject */
3333
protected $itemUpdater;
@@ -51,9 +51,9 @@ class CreateTest extends \PHPUnit_Framework_TestCase
5151
protected $addressRepositoryMock;
5252

5353
/**
54-
* @var \Magento\Customer\Api\Data\AddressDataBuilder|\PHPUnit_Framework_MockObject_MockObject
54+
* @var \Magento\Customer\Api\Data\AddressInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
5555
*/
56-
protected $addressBuilderMock;
56+
protected $addressFactoryMock;
5757

5858
/**
5959
* @var \Magento\Customer\Api\GroupRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
@@ -100,9 +100,9 @@ protected function setUp()
100100
'',
101101
false
102102
);
103-
$this->customerBuilderMock = $this->getMock(
104-
'Magento\Customer\Api\Data\CustomerDataBuilder',
105-
['mergeDataObjectWithArray', 'populateWithArray', 'create'],
103+
$this->customerFactoryMock = $this->getMock(
104+
'Magento\Customer\Api\Data\CustomerInterfaceFactory',
105+
['create'],
106106
[],
107107
'',
108108
false
@@ -138,8 +138,8 @@ protected function setUp()
138138
'',
139139
false
140140
);
141-
$this->addressBuilderMock = $this->getMock(
142-
'Magento\Customer\Api\Data\AddressDataBuilder',
141+
$this->addressFactoryMock = $this->getMock(
142+
'Magento\Customer\Api\Data\AddressInterfaceFactory',
143143
[],
144144
[],
145145
'',
@@ -186,9 +186,9 @@ protected function setUp()
186186
'quoteInitializer' => $this->quoteInitializerMock,
187187
'customerRepository' => $this->customerRepositoryMock,
188188
'addressRepository' => $this->addressRepositoryMock,
189-
'addressBuilder' => $this->addressBuilderMock,
189+
'addressFactory' => $this->addressFactoryMock,
190190
'metadataFormFactory' => $this->formFactoryMock,
191-
'customerBuilder' => $this->customerBuilderMock,
191+
'customerFactory' => $this->customerFactoryMock,
192192
'groupRepository' => $this->groupRepositoryMock,
193193
'quoteItemUpdater' => $this->itemUpdater,
194194
'customerMapper' => $this->customerMapper,
@@ -257,10 +257,7 @@ public function testSetAccountData()
257257

258258
$this->formFactoryMock->expects($this->any())->method('create')->will($this->returnValue($customerFormMock));
259259
$this->sessionQuoteMock->expects($this->any())->method('getQuote')->will($this->returnValue($quoteMock));
260-
$this->customerBuilderMock->expects($this->any())
261-
->method('mergeDataObjectWithArray')
262-
->will($this->returnSelf());
263-
$this->customerBuilderMock->expects($this->any())->method('create')->will($this->returnValue($customerMock));
260+
$this->customerFactoryMock->expects($this->any())->method('create')->will($this->returnValue($customerMock));
264261

265262
$this->groupRepositoryMock->expects($this->once())
266263
->method('getById')

0 commit comments

Comments
 (0)