Skip to content

Commit 82c9ede

Browse files
author
Yuri Kovsher
committed
MAGETWO-36105: Refactor controllers in the Persistent module
1 parent 927e8e2 commit 82c9ede

File tree

4 files changed

+75
-60
lines changed

4 files changed

+75
-60
lines changed

app/code/Magento/Persistent/Controller/Index.php

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,78 @@
55
*/
66
namespace Magento\Persistent\Controller;
77

8+
use Magento\Framework\App\Action\Action;
9+
use Magento\Framework\App\Action\Context;
10+
use Magento\Persistent\Model\QuoteManager;
11+
use Magento\Checkout\Model\Session as CheckoutSession;
12+
use Magento\Customer\Model\Session as CustomerSession;
13+
use Magento\Persistent\Helper\Session as SessionHelper;
14+
use Magento\Framework\Controller\Result\RedirectFactory;
15+
816
/**
917
* Persistent front controller
1018
*/
11-
class Index extends \Magento\Framework\App\Action\Action
19+
class Index extends Action
1220
{
1321
/**
14-
* Whether clear checkout session when logout
22+
* Persistent observer
1523
*
16-
* @var bool
24+
* @var \Magento\Persistent\Model\Observer
1725
*/
18-
protected $_clearCheckoutSession = true;
26+
protected $quoteManager;
27+
28+
/**
29+
* Checkout session
30+
*
31+
* @var \Magento\Checkout\Model\Session
32+
*/
33+
protected $checkoutSession;
1934

2035
/**
2136
* Customer session
2237
*
2338
* @var \Magento\Customer\Model\Session
2439
*/
25-
protected $_customerSession;
40+
protected $customerSession;
2641

2742
/**
28-
* Checkout session
29-
*
30-
* @var \Magento\Checkout\Model\Session
43+
* @var \Magento\Persistent\Helper\Session
3144
*/
32-
protected $_checkoutSession;
45+
protected $sessionHelper;
3346

3447
/**
35-
* Persistent observer
48+
* @var \Magento\Framework\Controller\Result\RedirectFactory
49+
*/
50+
protected $resultRedirectFactory;
51+
52+
/**
53+
* Whether clear checkout session when logout
3654
*
37-
* @var \Magento\Persistent\Model\Observer
55+
* @var bool
3856
*/
39-
protected $quoteManager;
57+
protected $clearCheckoutSession = true;
4058

4159
/**
4260
* @param \Magento\Framework\App\Action\Context $context
4361
* @param \Magento\Persistent\Model\QuoteManager $quoteManager
4462
* @param \Magento\Checkout\Model\Session $checkoutSession
4563
* @param \Magento\Customer\Model\Session $customerSession
64+
* @param \Magento\Persistent\Helper\Session $sessionHelper
65+
* @param \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
4666
*/
4767
public function __construct(
48-
\Magento\Framework\App\Action\Context $context,
49-
\Magento\Persistent\Model\QuoteManager $quoteManager,
50-
\Magento\Checkout\Model\Session $checkoutSession,
51-
\Magento\Customer\Model\Session $customerSession
68+
Context $context,
69+
QuoteManager $quoteManager,
70+
CheckoutSession $checkoutSession,
71+
CustomerSession $customerSession,
72+
SessionHelper $sessionHelper,
73+
RedirectFactory $resultRedirectFactory
5274
) {
5375
$this->quoteManager = $quoteManager;
54-
$this->_checkoutSession = $checkoutSession;
55-
$this->_customerSession = $customerSession;
76+
$this->checkoutSession = $checkoutSession;
77+
$this->customerSession = $customerSession;
78+
$this->sessionHelper = $sessionHelper;
79+
$this->resultRedirectFactory = $resultRedirectFactory;
5680
parent::__construct($context);
5781
}
5882

@@ -64,17 +88,7 @@ public function __construct(
6488
*/
6589
public function setClearCheckoutSession($clear = true)
6690
{
67-
$this->_clearCheckoutSession = $clear;
91+
$this->clearCheckoutSession = $clear;
6892
return $this;
6993
}
70-
71-
/**
72-
* Retrieve 'persistent session' helper instance
73-
*
74-
* @return \Magento\Persistent\Helper\Session
75-
*/
76-
protected function _getHelper()
77-
{
78-
return $this->_objectManager->get('Magento\Persistent\Helper\Session');
79-
}
8094
}
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
<?php
22
/**
3-
*
43
* Copyright © 2015 Magento. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\Persistent\Controller\Index;
87

9-
class ExpressCheckout extends \Magento\Persistent\Controller\Index
8+
use Magento\Persistent\Controller\Index;
9+
10+
class ExpressCheckout extends Index
1011
{
1112
/**
1213
* Add appropriate session message and redirect to shopping cart
1314
*
14-
* @return void
15+
* @return \Magento\Framework\Controller\Result\Redirect
1516
*/
1617
public function execute()
1718
{
1819
$this->messageManager->addNotice(__('Your shopping cart has been updated with new prices.'));
19-
$this->_redirect('checkout/cart');
20+
$resultRedirect = $this->resultRedirectFactory->create();
21+
$resultRedirect->setPath('checkout/cart');
22+
return $resultRedirect;
2023
}
2124
}
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
11
<?php
22
/**
3-
*
43
* Copyright © 2015 Magento. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\Persistent\Controller\Index;
87

9-
class SaveMethod extends \Magento\Persistent\Controller\Index
10-
{
11-
/**
12-
* @var \Magento\Persistent\Model\QuoteManager
13-
*/
14-
protected $quoteManager;
8+
use Magento\Persistent\Controller\Index;
159

