Skip to content

Commit fd339f9

Browse files
author
Yurii Torbyk
committed
MAGETWO-33058: Refactor Checkout module
1 parent 8892e7b commit fd339f9

32 files changed

+554
-458
lines changed

app/code/Magento/Checkout/Controller/Action.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,29 @@ abstract class Action extends \Magento\Framework\App\Action\Action
2929
*/
3030
protected $accountManagement;
3131

32+
/**
33+
* @var \Magento\Framework\Controller\Result\RedirectFactory
34+
*/
35+
protected $resultRedirectFactory;
36+
3237
/**
3338
* @param \Magento\Framework\App\Action\Context $context
3439
* @param \Magento\Customer\Model\Session $customerSession
3540
* @param CustomerRepositoryInterface $customerRepository
3641
* @param AccountManagementInterface $accountManagement
42+
* @param \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
3743
*/
3844
public function __construct(
3945
\Magento\Framework\App\Action\Context $context,
4046
\Magento\Customer\Model\Session $customerSession,
4147
CustomerRepositoryInterface $customerRepository,
42-
AccountManagementInterface $accountManagement
48+
AccountManagementInterface $accountManagement,
49+
\Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
4350
) {
4451
$this->_customerSession = $customerSession;
4552
$this->customerRepository = $customerRepository;
4653
$this->accountManagement = $accountManagement;
54+
$this->resultRedirectFactory = $resultRedirectFactory;
4755
parent::__construct($context);
4856
}
4957

@@ -54,7 +62,7 @@ public function __construct(
5462
*
5563
* @param bool $redirect - stop dispatch and redirect?
5664
* @param bool $addErrors - add error messages?
57-
* @return bool
65+
* @return bool|\Magento\Framework\Controller\Result\Redirect
5866
*/
5967
protected function _preDispatchValidateCustomer($redirect = true, $addErrors = true)
6068
{
@@ -73,8 +81,8 @@ protected function _preDispatchValidateCustomer($redirect = true, $addErrors = t
7381
}
7482
}
7583
if ($redirect) {
76-
$this->_redirect('customer/account/edit');
7784
$this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
85+
return $this->resultRedirectFactory->create()->setPath('customer/account/edit');
7886
}
7987
return false;
8088
}

app/code/Magento/Checkout/Controller/Cart.php

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

88
use Magento\Catalog\Controller\Product\View\ViewInterface;
99
use Magento\Checkout\Model\Cart as CustomerCart;
10+
use Magento\Store\Model\ScopeInterface;
1011

1112
/**
1213
* Shopping cart controller
@@ -38,56 +39,62 @@ class Cart extends \Magento\Framework\App\Action\Action implements ViewInterface
3839
*/
3940
protected $cart;
4041

42+
/**
43+
* @var \Magento\Framework\Controller\Result\RedirectFactory
44+
*/
45+
protected $resultRedirectFactory;
46+
4147
/**
4248
* @param \Magento\Framework\App\Action\Context $context
4349
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
4450
* @param \Magento\Checkout\Model\Session $checkoutSession
4551
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
4652
* @param \Magento\Core\App\Action\FormKeyValidator $formKeyValidator
4753
* @param CustomerCart $cart
54+
* @param \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
4855
*/
4956
public function __construct(
5057
\Magento\Framework\App\Action\Context $context,
5158
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
5259
\Magento\Checkout\Model\Session $checkoutSession,
5360
\Magento\Store\Model\StoreManagerInterface $storeManager,
5461
\Magento\Core\App\Action\FormKeyValidator $formKeyValidator,
55-
CustomerCart $cart
62+
CustomerCart $cart,
63+
\Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
5664
) {
5765
$this->_formKeyValidator = $formKeyValidator;
5866
$this->_scopeConfig = $scopeConfig;
5967
$this->_checkoutSession = $checkoutSession;
6068
$this->_storeManager = $storeManager;
6169
$this->cart = $cart;
70+
$this->resultRedirectFactory = $resultRedirectFactory;
6271
parent::__construct($context);
6372
}
6473

6574
/**
6675
* Set back redirect url to response
6776
*
68-
* @return $this
77+
* @return \Magento\Framework\Controller\Result\Redirect
6978
*/
7079
protected function _goBack()
7180
{
7281
$returnUrl = $this->getRequest()->getParam('return_url');
82+
$resultRedirect = $this->resultRedirectFactory->create();
7383
if ($returnUrl && $this->_isInternalUrl($returnUrl)) {
7484
$this->messageManager->getMessages()->clear();
75-
$this->getResponse()->setRedirect($returnUrl);
76-
} elseif (!$this->_scopeConfig->getValue(
77-
'checkout/cart/redirect_to_cart',
78-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
79-
) && !$this->getRequest()->getParam(
80-
'in_cart'
81-
) && ($backUrl = $this->_redirect->getRefererUrl())
85+
$resultRedirect->setUrl($returnUrl);
86+
} elseif (!$this->_scopeConfig->getValue('checkout/cart/redirect_to_cart', ScopeInterface::SCOPE_STORE)
87+
&& !$this->getRequest()->getParam('in_cart')
88+
&& ($backUrl = $this->_redirect->getRefererUrl())
8289
) {
83-
$this->getResponse()->setRedirect($backUrl);
90+
$resultRedirect->setUrl($backUrl);
8491
} else {
8592
if ($this->getRequest()->getActionName() == 'add' && !$this->getRequest()->getParam('in_cart')) {
8693
$this->_checkoutSession->setContinueShoppingUrl($this->_redirect->getRefererUrl());
8794
}
88-
$this->_redirect('checkout/cart');
95+
$resultRedirect->setPath('checkout/cart');
8996
}
90-
return $this;
97+
return $resultRedirect;
9198
}
9299

