1
1
<?php
2
2
/**
3
- *
4
3
* Copyright © 2015 Magento. All rights reserved.
5
4
* See COPYING.txt for license details.
6
5
*/
7
6
namespace Magento \ProductAlert \Controller \Add ;
8
7
8
+ use Magento \ProductAlert \Controller \Add as AddController ;
9
9
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 ;
10
16
use Magento \Framework \Exception \NoSuchEntityException ;
11
17
12
- class Price extends \ Magento \ ProductAlert \ Controller \Add
18
+ class Price extends AddController
13
19
{
14
20
/**
15
21
* @var \Magento\Store\Model\StoreManagerInterface
16
22
*/
17
- protected $ _storeManager ;
23
+ protected $ storeManager ;
18
24
19
- /** @var \Magento\Catalog\Api\ProductRepositoryInterface */
25
+ /**
26
+ * @var \Magento\Catalog\Api\ProductRepositoryInterface
27
+ */
20
28
protected $ productRepository ;
21
29
22
30
/**
23
- * @param Context $context
31
+ * @param \Magento\Framework\App\Action\ Context $context
24
32
* @param \Magento\Customer\Model\Session $customerSession
25
33
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
26
34
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
27
35
*/
28
36
public function __construct (
29
37
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
33
41
) {
34
- parent ::__construct ($ context , $ customerSession );
35
- $ this ->_storeManager = $ storeManager ;
42
+ $ this ->storeManager = $ storeManager ;
36
43
$ this ->productRepository = $ productRepository ;
44
+ parent ::__construct ($ context , $ customerSession );
37
45
}
38
46
39
47
/**
@@ -42,61 +50,57 @@ public function __construct(
42
50
* @param string $url
43
51
* @return bool
44
52
*/
45
- protected function _isInternal ($ url )
53
+ protected function isInternal ($ url )
46
54
{
47
55
if (strpos ($ url , 'http ' ) === false ) {
48
56
return false ;
49
57
}
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 ;
58
61
}
59
62
60
63
/**
61
- * @return void
64
+ * @return \Magento\Framework\Controller\Result\Redirect
62
65
*/
63
66
public function execute ()
64
67
{
65
- $ backUrl = $ this ->getRequest ()->getParam (\ Magento \ Framework \ App \ Action \ Action::PARAM_NAME_URL_ENCODED );
68
+ $ backUrl = $ this ->getRequest ()->getParam (Action::PARAM_NAME_URL_ENCODED );
66
69
$ productId = (int )$ this ->getRequest ()->getParam ('product_id ' );
70
+ /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
71
+ $ resultRedirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
67
72
if (!$ backUrl || !$ productId ) {
68
- $ this -> _redirect ('/ ' );
69
- return ;
73
+ $ resultRedirect -> setPath ('/ ' );
74
+ return $ resultRedirect ;
70
75
}
71
76
72
77
try {
78
+ /* @var $product \Magento\Catalog\Model\Product */
73
79
$ 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
+ );
86
90
$ model ->save ();
87
91
$ this ->messageManager ->addSuccess (__ ('You saved the alert subscription. ' ));
88
92
} catch (NoSuchEntityException $ noEntityException ) {
89
- /* @var $product \Magento\Catalog\Model\Product */
90
93
$ 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 );
93
96
} else {
94
- $ this -> _redirect ('/ ' );
97
+ $ resultRedirect -> setPath ('/ ' );
95
98
}
96
- return ;
99
+ return $ resultRedirect ;
97
100
} catch (\Exception $ e ) {
98
101
$ this ->messageManager ->addException ($ e , __ ('Unable to update the alert subscription. ' ));
99
102
}
100
- $ this ->getResponse ()->setRedirect ($ this ->_redirect ->getRedirectUrl ());
103
+ $ resultRedirect ->setUrl ($ this ->_redirect ->getRedirectUrl ());
104
+ return $ resultRedirect ;
101
105
}
102
106
}
0 commit comments