Skip to content

Commit b639708

Browse files
author
Robert He
committed
MAGETWO-33109 : Refactor code that uses customer builders in Quote module
-- changes from code review
1 parent ce806d2 commit b639708

File tree

4 files changed

+10
-20
lines changed

4 files changed

+10
-20
lines changed

app/code/Magento/Quote/Model/Observer/Frontend/Quote/Address/CollectTotals.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ class CollectTotals
3434
*/
3535
protected $groupManagement;
3636

37-
/**
38-
* @var \Magento\Framework\Api\DataObjectHelper
39-
*/
40-
protected $dataObjectHelper;
41-
4237
/**
4338
* Initialize dependencies.
4439
*
@@ -47,22 +42,19 @@ class CollectTotals
4742
* @param VatValidator $vatValidator
4843
* @param \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory
4944
* @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
50-
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
5145
*/
5246
public function __construct(
5347
\Magento\Customer\Helper\Address $customerAddressHelper,
5448
\Magento\Customer\Model\Vat $customerVat,
5549
VatValidator $vatValidator,
5650
\Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory,
57-
\Magento\Customer\Api\GroupManagementInterface $groupManagement,
58-
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper
51+
\Magento\Customer\Api\GroupManagementInterface $groupManagement
5952
) {
6053
$this->customerVat = $customerVat;
6154
$this->customerAddressHelper = $customerAddressHelper;
6255
$this->vatValidator = $vatValidator;
6356
$this->customerDataFactory = $customerDataFactory;
6457
$this->groupManagement = $groupManagement;
65-
$this->dataObjectHelper = $dataObjectHelper;
6658
}
6759

6860
/**
@@ -107,8 +99,7 @@ public function dispatch(\Magento\Framework\Event\Observer $observer)
10799
if ($groupId) {
108100
$quoteAddress->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId());
109101
$quote->setCustomerGroupId($groupId);
110-
$customer = $this->customerDataFactory->create();
111-
$this->dataObjectHelper->populateWithArray($customer, ['group_id' => $groupId]);
102+
$customer->setGroupId($groupId);
112103
$quote->setCustomer($customer);
113104
}
114105
}

app/code/Magento/Quote/Model/Quote.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -831,16 +831,16 @@ public function setCustomer(\Magento\Customer\Api\Data\CustomerInterface $custom
831831
/* @TODO: Remove the method after all external usages are refactored in MAGETWO-19930 */
832832
$this->_customer = $customer;
833833
$this->setCustomerId($customer->getId());
834-
$customerData = $this->customerDataFactory->create();
835-
$customerData->populate($customer);
836-
$customerData->setAddresses([]);
834+
$origAddresses = $customer->getAddresses();
835+
$customer->setAddresses([]);
837836
$customerDataFlatArray = $this->objectFactory->create(
838837
$this->extensibleDataObjectConverter->toFlatArray(
839-
$customerData,
838+
$customer,
840839
[],
841840
'\Magento\Customer\Api\Data\CustomerInterface'
842841
)
843842
);
843+
$customer->setAddresses($origAddresses);
844844
$this->_objectCopyService->copyFieldsetToTarget('customer_account', 'to_quote', $customerDataFlatArray, $this);
845845

846846
return $this;

dev/tests/unit/testsuite/Magento/Quote/Model/Observer/Frontend/Quote/Address/CollectTotalsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public function testDispatchWithDefaultCustomerGroupId()
280280
->method('setPrevQuoteCustomerGroupId')
281281
->with('customerGroupId');
282282
$this->quoteMock->expects($this->once())->method('setCustomerGroupId')->with('defaultCustomerGroupId');
283-
$this->dataObjectHelperMock->expects($this->once())
283+
$this->dataObjectHelperMock->expects($this->never())
284284
->method('populateWithArray')
285285
->with($this->customerMock, ['group_id' => 'defaultCustomerGroupId'])
286286
->will($this->returnSelf());
@@ -336,7 +336,7 @@ public function testDispatchWithCustomerCountryInEU()
336336

337337
$this->quoteMock->expects($this->once())->method('setCustomerGroupId')->with('customerGroupId');
338338
$this->quoteMock->expects($this->once())->method('setCustomer')->with($this->customerMock);
339-
$this->dataObjectHelperMock->expects($this->once())
339+
$this->dataObjectHelperMock->expects($this->never())
340340
->method('populateWithArray')
341341
->with($this->customerMock, ['group_id' => 'customerGroupId'])
342342
->will($this->returnSelf());

lib/internal/Magento/Framework/Api/DataObjectHelper.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ protected function setComplexValue(
175175
* @param ExtensibleDataInterface $firstDataObject
176176
* @param ExtensibleDataInterface $secondDataObject
177177
* @return $this
178+
* @throws \LogicException
178179
*/
179180
public function mergeDataObjects(
180181
$className,
@@ -184,10 +185,8 @@ public function mergeDataObjects(
184185
if (!$firstDataObject instanceof $className || !$secondDataObject instanceof $className) {
185186
throw new \LogicException('Wrong prototype object given. It can only be of "' . $className . '" type.');
186187
}
187-
$firstObjectArray = $this->objectProcessor->buildOutputDataArray($firstDataObject, $className);
188188
$secondObjectArray = $this->objectProcessor->buildOutputDataArray($secondDataObject, $className);
189-
$this->_setDataValues($this, $firstObjectArray);
190-
$this->_setDataValues($this, $secondObjectArray);
189+
$this->_setDataValues($firstDataObject, $secondObjectArray);
191190
return $this;
192191
}
193192
}

0 commit comments

Comments
 (0)