10+
class SaveMethod extends Index
11+
{
1612
/**
1713
* Save onepage checkout method to be register
1814
*
19-
* @return void
15+
* @return \Magento\Framework\Controller\Result\Redirect
2016
*/
2117
public function execute()
2218
{
23-
if ($this->_getHelper()->isPersistent()) {
24-
$this->_getHelper()->getSession()->removePersistentCookie();
25-
if (!$this->_customerSession->isLoggedIn()) {
26-
$this->_customerSession->setCustomerId(null)->setCustomerGroupId(null);
19+
if ($this->sessionHelper->isPersistent()) {
20+
$this->sessionHelper->getSession()->removePersistentCookie();
21+
if (!$this->customerSession->isLoggedIn()) {
22+
$this->customerSession->setCustomerId(null)->setCustomerGroupId(null);
2723
}
28-
2924
$this->quoteManager->setGuest();
3025
}
31-
3226
$checkoutUrl = $this->_redirect->getRefererUrl();
33-
$this->getResponse()->setRedirect($checkoutUrl . (strpos($checkoutUrl, '?') ? '&' : '?') . 'register');
27+
$resultRedirect = $this->resultRedirectFactory->create();
28+
$resultRedirect->setPath($checkoutUrl . (strpos($checkoutUrl, '?') ? '&' : '?') . 'register');
29+
return $resultRedirect;
3430
}
3531
}
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,42 @@
11
<?php
22
/**
3-
*
43
* Copyright © 2015 Magento. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\Persistent\Controller\Index;
87

9-
class UnsetCookie extends \Magento\Persistent\Controller\Index
8+
use Magento\Persistent\Controller\Index;
9+
10+
class UnsetCookie extends Index
1011
{
1112
/**
1213
* Revert all persistent data
1314
*
1415
* @return $this
1516
*/
16-
protected function _cleanup()
17+
protected function cleanup()
1718
{
1819
$this->_eventManager->dispatch('persistent_session_expired');
19-
$this->_customerSession->setCustomerId(null)->setCustomerGroupId(null);
20-
if ($this->_clearCheckoutSession) {
21-
$this->_checkoutSession->clearStorage();
20+
$this->customerSession->setCustomerId(null)->setCustomerGroupId(null);
21+
if ($this->clearCheckoutSession) {
22+
$this->checkoutSession->clearStorage();
2223
}
23-
$this->_getHelper()->getSession()->removePersistentCookie();
24+
$this->sessionHelper->getSession()->removePersistentCookie();
2425
return $this;
2526
}
2627

2728
/**
2829
* Unset persistent cookie action
2930
*
30-
* @return void
31+
* @return \Magento\Framework\Controller\Result\Redirect
3132
*/
3233
public function execute()
3334
{
34-
if ($this->_getHelper()->isPersistent()) {
35-
$this->_cleanup();
35+
if ($this->sessionHelper->isPersistent()) {
36+
$this->cleanup();
3637
}
37-
$this->_redirect('customer/account/login');
38-
return;
38+
$resultRedirect = $this->resultRedirectFactory->create();
39+
$resultRedirect->setPath('customer/account/login');
40+
return $resultRedirect;
3941
}
4042
}

0 commit comments

Comments
 (0)