Skip to content

Commit 9f3855e

Browse files
author
Yuri Kovsher
committed
MAGETWO-36728: Refactor controller actions in the ProductAlert module
1 parent cd7d717 commit 9f3855e

File tree

8 files changed

+180
-142
lines changed

8 files changed

+180
-142
lines changed

app/code/Magento/ProductAlert/Controller/Add.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,42 @@
55
*/
66
namespace Magento\ProductAlert\Controller;
77

8+
use Magento\Framework\App\Action\Action;
89
use Magento\Framework\App\Action\Context;
10+
use Magento\Customer\Model\Session as CustomerSession;
911
use Magento\Framework\App\RequestInterface;
1012

11-
class Add extends \Magento\Framework\App\Action\Action
13+
class Add extends Action
1214
{
1315
/**
1416
* @var \Magento\Customer\Model\Session
1517
*/
16-
protected $_customerSession;
18+
protected $customerSession;
1719

1820
/**
19-
* @param Context $context
21+
* @param \Magento\Framework\App\Action\Context $context
2022
* @param \Magento\Customer\Model\Session $customerSession
2123
*/
2224
public function __construct(
2325
Context $context,
24-
\Magento\Customer\Model\Session $customerSession
26+
CustomerSession $customerSession
2527
) {
26-
$this->_customerSession = $customerSession;
28+
$this->customerSession = $customerSession;
2729
parent::__construct($context);
2830
}
2931

3032
/**
3133
* Check customer authentication for some actions
3234
*
33-
* @param RequestInterface $request
35+
* @param \Magento\Framework\App\RequestInterface $request
3436
* @return \Magento\Framework\App\ResponseInterface
3537
*/
3638
public function dispatch(RequestInterface $request)
3739
{
38-
if (!$this->_customerSession->authenticate($this)) {
40+
if (!$this->customerSession->authenticate($this)) {
3941
$this->_actionFlag->set('', 'no-dispatch', true);
40-
if (!$this->_customerSession->getBeforeUrl()) {
41-
$this->_customerSession->setBeforeUrl($this->_redirect->getRefererUrl());
42+
if (!$this->customerSession->getBeforeUrl()) {
43+
$this->customerSession->setBeforeUrl($this->_redirect->getRefererUrl());
4244
}
4345
}
4446
return parent::dispatch($request);
Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,47 @@
11
<?php
22
/**
3-
*
43
* Copyright © 2015 Magento. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\ProductAlert\Controller\Add;
87

8+
use Magento\ProductAlert\Controller\Add as AddController;
99
use Magento\Framework\App\Action\Context;
10+
use Magento\Customer\Model\Session as CustomerSession;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
use Magento\Catalog\Api\ProductRepositoryInterface;
13+
use Magento\Framework\UrlInterface;
14+
use Magento\Framework\App\Action\Action;
15+
use Magento\Framework\Controller\ResultFactory;
1016
use Magento\Framework\Exception\NoSuchEntityException;
1117

12-
class Price extends \Magento\ProductAlert\Controller\Add
18+
class Price extends AddController
1319
{
1420
/**
1521
* @var \Magento\Store\Model\StoreManagerInterface
1622
*/
17-
protected $_storeManager;
23+
protected $storeManager;
1824

19-
/** @var \Magento\Catalog\Api\ProductRepositoryInterface */
25+
/**
26+
* @var \Magento\Catalog\Api\ProductRepositoryInterface
27+
*/
2028
protected $productRepository;
2129

2230
/**
23-
* @param Context $context
31+
* @param \Magento\Framework\App\Action\Context $context
2432
* @param \Magento\Customer\Model\Session $customerSession
2533
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
2634
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
2735
*/
2836
public function __construct(
2937
Context $context,
30-
\Magento\Customer\Model\Session $customerSession,
31-
\Magento\Store\Model\StoreManagerInterface $storeManager,
32-
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
38+
CustomerSession $customerSession,
39+
StoreManagerInterface $storeManager,
40+
ProductRepositoryInterface $productRepository
3341
) {
34-
parent::__construct($context, $customerSession);
35-
$this->_storeManager = $storeManager;
42+
$this->storeManager = $storeManager;
3643
$this->productRepository = $productRepository;
44+
parent::__construct($context, $customerSession);
3745
}
3846

3947
/**
@@ -42,61 +50,57 @@ public function __construct(
4250
* @param string $url
4351
* @return bool
4452
*/
45-
protected function _isInternal($url)
53+
protected function isInternal($url)
4654
{
4755
if (strpos($url, 'http') === false) {
4856
return false;
4957
}
50-
$currentStore = $this->_storeManager->getStore();
51-
return strpos(
52-
$url,
53-
$currentStore->getBaseUrl()
54-
) === 0 || strpos(
55-
$url,
56-
$currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK, true)
57-
) === 0;
58+
$currentStore = $this->storeManager->getStore();
59+
return strpos($url, $currentStore->getBaseUrl()) === 0
60+
|| strpos($url, $currentStore->getBaseUrl(UrlInterface::URL_TYPE_LINK, true)) === 0;
5861
}
5962

6063
/**
61-
* @return void
64+
* @return \Magento\Framework\Controller\Result\Redirect
6265
*/
6366
public function execute()
6467
{
65-
$backUrl = $this->getRequest()->getParam(\Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED);
68+
$backUrl = $this->getRequest()->getParam(Action::PARAM_NAME_URL_ENCODED);
6669
$productId = (int)$this->getRequest()->getParam('product_id');
70+
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
71+
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
6772
if (!$backUrl || !$productId) {
68-
$this->_redirect('/');
69-
return;
73+
$resultRedirect->setPath('/');
74+
return $resultRedirect;
7075
}
7176

7277
try {
78+
/* @var $product \Magento\Catalog\Model\Product */
7379
$product = $this->productRepository->getById($productId);
74-
75-
$model = $this->_objectManager->create(
76-
'Magento\ProductAlert\Model\Price'
77-
)->setCustomerId(
78-
$this->_customerSession->getCustomerId()
79-
)->setProductId(
80-
$product->getId()
81-
)->setPrice(
82-
$product->getFinalPrice()
83-
)->setWebsiteId(
84-
$this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getWebsiteId()
85-
);
80+
/** @var \Magento\ProductAlert\Model\Price $model */
81+
$model = $this->_objectManager->create('Magento\ProductAlert\Model\Price')
82+
->setCustomerId($this->customerSession->getCustomerId())
83+
->setProductId($product->getId())
84+
->setPrice($product->getFinalPrice())
85+
->setWebsiteId(
86+
$this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')
87+
->getStore()
88+
->getWebsiteId()
89+
);
8690
$model->save();
8791
$this->messageManager->addSuccess(__('You saved the alert subscription.'));
8892
} catch (NoSuchEntityException $noEntityException) {
89-
/* @var $product \Magento\Catalog\Model\Product */
9093
$this->messageManager->addError(__('There are not enough parameters.'));
91-
if ($this->_isInternal($backUrl)) {
92-
$this->getResponse()->setRedirect($backUrl);
94+
if ($this->isInternal($backUrl)) {
95+
$resultRedirect->setUrl($backUrl);
9396
} else {
94-
$this->_redirect('/');
97+
$resultRedirect->setPath('/');
9598
}
96-
return;
99+
return $resultRedirect;
97100
} catch (\Exception $e) {
98101
$this->messageManager->addException($e, __('Unable to update the alert subscription.'));
99102
}
100-
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
103+
$resultRedirect->setUrl($this->_redirect->getRedirectUrl());
104+
return $resultRedirect;
101105
}
102106
}
Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
<?php
22
/**
3-
*
43
* Copyright © 2015 Magento. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\ProductAlert\Controller\Add;
87

8+
use Magento\ProductAlert\Controller\Add as AddController;
9+
use Magento\Framework\App\Action\Context;
10+
use Magento\Customer\Model\Session as CustomerSession;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Framework\App\Action\Action;
13+
use Magento\Framework\Controller\ResultFactory;
914
use Magento\Framework\Exception\NoSuchEntityException;
1015

11-
class Stock extends \Magento\ProductAlert\Controller\Add
16+
class Stock extends AddController
1217
{
13-
/** @var \Magento\Catalog\Api\ProductRepositoryInterface */
18+
/**
19+
* @var \Magento\Catalog\Api\ProductRepositoryInterface
20+
*/
1421
protected $productRepository;
1522

1623
/**
@@ -19,48 +26,50 @@ class Stock extends \Magento\ProductAlert\Controller\Add
1926
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
2027
*/
2128
public function __construct(
22-
\Magento\Framework\App\Action\Context $context,
23-
\Magento\Customer\Model\Session $customerSession,
24-
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
29+
Context $context,
30+
CustomerSession $customerSession,
31+
ProductRepositoryInterface $productRepository
2532
) {
26-
parent::__construct($context, $customerSession);
2733
$this->productRepository = $productRepository;
34+
parent::__construct($context, $customerSession);
2835
}
2936

3037
/**
31-
* @return void
38+
* @return \Magento\Framework\Controller\Result\Redirect
3239
*/
3340
public function execute()
3441
{
35-
$backUrl = $this->getRequest()->getParam(\Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED);
42+
$backUrl = $this->getRequest()->getParam(Action::PARAM_NAME_URL_ENCODED);
3643
$productId = (int)$this->getRequest()->getParam('product_id');
44+
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
45+
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
3746
if (!$backUrl || !$productId) {
38-
$this->_redirect('/');
39-
return;
47+
$resultRedirect->setPath('/');
48+
return $resultRedirect;
4049
}
4150

4251
try {
52+
/* @var $product \Magento\Catalog\Model\Product */
4353
$product = $this->productRepository->getById($productId);
44-
45-
$model = $this->_objectManager->create(
46-
'Magento\ProductAlert\Model\Stock'
47-
)->setCustomerId(
48-
$this->_customerSession->getCustomerId()
49-
)->setProductId(
50-
$product->getId()
51-
)->setWebsiteId(
52-
$this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getWebsiteId()
53-
);
54+
/** @var \Magento\ProductAlert\Model\Stock $model */
55+
$model = $this->_objectManager->create('Magento\ProductAlert\Model\Stock')
56+
->setCustomerId($this->customerSession->getCustomerId())
57+
->setProductId($product->getId())
58+
->setWebsiteId(
59+
$this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')
60+
->getStore()
61+
->getWebsiteId()
62+
);
5463
$model->save();
5564
$this->messageManager->addSuccess(__('Alert subscription has been saved.'));
5665
} catch (NoSuchEntityException $noEntityException) {
57-
/* @var $product \Magento\Catalog\Model\Product */
5866
$this->messageManager->addError(__('There are not enough parameters.'));
59-
$this->getResponse()->setRedirect($backUrl);
60-
return;
67+
$resultRedirect->setUrl($backUrl);
68+
return $resultRedirect;
6169
} catch (\Exception $e) {
6270
$this->messageManager->addException($e, __('Unable to update the alert subscription.'));
6371
}
64-
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
72+
$resultRedirect->setUrl($this->_redirect->getRedirectUrl());
73+
return $resultRedirect;
6574
}
6675
}

app/code/Magento/ProductAlert/Controller/Add/TestObserver.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
<?php
22
/**
3-
*
43
* Copyright © 2015 Magento. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\ProductAlert\Controller\Add;
87

9-
class TestObserver extends \Magento\ProductAlert\Controller\Add
8+
use Magento\ProductAlert\Controller\Add as AddController;
9+
use Magento\Framework\Object;
10+
11+
class TestObserver extends AddController
1012
{
1113
/**
1214
* @return void
1315
*/
1416
public function execute()
1517
{
16-
$object = new \Magento\Framework\Object();
18+
$object = new Object();
19+
/** @var \Magento\ProductAlert\Model\Observer $observer */
1720
$observer = $this->_objectManager->get('Magento\ProductAlert\Model\Observer');
1821
$observer->process($object);
1922
}

0 commit comments

Comments
 (0)