Skip to content

Commit 57bbbe7

Browse files
authored
Merge pull request #28 from yuriyhamulevych/master
2032-objectManager move classes to constructor [Implemented]
2 parents e0bffd7 + 3bf4b8d commit 57bbbe7

File tree

3 files changed

+81
-19
lines changed

3 files changed

+81
-19
lines changed

Controller/Adminhtml/Login/Index.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@
1313
*/
1414
class Index extends \Magento\Backend\App\Action
1515
{
16+
/**
17+
* @var \Magefan\LoginAsCustomer\Model\Login
18+
*/
19+
protected $login = null;
20+
21+
/**
22+
* Index constructor.
23+
* @param \Magento\Backend\App\Action\Context $context
24+
* @param \Magefan\LoginAsCustomer\Model\Login|null $login
25+
*/
26+
public function __construct(
27+
\Magento\Backend\App\Action\Context $context,
28+
\Magefan\LoginAsCustomer\Model\Login $login = null
29+
) {
30+
parent::__construct($context);
31+
$objectManager = $this->_objectManager;
32+
$this->login = $login ?: $objectManager->get(\Magefan\LoginAsCustomer\Model\Login::class);
33+
}
1634
/**
1735
* Login as customer log
1836
*
@@ -25,9 +43,7 @@ public function execute()
2543
return;
2644
}
2745

28-
$this->_objectManager
29-
->create(\Magefan\LoginAsCustomer\Model\Login::class)
30-
->deleteNotUsed();
46+
$this->login->deleteNotUsed();
3147

3248
$this->_view->loadLayout();
3349
$this->_setActiveMenu('Magefan_LoginAsCustomer::login_log');

Controller/Adminhtml/Login/Login.php

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,46 @@
99
namespace Magefan\LoginAsCustomer\Controller\Adminhtml\Login;
1010

1111
/**
12-
* LoginAsCustomer login action
12+
* Class Login
13+
* @package Magefan\LoginAsCustomer\Controller\Adminhtml\Login
1314
*/
1415
class Login extends \Magento\Backend\App\Action
1516
{
17+
/**
18+
* @var \Magefan\LoginAsCustomer\Model\Login
19+
*/
20+
protected $login;
21+
/**
22+
* @var \Magento\Backend\Model\Auth\Session
23+
*/
24+
protected $session = null;
25+
/**
26+
* @var \Magento\Store\Model\StoreManagerInterface
27+
*/
28+
protected $storeManager = null;
29+
/**
30+
* @var \Magento\Framework\Url
31+
*/
32+
protected $url = null;
33+
/**
34+
* Login constructor.
35+
* @param \Magento\Backend\App\Action\Context $context
36+
* @param \Magefan\LoginAsCustomer\Model\Login $login
37+
*/
38+
public function __construct(
39+
\Magento\Backend\App\Action\Context $context,
40+
\Magefan\LoginAsCustomer\Model\Login $login = null,
41+
\Magento\Backend\Model\Auth\Session $session = null,
42+
\Magento\Store\Model\StoreManagerInterface $storeManager = null,
43+
\Magento\Framework\Url $url = null
44+
) {
45+
parent::__construct($context);
46+
$objectManager = $this->_objectManager;
47+
$this->login = $login ?: $objectManager->get(\Magefan\LoginAsCustomer\Model\Login::class);
48+
$this->session = $session ?: $objectManager->get(\Magento\Backend\Model\Auth\Session::class);
49+
$this->storeManager = $storeManager ?: $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
50+
$this->url = $url ?: $objectManager->get(\Magento\Framework\Url::class);
51+
}
1652
/**
1753
* Login as customer action
1854
*
@@ -22,9 +58,7 @@ public function execute()
2258
{
2359
$customerId = (int) $this->getRequest()->getParam('customer_id');
2460

25-
$login = $this->_objectManager
26-
->create(\Magefan\LoginAsCustomer\Model\Login::class)
27-
->setCustomerId($customerId);
61+
$login = $this->login->setCustomerId($customerId);
2862

2963
$login->deleteNotUsed();
3064

@@ -36,22 +70,18 @@ public function execute()
3670
return;
3771
}
3872

39-
$user = $this->_objectManager->get(\Magento\Backend\Model\Auth\Session::class)->getUser();
73+
$user = $this->session->getUser();
4074
$login->generate($user->getId());
4175
$customerStoreId = $this->getCustomerStoreId($customer);
4276

43-
$storeManager = $this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
44-
4577
if ($customerStoreId) {
46-
$store = $storeManager->getStore($customerStoreId);
78+
$store = $this->storeManager->getStore($customerStoreId);
4779
} else {
48-
$store = $storeManager->getDefaultStoreView();
80+
$store = $this->storeManager->getDefaultStoreView();
4981
}
5082

51-
$url = $this->_objectManager->get(\Magento\Framework\Url::class)
52-
->setScope($store);
53-
54-
$redirectUrl = $url->getUrl('loginascustomer/login/index', ['secret' => $login->getSecret(), '_nosid' => true]);
83+
$redirectUrl = $this->url->setScope($store)
84+
->getUrl('loginascustomer/login/index', ['secret' => $login->getSecret(), '_nosid' => true]);
5585

5686
$this->getResponse()->setRedirect($redirectUrl);
5787
}

Controller/Login/Index.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@
1212
*/
1313
class Index extends \Magento\Framework\App\Action\Action
1414
{
15+
/**
16+
* @var \Magefan\LoginAsCustomer\Model\Login
17+
*/
18+
protected $login = null;
19+
20+
/**
21+
* Index constructor.
22+
* @param \Magento\Backend\App\Action\Context $context
23+
* @param \Magefan\LoginAsCustomer\Model\Login|null $login
24+
*/
25+
public function __construct(
26+
\Magento\Backend\App\Action\Context $context,
27+
\Magefan\LoginAsCustomer\Model\Login $login = null
28+
) {
29+
parent::__construct($context);
30+
$objectManager = $this->_objectManager;
31+
$this->login = $login ?: $objectManager->get(\Magefan\LoginAsCustomer\Model\Login::class);
32+
}
1533
/**
1634
* Login as customer action
1735
*
@@ -50,9 +68,7 @@ protected function _initLogin()
5068
return false;
5169
}
5270

53-
$login = $this->_objectManager
54-
->create(\Magefan\LoginAsCustomer\Model\Login::class)
55-
->loadNotUsed($secret);
71+
$login = $this->login->loadNotUsed($secret);
5672

5773
if ($login->getId()) {
5874
return $login;

0 commit comments

Comments
 (0)