Skip to content

Commit 43616e0

Browse files
author
Jan Polak
committed
CustomerData class refactor
1 parent 4e074d0 commit 43616e0

File tree

8 files changed

+55
-49
lines changed

8 files changed

+55
-49
lines changed

app/code/Magento/OneTouchOrdering/Controller/Button/PlaceOrder.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
use Magento\Framework\Controller\ResultFactory;
1313
use Magento\Framework\Data\Form\FormKey\Validator;
1414
use Magento\Framework\Exception\NoSuchEntityException;
15-
use Magento\OneTouchOrdering\Model\CustomerData;
15+
use Magento\OneTouchOrdering\Model\CustomerDataGetter;
16+
use Magento\OneTouchOrdering\Model\CustomerDataGetterFactory;
1617
use Magento\OneTouchOrdering\Model\PlaceOrder as PlaceOrderModel;
1718
use Magento\Store\Model\StoreManagerInterface;
1819

@@ -40,7 +41,7 @@ class PlaceOrder extends \Magento\Framework\App\Action\Action
4041
*/
4142
private $customerSession;
4243
/**
43-
* @var CustomerData
44+
* @var CustomerDataGetter
4445
*/
4546
private $customerData;
4647
/**
@@ -56,7 +57,7 @@ class PlaceOrder extends \Magento\Framework\App\Action\Action
5657
* @param PlaceOrderModel $placeOrder
5758
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
5859
* @param Session $customerSession
59-
* @param CustomerData $customerData
60+
* @param CustomerDataGetterFactory $customerData
6061
* @param Validator $formKeyValidator
6162
*/
6263
public function __construct(
@@ -66,7 +67,7 @@ public function __construct(
6667
PlaceOrderModel $placeOrder,
6768
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
6869
Session $customerSession,
69-
CustomerData $customerData,
70+
CustomerDataGetterFactory $customerData,
7071
Validator $formKeyValidator
7172
) {
7273
parent::__construct($context);
@@ -89,7 +90,7 @@ public function execute()
8990
$product = $this->initProduct();
9091
$params = $this->getRequest()->getParams();
9192
try {
92-
$customerData = $this->customerData->setCustomer($this->customerSession->getCustomer());
93+
$customerData = $this->customerData->create($this->customerSession->getCustomer());
9394
$orderId = $this->placeOrder->placeOrder($product, $customerData, $params);
9495
} catch (NoSuchEntityException $e) {
9596
return $this->createResponse($errorMsg, false);

app/code/Magento/OneTouchOrdering/Model/CustomerData.php renamed to app/code/Magento/OneTouchOrdering/Model/CustomerDataGetter.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88
use Magento\Customer\Model\Customer;
99
use Magento\Framework\Exception\LocalizedException;
1010

11-
class CustomerData
11+
class CustomerDataGetter
1212
{
1313
/**
1414
* @var Customer
1515
*/
1616
private $customer;
1717

18+
public function __construct(
19+
Customer $customer
20+
) {
21+
$this->customer = $customer;
22+
}
23+
1824
/**
1925
* @return \Magento\Customer\Api\Data\AddressInterface
2026
*/
@@ -56,29 +62,12 @@ public function getCustomerId(): int
5662
return $this->getCustomer()->getId();
5763
}
5864

59-
/**
60-
* @param Customer $customer
61-
* @return $this
62-
*/
63-
public function setCustomer(Customer $customer)
64-
{
65-
$this->customer = $customer;
66-
67-
return $this;
68-
}
69-
7065
/**
7166
* @return Customer
7267
* @throws LocalizedException
7368
*/
7469
private function getCustomer(): Customer
7570
{
76-
if (!$this->customer) {
77-
throw new LocalizedException(
78-
__('Something went wrong. Please try again later.')
79-
);
80-
}
81-
8271
return $this->customer;
8372
}
8473
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Magento\OneTouchOrdering\Model;
4+
5+
use Magento\Customer\Model\Customer;
6+
use Magento\Framework\ObjectManager\ObjectManager;
7+
8+
class CustomerDataGetterFactory
9+
{
10+
/**
11+
* @var ObjectManager
12+
*/
13+
private $objectManager;
14+
15+
public function __construct(
16+
ObjectManager $objectManager
17+
) {
18+
$this->objectManager = $objectManager;
19+
}
20+
21+
public function create(Customer $customer)
22+
{
23+
return $this->objectManager->create(CustomerDataGetter::class, ['customer' => $customer]);
24+
}
25+
}

app/code/Magento/OneTouchOrdering/Model/PlaceOrder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ public function __construct(
5858

5959
/**
6060
* @param Product $product
61-
* @param CustomerData $customerData
61+
* @param CustomerDataGetter $customerData
6262
* @param array $params
63-
* @throws \Exception
63+
* @throws Exception
6464
* @return int
6565
*/
66-
public function placeOrder(Product $product, CustomerData $customerData, array $params): int
66+
public function placeOrder(Product $product, CustomerDataGetter $customerData, array $params): int
6767
{
6868
$paramsObject = $this->getProductRequest($params);
6969
$quote = $this->prepareQuote->prepare($customerData, $paramsObject);

app/code/Magento/OneTouchOrdering/Model/PrepareQuote.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public function __construct(
4444
}
4545

4646
/**
47-
* @param CustomerData $customerData
47+
* @param CustomerDataGetter $customerData
4848
* @param DataObject $params
4949
* @return Quote
5050
*/
51-
public function prepare(CustomerData $customerData, DataObject $params): Quote
51+
public function prepare(CustomerDataGetter $customerData, DataObject $params): Quote
5252
{
5353
$store = $this->storeManager->getStore();
5454
$quote = $this->quoteFactory->create();

app/code/Magento/OneTouchOrdering/Test/Unit/Model/CustomerDataTest.php renamed to app/code/Magento/OneTouchOrdering/Test/Unit/Model/CustomerDataGetterTest.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
use Magento\Customer\Model\Address;
99
use Magento\Customer\Model\Customer;
1010
use Magento\Framework\Exception\LocalizedException;
11-
use Magento\OneTouchOrdering\Model\CustomerData;
11+
use Magento\OneTouchOrdering\Model\CustomerDataGetter;
1212
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1313
use PHPUnit\Framework\TestCase;
1414

15-
class CustomerDataTest extends TestCase
15+
class CustomerDataGetterTest extends TestCase
1616
{
1717
/**
1818
* @var \PHPUnit_Framework_MockObject_MockObject|Customer
1919
*/
2020
private $customer;
2121
/**
22-
* @var CustomerData
22+
* @var CustomerDataGetter
2323
*/
2424
private $customerData;
2525
/**
@@ -47,12 +47,13 @@ public function setUp()
4747
\Magento\Customer\Api\Data\AddressInterface::class
4848
);
4949

50-
$this->customerData = $objectManager->getObject(CustomerData::class);
50+
$this->customerData = $objectManager->getObject(CustomerDataGetter::class, [
51+
'customer' => $this->customer
52+
]);
5153
}
5254

5355
public function testGetDefaultBillingAddressDataModel()
5456
{
55-
$this->customerData->setCustomer($this->customer);
5657
$this->customer
5758
->expects($this->once())
5859
->method('getDefaultBillingAddress')
@@ -68,7 +69,6 @@ public function testGetDefaultBillingAddressDataModel()
6869

6970
public function testGetDefaultShippingAddressDataModel()
7071
{
71-
$this->customerData->setCustomer($this->customer);
7272
$this->customer
7373
->expects($this->once())
7474
->method('getDefaultShippingAddress')
@@ -85,7 +85,6 @@ public function testGetDefaultShippingAddressDataModel()
8585
public function testShippingAddressDataModel()
8686
{
8787
$addressId = 123;
88-
$this->customerData->setCustomer($this->customer);
8988
$this->customer
9089
->expects($this->once())
9190
->method('getAddressById')
@@ -102,7 +101,6 @@ public function testShippingAddressDataModel()
102101

103102
public function testGetCustomerDataModel()
104103
{
105-
$this->customerData->setCustomer($this->customer);
106104
$this->customer
107105
->expects($this->once())
108106
->method('getDataModel')
@@ -114,18 +112,11 @@ public function testGetCustomerDataModel()
114112
public function testGetCustomerId()
115113
{
116114
$customerId = 32;
117-
$this->customerData->setCustomer($this->customer);
118115
$this->customer
119116
->expects($this->once())
120117
->method('getId')
121118
->willReturn($customerId);
122119
$result = $this->customerData->getCustomerId();
123120
$this->assertSame($result, $customerId);
124121
}
125-
126-
public function testNoCustomer()
127-
{
128-
$this->expectException(LocalizedException::class);
129-
$this->customerData->getCustomerDataModel();
130-
}
131122
}

app/code/Magento/OneTouchOrdering/Test/Unit/Model/PlaceOrderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use Magento\Catalog\Model\Product;
99
use Magento\Framework\DataObject;
10-
use Magento\OneTouchOrdering\Model\CustomerData;
10+
use Magento\OneTouchOrdering\Model\CustomerDataGetter;
1111
use Magento\OneTouchOrdering\Model\PlaceOrder;
1212
use Magento\OneTouchOrdering\Model\PrepareQuote;
1313
use Magento\OneTouchOrdering\Model\ShippingRateChooser;
@@ -23,7 +23,7 @@ class PlaceOrderTest extends TestCase
2323
*/
2424
private $shippingRateChooser;
2525
/**
26-
* @var \PHPUnit_Framework_MockObject_MockObject|CustomerData
26+
* @var \PHPUnit_Framework_MockObject_MockObject|CustomerDataGetter
2727
*/
2828
private $customerData;
2929
/**
@@ -55,7 +55,7 @@ public function setUp()
5555
{
5656
$objectManager = new ObjectManager($this);
5757

58-
$this->customerData = $this->createMock(CustomerData::class);
58+
$this->customerData = $this->createMock(CustomerDataGetter::class);
5959
$this->cartManagementInterface = $this->createMock(
6060
\Magento\Quote\Api\CartManagementInterface::class
6161
);

app/code/Magento/OneTouchOrdering/Test/Unit/Model/PrepareQuoteTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Magento\Customer\Api\Data\CustomerInterface;
1010
use Magento\Framework\DataObject;
1111
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12-
use Magento\OneTouchOrdering\Model\CustomerData;
12+
use Magento\OneTouchOrdering\Model\CustomerDataGetter;
1313
use Magento\OneTouchOrdering\Model\PrepareQuote;
1414
use Magento\Quote\Model\Quote;
1515
use Magento\Quote\Model\Quote\Address as QuoteAddress;
@@ -21,7 +21,7 @@
2121
class PrepareQuoteTest extends TestCase
2222
{
2323
/**
24-
* @var \PHPUnit_Framework_MockObject_MockObject|CustomerData
24+
* @var \PHPUnit_Framework_MockObject_MockObject|CustomerDataGetter
2525
*/
2626
private $customerData;
2727
/**
@@ -49,7 +49,7 @@ public function setUp()
4949
{
5050
$objectManager = new ObjectManager($this);
5151

52-
$this->customerData = $this->createMock(CustomerData::class);
52+
$this->customerData = $this->createMock(CustomerDataGetter::class);
5353
$this->quoteFactory = $this->createMock(QuoteFactory::class);
5454
$this->quote = $this->getMockBuilder(Quote::class)
5555
->disableOriginalConstructor()

0 commit comments

Comments
 (0)