5
5
*/
6
6
namespace Magento \Inventory \Controller \Adminhtml \Stock ;
7
7
8
- use Exception ;
9
8
use Magento \Backend \App \Action ;
10
9
use Magento \Backend \App \Action \Context ;
11
- use Magento \Framework \Api \DataObjectHelper ;
12
10
use Magento \Framework \Controller \Result \Redirect ;
13
- use Magento \Framework \EntityManager \EventManager ;
14
- use Magento \Framework \Exception \CouldNotSaveException ;
15
- use Magento \Framework \Exception \InputException ;
16
- use Magento \Framework \Exception \NoSuchEntityException ;
11
+ use Magento \Framework \Exception \LocalizedException ;
17
12
use Magento \Framework \Validation \ValidationException ;
18
13
use Magento \InventoryApi \Api \Data \StockInterface ;
19
- use Magento \InventoryApi \Api \Data \StockInterfaceFactory ;
20
- use Magento \InventoryApi \Api \StockRepositoryInterface ;
21
14
22
15
/**
23
16
* Save Controller
@@ -30,52 +23,20 @@ class Save extends Action
30
23
const ADMIN_RESOURCE = 'Magento_Inventory::stock ' ;
31
24
32
25
/**
33
- * @var StockInterfaceFactory
26
+ * @var StockSaveProcessor
34
27
*/
35
- private $ stockFactory ;
36
-
37
- /**
38
- * @var StockRepositoryInterface
39
- */
40
- private $ stockRepository ;
41
-
42
- /**
43
- * @var DataObjectHelper
44
- */
45
- private $ dataObjectHelper ;
46
-
47
- /**
48
- * @var StockSourceLinkProcessor
49
- */
50
- private $ stockSourceLinkProcessor ;
51
-
52
- /**
53
- * @var EventManager
54
- */
55
- private $ eventManager ;
28
+ private $ stockSaveProcessor ;
56
29
57
30
/**
58
31
* @param Context $context
59
- * @param StockInterfaceFactory $stockFactory
60
- * @param StockRepositoryInterface $stockRepository
61
- * @param DataObjectHelper $sourceHydrator
62
- * @param StockSourceLinkProcessor $stockSourceLinkProcessor
63
- * @param EventManager $eventManager
32
+ * @param StockSaveProcessor $stockSaveProcessor
64
33
*/
65
34
public function __construct (
66
35
Context $ context ,
67
- StockInterfaceFactory $ stockFactory ,
68
- StockRepositoryInterface $ stockRepository ,
69
- DataObjectHelper $ sourceHydrator ,
70
- StockSourceLinkProcessor $ stockSourceLinkProcessor ,
71
- EventManager $ eventManager
36
+ StockSaveProcessor $ stockSaveProcessor
72
37
) {
73
38
parent ::__construct ($ context );
74
- $ this ->stockFactory = $ stockFactory ;
75
- $ this ->stockRepository = $ stockRepository ;
76
- $ this ->dataObjectHelper = $ sourceHydrator ;
77
- $ this ->stockSourceLinkProcessor = $ stockSourceLinkProcessor ;
78
- $ this ->eventManager = $ eventManager ;
39
+ $ this ->stockSaveProcessor = $ stockSaveProcessor ;
79
40
}
80
41
81
42
/**
@@ -85,71 +46,30 @@ public function execute()
85
46
{
86
47
$ resultRedirect = $ this ->resultRedirectFactory ->create ();
87
48
$ requestData = $ this ->getRequest ()->getParams ();
88
- if ($ this ->getRequest ()->isPost () && !empty ($ requestData ['general ' ])) {
89
- try {
90
- $ stockId = isset ($ requestData ['general ' ][StockInterface::STOCK_ID ])
91
- ? (int )$ requestData ['general ' ][StockInterface::STOCK_ID ]
92
- : null ;
93
- $ stockId = $ this ->processSave ($ requestData , $ stockId );
94
-
95
- $ this ->messageManager ->addSuccessMessage (__ ('The Stock has been saved. ' ));
96
- $ this ->processRedirectAfterSuccessSave ($ resultRedirect , $ stockId );
97
- } catch (NoSuchEntityException $ e ) {
98
- $ this ->messageManager ->addErrorMessage (__ ('The Stock does not exist. ' ));
99
- $ this ->processRedirectAfterFailureSave ($ resultRedirect );
100
- } catch (ValidationException $ e ) {
101
- foreach ($ e ->getErrors () as $ localizedError ) {
102
- $ this ->messageManager ->addErrorMessage ($ localizedError ->getMessage ());
103
- }
104
- $ this ->processRedirectAfterFailureSave ($ resultRedirect , $ stockId );
105
- } catch (CouldNotSaveException $ e ) {
106
- $ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
107
- $ this ->processRedirectAfterFailureSave ($ resultRedirect , $ stockId );
108
- } catch (InputException $ e ) {
109
- $ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
110
- $ this ->processRedirectAfterFailureSave ($ resultRedirect , $ stockId );
111
- } catch (Exception $ e ) {
112
- $ this ->messageManager ->addErrorMessage (__ ('Could not save stock. ' ));
113
- $ this ->processRedirectAfterFailureSave ($ resultRedirect , $ stockId ?? null );
114
- }
115
- } else {
49
+ if (!$ this ->getRequest ()->isPost () || empty ($ requestData ['general ' ])) {
116
50
$ this ->messageManager ->addErrorMessage (__ ('Wrong request. ' ));
117
51
$ this ->processRedirectAfterFailureSave ($ resultRedirect );
118
- }
119
- return $ resultRedirect ;
120
- }
121
52
122
- /**
123
- * Saves inventory stock and returns stock id
124
- *
125
- * @param int $stockId
126
- * @param array $requestData
127
- * @return int
128
- */
129
- private function processSave (array $ requestData , int $ stockId = null ): int
130
- {
131
- if (null === $ stockId ) {
132
- /** @var StockInterface $stock */
133
- $ stock = $ this ->stockFactory ->create ();
134
- } else {
135
- $ stock = $ this ->stockRepository ->get ($ stockId );
53
+ return $ resultRedirect ;
54
+ }
55
+ try {
56
+ $ stockId = isset ($ requestData ['general ' ][StockInterface::STOCK_ID ])
57
+ ? (int )$ requestData ['general ' ][StockInterface::STOCK_ID ]
58
+ : null ;
59
+ $ stockId = $ this ->stockSaveProcessor ->process ($ stockId , $ requestData );
60
+ $ this ->messageManager ->addSuccessMessage (__ ('The Stock has been saved. ' ));
61
+ $ this ->processRedirectAfterSuccessSave ($ resultRedirect , $ stockId );
62
+ } catch (ValidationException $ e ) {
63
+ foreach ($ e ->getErrors () as $ localizedError ) {
64
+ $ this ->messageManager ->addErrorMessage ($ localizedError ->getMessage ());
65
+ }
66
+ $ this ->processRedirectAfterFailureSave ($ resultRedirect , $ stockId );
67
+ } catch (LocalizedException $ e ) {
68
+ $ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
69
+ $ this ->processRedirectAfterFailureSave ($ resultRedirect , $ stockId ?? null );
136
70
}
137
- $ this ->dataObjectHelper ->populateWithArray ($ stock , $ requestData ['general ' ], StockInterface::class);
138
- $ this ->eventManager ->dispatch (
139
- 'save_stock_controller_populate_stock_with_data ' ,
140
- [
141
- 'request_data ' => $ requestData ,
142
- 'stock ' => $ stock ,
143
- ]
144
- );
145
- $ stockId = $ this ->stockRepository ->save ($ stock );
146
71
147
- $ assignedSources =
148
- isset ($ requestData ['sources ' ]['assigned_sources ' ]) && is_array ($ requestData ['sources ' ]['assigned_sources ' ])
149
- ? $ requestData ['sources ' ]['assigned_sources ' ]
150
- : [];
151
- $ this ->stockSourceLinkProcessor ->process ($ stockId , $ assignedSources );
152
- return $ stockId ;
72
+ return $ resultRedirect ;
153
73
}
154
74
155
75
/**
0 commit comments