5
5
*/
6
6
namespace Magento \Sitemap \Controller \Adminhtml \Sitemap ;
7
7
8
- use Magento \Backend \App \Action ;
8
+ use Magento \Backend \App \Action \Context ;
9
+ use Magento \Framework \App \Action \HttpPostActionInterface ;
9
10
use Magento \Framework \App \Filesystem \DirectoryList ;
10
11
use Magento \Framework \Controller ;
12
+ use Magento \Framework \Validator \StringLength ;
13
+ use Magento \MediaStorage \Model \File \Validator \AvailablePath ;
14
+ use Magento \Sitemap \Model \SitemapFactory ;
11
15
12
- class Save extends \Magento \Sitemap \Controller \Adminhtml \Sitemap
16
+ /**
17
+ * Save sitemap controller.
18
+ */
19
+ class Save extends \Magento \Sitemap \Controller \Adminhtml \Sitemap implements HttpPostActionInterface
13
20
{
21
+ /**
22
+ * Maximum length of sitemap filename
23
+ */
24
+ const MAX_FILENAME_LENGTH = 32 ;
25
+
26
+ /**
27
+ * @var StringLength
28
+ */
29
+ private $ stringValidator ;
30
+
31
+ /**
32
+ * @var AvailablePath
33
+ */
34
+ private $ pathValidator ;
35
+
36
+ /**
37
+ * @var \Magento\Sitemap\Helper\Data
38
+ */
39
+ private $ sitemapHelper ;
40
+
41
+ /**
42
+ * @var \Magento\Framework\Filesystem
43
+ */
44
+ private $ filesystem ;
45
+
46
+ /**
47
+ * @var SitemapFactory
48
+ */
49
+ private $ sitemapFactory ;
50
+
51
+ /**
52
+ * Save constructor.
53
+ * @param Context $context
54
+ * @param StringLength $stringValidator
55
+ * @param AvailablePath $pathValidator
56
+ * @param \Magento\Sitemap\Helper\Data $sitemapHelper
57
+ * @param \Magento\Framework\Filesystem $filesystem
58
+ * @param SitemapFactory $sitemapFactory
59
+ */
60
+ public function __construct (
61
+ Context $ context ,
62
+ StringLength $ stringValidator = null ,
63
+ AvailablePath $ pathValidator = null ,
64
+ \Magento \Sitemap \Helper \Data $ sitemapHelper = null ,
65
+ \Magento \Framework \Filesystem $ filesystem = null ,
66
+ SitemapFactory $ sitemapFactory = null
67
+ ) {
68
+ parent ::__construct ($ context );
69
+ $ this ->stringValidator = $ stringValidator ?: $ this ->_objectManager ->get (StringLength::class);
70
+ $ this ->pathValidator = $ pathValidator ?: $ this ->_objectManager ->get (AvailablePath::class);
71
+ $ this ->sitemapHelper = $ sitemapHelper ?: $ this ->_objectManager ->get (\Magento \Sitemap \Helper \Data::class);
72
+ $ this ->filesystem = $ filesystem ?: $ this ->_objectManager ->get (\Magento \Framework \Filesystem::class);
73
+ $ this ->sitemapFactory = $ sitemapFactory ?: $ this ->_objectManager ->get (SitemapFactory::class);
74
+ }
75
+
14
76
/**
15
77
* Validate path for generation
16
78
*
@@ -23,17 +85,25 @@ protected function validatePath(array $data)
23
85
if (!empty ($ data ['sitemap_filename ' ]) && !empty ($ data ['sitemap_path ' ])) {
24
86
$ data ['sitemap_path ' ] = '/ ' . ltrim ($ data ['sitemap_path ' ], '/ ' );
25
87
$ path = rtrim ($ data ['sitemap_path ' ], '\\/ ' ) . '/ ' . $ data ['sitemap_filename ' ];
26
- /** @var $validator \Magento\MediaStorage\Model\File\Validator\AvailablePath */
27
- $ validator = $ this ->_objectManager ->create (\Magento \MediaStorage \Model \File \Validator \AvailablePath::class);
28
- /** @var $helper \Magento\Sitemap\Helper\Data */
29
- $ helper = $ this ->_objectManager ->get (\Magento \Sitemap \Helper \Data::class);
30
- $ validator ->setPaths ($ helper ->getValidPaths ());
31
- if (!$ validator ->isValid ($ path )) {
32
- foreach ($ validator ->getMessages () as $ message ) {
88
+ $ this ->pathValidator ->setPaths ($ this ->sitemapHelper ->getValidPaths ());
89
+ if (!$ this ->pathValidator ->isValid ($ path )) {
90
+ foreach ($ this ->pathValidator ->getMessages () as $ message ) {
91
+ $ this ->messageManager ->addErrorMessage ($ message );
92
+ }
93
+ // save data in session
94
+ $ this ->_session ->setFormData ($ data );
95
+ // redirect to edit form
96
+ return false ;
97
+ }
98
+
99
+ $ filename = rtrim ($ data ['sitemap_filename ' ]);
100
+ $ this ->stringValidator ->setMax (self ::MAX_FILENAME_LENGTH );
101
+ if (!$ this ->stringValidator ->isValid ($ filename )) {
102
+ foreach ($ this ->stringValidator ->getMessages () as $ message ) {
33
103
$ this ->messageManager ->addErrorMessage ($ message );
34
104
}
35
105
// save data in session
36
- $ this ->_objectManager -> get (\ Magento \ Backend \ Model \Session::class) ->setFormData ($ data );
106
+ $ this ->_session ->setFormData ($ data );
37
107
// redirect to edit form
38
108
return false ;
39
109
}
@@ -49,9 +119,8 @@ protected function validatePath(array $data)
49
119
*/
50
120
protected function clearSiteMap (\Magento \Sitemap \Model \Sitemap $ model )
51
121
{
52
- /** @var \Magento\Framework\Filesystem\Directory\Write $directory */
53
- $ directory = $ this ->_objectManager ->get (\Magento \Framework \Filesystem::class)
54
- ->getDirectoryWrite (DirectoryList::ROOT );
122
+ /** @var \Magento\Framework\Filesystem $directory */
123
+ $ directory = $ this ->filesystem ->getDirectoryWrite (DirectoryList::ROOT );
55
124
56
125
if ($ this ->getRequest ()->getParam ('sitemap_id ' )) {
57
126
$ model ->load ($ this ->getRequest ()->getParam ('sitemap_id ' ));
@@ -74,7 +143,7 @@ protected function saveData($data)
74
143
{
75
144
// init model and set data
76
145
/** @var \Magento\Sitemap\Model\Sitemap $model */
77
- $ model = $ this ->_objectManager ->create (\ Magento \ Sitemap \ Model \Sitemap::class );
146
+ $ model = $ this ->sitemapFactory ->create ();
78
147
$ this ->clearSiteMap ($ model );
79
148
$ model ->setData ($ data );
80
149
@@ -85,13 +154,13 @@ protected function saveData($data)
85
154
// display success message
86
155
$ this ->messageManager ->addSuccessMessage (__ ('You saved the sitemap. ' ));
87
156
// clear previously saved data from session
88
- $ this ->_objectManager -> get (\ Magento \ Backend \ Model \Session::class) ->setFormData (false );
157
+ $ this ->_session ->setFormData (false );
89
158
return $ model ->getId ();
90
159
} catch (\Exception $ e ) {
91
160
// display error message
92
161
$ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
93
162
// save data in session
94
- $ this ->_objectManager -> get (\ Magento \ Backend \ Model \Session::class) ->setFormData ($ data );
163
+ $ this ->_session ->setFormData ($ data );
95
164
}
96
165
return false ;
97
166
}
0 commit comments