4
4
* See COPYING.txt for license details.
5
5
*/
6
6
7
+ declare (strict_types=1 );
8
+
7
9
namespace Magento \ProductAlert \Controller \Unsubscribe ;
8
10
9
- use Magento \ProductAlert \Controller \Unsubscribe as UnsubscribeController ;
10
- use Magento \Framework \App \Action \Context ;
11
- use Magento \Customer \Model \Session as CustomerSession ;
12
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
12
+ use Magento \Customer \Model \Session as CustomerSession ;
13
+ use Magento \Framework \App \Action \Context ;
14
+ use Magento \Framework \App \Action \HttpPostActionInterface ;
15
+ use Magento \Framework \App \ObjectManager ;
16
+ use Magento \Framework \Controller \Result \Redirect ;
13
17
use Magento \Framework \Controller \ResultFactory ;
14
18
use Magento \Framework \Exception \NoSuchEntityException ;
19
+ use Magento \ProductAlert \Controller \Unsubscribe as UnsubscribeController ;
20
+ use Magento \ProductAlert \Model \StockFactory ;
21
+ use Magento \Store \Model \StoreManagerInterface ;
22
+ use Magento \Catalog \Api \Data \ProductInterface ;
15
23
16
- class Stock extends UnsubscribeController
24
+ /**
25
+ * Unsubscribing from 'back in stock alert'.
26
+ */
27
+ class Stock extends UnsubscribeController implements HttpPostActionInterface
17
28
{
18
29
/**
19
- * @var \Magento\Catalog\Api\ ProductRepositoryInterface
30
+ * @var ProductRepositoryInterface
20
31
*/
21
32
protected $ productRepository ;
22
33
23
34
/**
24
- * @param \Magento\Framework\App\Action\Context $context
25
- * @param \Magento\Customer\Model\Session $customerSession
26
- * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
35
+ * @var StoreManagerInterface|null
36
+ */
37
+ private $ storeManager ;
38
+
39
+ /**
40
+ * @var StockFactory|null
41
+ */
42
+ private $ stockFactory ;
43
+
44
+ /**
45
+ * @param Context $context
46
+ * @param CustomerSession $customerSession
47
+ * @param ProductRepositoryInterface $productRepository
48
+ * @param StoreManagerInterface|null $storeManager
49
+ * @param StockFactory|null $stockFactory
27
50
*/
28
51
public function __construct (
29
52
Context $ context ,
30
53
CustomerSession $ customerSession ,
31
- ProductRepositoryInterface $ productRepository
54
+ ProductRepositoryInterface $ productRepository ,
55
+ StoreManagerInterface $ storeManager = null ,
56
+ StockFactory $ stockFactory = null
32
57
) {
33
58
$ this ->productRepository = $ productRepository ;
59
+ $ this ->storeManager = $ storeManager ?? ObjectManager::getInstance ()->get (StoreManagerInterface::class);
60
+ $ this ->stockFactory = $ stockFactory ?? ObjectManager::getInstance ()->get (StockFactory::class);
34
61
parent ::__construct ($ context , $ customerSession );
35
62
}
36
63
37
64
/**
38
- * @return \Magento\Framework\Controller\Result\Redirect
65
+ * Unsubscribing from 'back in stock alert'.
66
+ *
67
+ * @return Redirect
39
68
*/
40
69
public function execute ()
41
70
{
42
71
$ productId = (int )$ this ->getRequest ()->getParam ('product ' );
43
- /** @var \Magento\Framework\Controller\Result\ Redirect $resultRedirect */
72
+ /** @var Redirect $resultRedirect */
44
73
$ resultRedirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
45
74
if (!$ productId ) {
46
75
$ resultRedirect ->setPath ('/ ' );
47
76
return $ resultRedirect ;
48
77
}
49
78
50
79
try {
51
- $ product = $ this ->productRepository ->getById ($ productId );
52
- if (!$ product ->isVisibleInCatalog ()) {
53
- throw new NoSuchEntityException ();
54
- }
55
-
56
- $ model = $ this ->_objectManager ->create (\Magento \ProductAlert \Model \Stock::class)
80
+ $ product = $ this ->retrieveProduct ($ productId );
81
+ $ model = $ this ->stockFactory ->create ()
57
82
->setCustomerId ($ this ->customerSession ->getCustomerId ())
58
83
->setProductId ($ product ->getId ())
59
84
->setWebsiteId (
60
- $ this ->_objectManager -> get (\ Magento \ Store \ Model \StoreManagerInterface::class)
85
+ $ this ->storeManager
61
86
->getStore ()
62
87
->getWebsiteId ()
88
+ )->setStoreId (
89
+ $ this ->storeManager
90
+ ->getStore ()
91
+ ->getId ()
63
92
)
64
93
->loadByParam ();
65
94
if ($ model ->getId ()) {
@@ -79,4 +108,21 @@ public function execute()
79
108
$ resultRedirect ->setUrl ($ product ->getProductUrl ());
80
109
return $ resultRedirect ;
81
110
}
111
+
112
+ /**
113
+ * Retrieving product
114
+ *
115
+ * @param int $productId
116
+ *
117
+ * @return ProductInterface
118
+ * @throws NoSuchEntityException
119
+ */
120
+ private function retrieveProduct (int $ productId ): ProductInterface
121
+ {
122
+ $ product = $ this ->productRepository ->getById ($ productId );
123
+ if (!$ product ->isVisibleInCatalog ()) {
124
+ throw new NoSuchEntityException ();
125
+ }
126
+ return $ product ;
127
+ }
82
128
}
0 commit comments