93100
/**

app/code/Magento/Checkout/Controller/Cart/Add.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Add extends \Magento\Checkout\Controller\Cart
2727
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
2828
* @param \Magento\Core\App\Action\FormKeyValidator $formKeyValidator
2929
* @param CustomerCart $cart
30+
* @param \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
3031
* @param ProductRepositoryInterface $productRepository
3132
*/
3233
public function __construct(
@@ -36,9 +37,18 @@ public function __construct(
3637
\Magento\Store\Model\StoreManagerInterface $storeManager,
3738
\Magento\Core\App\Action\FormKeyValidator $formKeyValidator,
3839
CustomerCart $cart,
40+
\Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
3941
ProductRepositoryInterface $productRepository
4042
) {
41-
parent::__construct($context, $scopeConfig, $checkoutSession, $storeManager, $formKeyValidator, $cart);
43+
parent::__construct(
44+
$context,
45+
$scopeConfig,
46+
$checkoutSession,
47+
$storeManager,
48+
$formKeyValidator,
49+
$cart,
50+
$resultRedirectFactory
51+
);
4252
$this->productRepository = $productRepository;
4353
}
4454

@@ -64,7 +74,7 @@ protected function _initProduct()
6474
/**
6575
* Add product to shopping cart action
6676
*
67-
* @return void
77+
* @return \Magento\Framework\Controller\Result\Redirect
6878
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
6979
*/
7080
public function execute()
@@ -85,8 +95,7 @@ public function execute()
8595
* Check product availability
8696
*/
8797
if (!$product) {
88-
$this->_goBack();
89-
return;
98+
return $this->_goBack();
9099
}
91100

92101
$this->cart->addProduct($product, $params);
@@ -114,7 +123,7 @@ public function execute()
114123
);
115124
$this->messageManager->addSuccess($message);
116125
}
117-
$this->_goBack();
126+
return $this->_goBack();
118127
}
119128
} catch (\Magento\Framework\Model\Exception $e) {
120129
if ($this->_checkoutSession->getUseNotice(true)) {
@@ -132,15 +141,15 @@ public function execute()
132141

133142
$url = $this->_checkoutSession->getRedirectUrl(true);
134143
if ($url) {
135-
$this->getResponse()->setRedirect($url);
144+
return $this->resultRedirectFactory->create()->setUrl($url);
136145
} else {
137146
$cartUrl = $this->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl();
138-
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl($cartUrl));
147+
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl($cartUrl));
139148
}
140149
} catch (\Exception $e) {
141150
$this->messageManager->addException($e, __('We cannot add this item to your shopping cart'));
142151
$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
143-
$this->_goBack();
152+
return $this->_goBack();
144153
}
145154
}
146155
}

app/code/Magento/Checkout/Controller/Cart/Addgroup.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@
99
class Addgroup extends \Magento\Checkout\Controller\Cart
1010
{
1111
/**
12-
* @return void
12+
* @return \Magento\Framework\Controller\Result\Redirect
1313
*/
1414
public function execute()
1515
{
1616
$orderItemIds = $this->getRequest()->getParam('order_items', []);
1717
if (is_array($orderItemIds)) {
18-
$itemsCollection = $this->_objectManager->create(
19-
'Magento\Sales\Model\Order\Item'
20-
)->getCollection()->addIdFilter(
21-
$orderItemIds
22-
)->load();
18+
$itemsCollection = $this->_objectManager->create('Magento\Sales\Model\Order\Item')
19+
->getCollection()
20+
->addIdFilter($orderItemIds)
21+
->load();
2322
/* @var $itemsCollection \Magento\Sales\Model\Resource\Order\Item\Collection */
2423
foreach ($itemsCollection as $item) {
2524
try {
@@ -33,12 +32,12 @@ public function execute()
3332
} catch (\Exception $e) {
3433
$this->messageManager->addException($e, __('We cannot add this item to your shopping cart'));
3534
$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
36-
$this->_goBack();
35+
return $this->_goBack();
3736
}
3837
}
3938
$this->cart->save();
4039
$this->_checkoutSession->setCartWasUpdated(true);
4140
}
42-
$this->_goBack();
41+
return $this->_goBack();
4342
}
4443
}

