7
7
8
8
namespace Magento \Catalog \Controller \Adminhtml ;
9
9
10
+ use Magento \Framework \App \ObjectManager ;
10
11
use Magento \Store \Model \Store ;
12
+ use Magento \Framework \Controller \ResultFactory ;
11
13
12
14
/**
13
15
* Catalog category controller
16
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14
17
*/
15
18
abstract class Category extends \Magento \Backend \App \Action
16
19
{
@@ -26,20 +29,61 @@ abstract class Category extends \Magento\Backend\App\Action
26
29
*/
27
30
protected $ dateFilter ;
28
31
32
+ /**
33
+ * @var \Magento\Store\Model\StoreManagerInterface
34
+ */
35
+ private $ storeManager ;
36
+
37
+ /**
38
+ * @var \Magento\Framework\Registry
39
+ */
40
+ private $ registry ;
41
+
42
+ /**
43
+ * @var \Magento\Cms\Model\Wysiwyg\Config
44
+ */
45
+ private $ wysiwigConfig ;
46
+
47
+ /**
48
+ * @var \Magento\Backend\Model\Auth\Session
49
+ */
50
+ private $ authSession ;
51
+
29
52
/**
30
53
* @param \Magento\Backend\App\Action\Context $context
31
54
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date|null $dateFilter
55
+ * @param \Magento\Store\Model\StoreManagerInterface $storeManager
56
+ * @param \Magento\Framework\Registry $registry
57
+ * @param \Magento\Cms\Model\Wysiwyg\Config $wysiwigConfig
58
+ * @param \Magento\Backend\Model\Auth\Session $authSession
32
59
*/
33
60
public function __construct (
34
61
\Magento \Backend \App \Action \Context $ context ,
35
- \Magento \Framework \Stdlib \DateTime \Filter \Date $ dateFilter = null
62
+ \Magento \Framework \Stdlib \DateTime \Filter \Date $ dateFilter = null ,
63
+ \Magento \Store \Model \StoreManagerInterface $ storeManager = null ,
64
+ \Magento \Framework \Registry $ registry = null ,
65
+ \Magento \Cms \Model \Wysiwyg \Config $ wysiwigConfig = null ,
66
+ \Magento \Backend \Model \Auth \Session $ authSession = null
36
67
) {
37
68
$ this ->dateFilter = $ dateFilter ;
69
+ $ this ->storeManager = $ storeManager ?: ObjectManager::getInstance ()->get (
70
+ \Magento \Store \Model \StoreManagerInterface::class
71
+ );
72
+ $ this ->registry = $ registry ?: ObjectManager::getInstance ()->get (
73
+ \Magento \Framework \Registry::class
74
+ );
75
+ $ this ->wysiwigConfig = $ wysiwigConfig ?: ObjectManager::getInstance ()->get (
76
+ \Magento \Cms \Model \Wysiwyg \Config::class
77
+ );
78
+ $ this ->authSession = $ authSession ?: ObjectManager::getInstance ()->get (
79
+ \Magento \Backend \Model \Auth \Session::class
80
+ );
38
81
parent ::__construct ($ context );
39
82
}
40
83
41
84
/**
42
- * Initialize requested category and put it into registry.
85
+ * Initialize requested category and put it into registry
86
+ *
43
87
* Root category can be returned, if inappropriate store/category is specified
44
88
*
45
89
* @param bool $getRootInstead
@@ -55,11 +99,7 @@ protected function _initCategory($getRootInstead = false)
55
99
if ($ categoryId ) {
56
100
$ category ->load ($ categoryId );
57
101
if ($ storeId ) {
58
- $ rootId = $ this ->_objectManager ->get (
59
- \Magento \Store \Model \StoreManagerInterface::class
60
- )->getStore (
61
- $ storeId
62
- )->getRootCategoryId ();
102
+ $ rootId = $ this ->storeManager ->getStore ($ storeId )->getRootCategoryId ();
63
103
if (!in_array ($ rootId , $ category ->getPathIds ())) {
64
104
// load root category instead wrong one
65
105
if ($ getRootInstead ) {
@@ -71,10 +111,9 @@ protected function _initCategory($getRootInstead = false)
71
111
}
72
112
}
73
113
74
- $ this ->_objectManager ->get (\Magento \Framework \Registry::class)->register ('category ' , $ category );
75
- $ this ->_objectManager ->get (\Magento \Framework \Registry::class)->register ('current_category ' , $ category );
76
- $ this ->_objectManager ->get (\Magento \Cms \Model \Wysiwyg \Config::class)
77
- ->setStoreId ($ storeId );
114
+ $ this ->registry ->register ('category ' , $ category );
115
+ $ this ->registry ->register ('current_category ' , $ category );
116
+ $ this ->wysiwigConfig ->setStoreId ($ storeId );
78
117
return $ category ;
79
118
}
80
119
@@ -91,9 +130,8 @@ private function resolveCategoryId() : int
91
130
}
92
131
93
132
/**
94
- * Resolve store id
133
+ * Resolve store Id, tries to take store id from store HTTP parameter
95
134
*
96
- * Tries to take store id from store HTTP parameter
97
135
* @see Store
98
136
*
99
137
* @return int
@@ -121,11 +159,7 @@ protected function ajaxRequestResponse($category, $resultPage)
121
159
$ breadcrumbsPath = $ category ->getPath ();
122
160
if (empty ($ breadcrumbsPath )) {
123
161
// but if no category, and it is deleted - prepare breadcrumbs from path, saved in session
124
- $ breadcrumbsPath = $ this ->_objectManager ->get (
125
- \Magento \Backend \Model \Auth \Session::class
126
- )->getDeletedPath (
127
- true
128
- );
162
+ $ breadcrumbsPath = $ this ->authSession ->getDeletedPath (true );
129
163
if (!empty ($ breadcrumbsPath )) {
130
164
$ breadcrumbsPath = explode ('/ ' , $ breadcrumbsPath );
131
165
// no need to get parent breadcrumbs if deleting category level 1
@@ -138,19 +172,21 @@ protected function ajaxRequestResponse($category, $resultPage)
138
172
}
139
173
}
140
174
141
- $ eventResponse = new \Magento \Framework \DataObject ([
142
- 'content ' => $ resultPage ->getLayout ()->getUiComponent ('category_form ' )->getFormHtml ()
143
- . $ resultPage ->getLayout ()->getBlock ('category.tree ' )
144
- ->getBreadcrumbsJavascript ($ breadcrumbsPath , 'editingCategoryBreadcrumbs ' ),
145
- 'messages ' => $ resultPage ->getLayout ()->getMessagesBlock ()->getGroupedHtml (),
146
- 'toolbar ' => $ resultPage ->getLayout ()->getBlock ('page.actions.toolbar ' )->toHtml ()
147
- ]);
175
+ $ eventResponse = new \Magento \Framework \DataObject (
176
+ [
177
+ 'content ' => $ resultPage ->getLayout ()->getUiComponent ('category_form ' )->getFormHtml ()
178
+ . $ resultPage ->getLayout ()->getBlock ('category.tree ' )
179
+ ->getBreadcrumbsJavascript ($ breadcrumbsPath , 'editingCategoryBreadcrumbs ' ),
180
+ 'messages ' => $ resultPage ->getLayout ()->getMessagesBlock ()->getGroupedHtml (),
181
+ 'toolbar ' => $ resultPage ->getLayout ()->getBlock ('page.actions.toolbar ' )->toHtml ()
182
+ ]
183
+ );
148
184
$ this ->_eventManager ->dispatch (
149
185
'category_prepare_ajax_response ' ,
150
186
['response ' => $ eventResponse , 'controller ' => $ this ]
151
187
);
152
188
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
153
- $ resultJson = $ this ->_objectManager -> get (\ Magento \ Framework \ Controller \ Result \Json::class );
189
+ $ resultJson = $ this ->resultFactory -> create (ResultFactory:: TYPE_JSON );
154
190
$ resultJson ->setHeader ('Content-type ' , 'application/json ' , true );
155
191
$ resultJson ->setData ($ eventResponse ->getData ());
156
192
return $ resultJson ;
0 commit comments