app/code/Magento/Checkout/Controller/Cart/Configure.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Configure extends \Magento\Checkout\Controller\Cart
2020
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
2121
* @param \Magento\Core\App\Action\FormKeyValidator $formKeyValidator
2222
* @param \Magento\Checkout\Model\Cart $cart
23+
* @param \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
2324
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
2425
*/
2526
public function __construct(
@@ -29,16 +30,25 @@ public function __construct(
2930
\Magento\Store\Model\StoreManagerInterface $storeManager,
3031
\Magento\Core\App\Action\FormKeyValidator $formKeyValidator,
3132
\Magento\Checkout\Model\Cart $cart,
33+
\Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
3234
\Magento\Framework\View\Result\PageFactory $resultPageFactory
3335
) {
34-
parent::__construct($context, $scopeConfig, $checkoutSession, $storeManager, $formKeyValidator, $cart);
36+
parent::__construct(
37+
$context,
38+
$scopeConfig,
39+
$checkoutSession,
40+
$storeManager,
41+
$formKeyValidator,
42+
$cart,
43+
$resultRedirectFactory
44+
);
3545
$this->resultPageFactory = $resultPageFactory;
3646
}
3747

3848
/**
3949
* Action to reconfigure cart item
4050
*
41-
* @return \Magento\Framework\View\Result\Page
51+
* @return \Magento\Framework\View\Result\Page|\Magento\Framework\Controller\Result\Redirect
4252
*/
4353
public function execute()
4454
{
@@ -53,8 +63,7 @@ public function execute()
5363
try {
5464
if (!$quoteItem || $productId != $quoteItem->getProduct()->getId()) {
5565
$this->messageManager->addError(__("We can't find the quote item."));
56-
$this->_redirect('checkout/cart');
57-
return;
66+
return $this->resultRedirectFactory->create()->setPath('checkout/cart');
5867
}
5968

6069
$params = new \Magento\Framework\Object();
@@ -74,8 +83,7 @@ public function execute()
7483
} catch (\Exception $e) {
7584
$this->messageManager->addError(__('We cannot configure the product.'));
7685
$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
77-
$this->_goBack();
78-
return;
86+
return $this->_goBack();
7987
}
8088
}
8189
}

app/code/Magento/Checkout/Controller/Cart/CouponPost.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class CouponPost extends \Magento\Checkout\Controller\Cart
2424
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
2525
* @param \Magento\Core\App\Action\FormKeyValidator $formKeyValidator
2626
* @param CustomerCart $cart
27+
* @param \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
2728
* @param \Magento\Quote\Model\QuoteRepository $quoteRepository
2829
*/
2930
public function __construct(
@@ -33,6 +34,7 @@ public function __construct(
3334
\Magento\Store\Model\StoreManagerInterface $storeManager,
3435
\Magento\Core\App\Action\FormKeyValidator $formKeyValidator,
3536
CustomerCart $cart,
37+
\Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
3638
\Magento\Quote\Model\QuoteRepository $quoteRepository
3739
) {
3840
parent::__construct(
@@ -41,15 +43,16 @@ public function __construct(
4143
$checkoutSession,
4244
$storeManager,
4345
$formKeyValidator,
44-
$cart
46+
$cart,
47+
$resultRedirectFactory
4548
);
4649
$this->quoteRepository = $quoteRepository;
4750
}
4851

4952
/**
5053
* Initialize coupon
5154
*
52-
* @return void
55+
* @return \Magento\Framework\Controller\Result\Redirect
5356
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
5457
* @SuppressWarnings(PHPMD.NPathComplexity)
5558
*/
@@ -59,20 +62,16 @@ public function execute()
5962
* No reason continue with empty shopping cart
6063
*/
6164
if (!$this->cart->getQuote()->getItemsCount()) {
62-
$this->_goBack();
63-
return;
65+
return $this->_goBack();
6466
}
6567

66-
$couponCode = $this->getRequest()->getParam(
67-
'remove'
68-
) == 1 ? '' : trim(
69-
$this->getRequest()->getParam('coupon_code')
70-
);
68+
$couponCode = $this->getRequest()->getParam('remove') == 1
69+
? ''
70+
: trim($this->getRequest()->getParam('coupon_code'));
7171
$oldCouponCode = $this->cart->getQuote()->getCouponCode();
7272

7373
if (!strlen($couponCode) && !strlen($oldCouponCode)) {
74-
$this->_goBack();
75-
return;
74+
return $this->_goBack();
7675
}
7776

7877
try {
@@ -109,6 +108,6 @@ public function execute()
109108
$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
110109
}
111110

112-
$this->_goBack();
111+
return $this->_goBack();
113112
}
114113
}

app/code/Magento/Checkout/Controller/Cart/Delete.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Delete extends \Magento\Checkout\Controller\Cart
1111
/**
1212
* Delete shopping cart item action
1313
*
14-
* @return void
14+
* @return \Magento\Framework\Controller\Result\Redirect
1515
*/
1616
public function execute()
1717
{
@@ -25,6 +25,6 @@ public function execute()
2525
}
2626
}
2727
$defaultUrl = $this->_objectManager->create('Magento\Framework\UrlInterface')->getUrl('*/*');
28-
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl($defaultUrl));
28+
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl($defaultUrl));
2929
}
3030
}

0 commit comments

Comments
 (0)