11
11
12
12
class Save extends \Magento \Sitemap \Controller \Adminhtml \Sitemap
13
13
{
14
+ /**
15
+ * Maximum length of sitemap filename
16
+ */
17
+ const MAX_FILENAME_LENGTH = 32 ;
18
+
19
+ /**
20
+ * @var \Magento\Framework\Validator\StringLength
21
+ */
22
+ private $ stringValidator ;
23
+
24
+ /**
25
+ * @var \Magento\MediaStorage\Model\File\Validator\AvailablePath
26
+ */
27
+ private $ pathValidator ;
28
+
29
+ /**
30
+ * @var \Magento\Sitemap\Helper\Data
31
+ */
32
+ private $ sitemapHelper ;
33
+
34
+ /**
35
+ * @var \Magento\Framework\Filesystem
36
+ */
37
+ private $ filesystem ;
38
+
39
+ /**
40
+ * @var \Magento\Sitemap\Model\SitemapFactory
41
+ */
42
+ private $ sitemapFactory ;
43
+
44
+ /**
45
+ * Save constructor.
46
+ * @param Action\Context $context
47
+ * @param \Magento\Framework\Validator\StringLength $stringValidator
48
+ * @param \Magento\MediaStorage\Model\File\Validator\AvailablePath $pathValidator
49
+ * @param \Magento\Sitemap\Helper\Data $sitemapHelper
50
+ * @param \Magento\Framework\Filesystem $filesystem
51
+ * @param \Magento\Sitemap\Model\SitemapFactory $sitemapFactory
52
+ */
53
+ public function __construct (
54
+ \Magento \Backend \App \Action \Context $ context ,
55
+ \Magento \Framework \Validator \StringLength $ stringValidator = null ,
56
+ \Magento \MediaStorage \Model \File \Validator \AvailablePath $ pathValidator = null ,
57
+ \Magento \Sitemap \Helper \Data $ sitemapHelper = null ,
58
+ \Magento \Framework \Filesystem $ filesystem = null ,
59
+ \Magento \Sitemap \Model \SitemapFactory $ sitemapFactory = null
60
+ ) {
61
+ parent ::__construct ($ context );
62
+ $ this ->stringValidator = $ stringValidator ?: $ this ->_objectManager ->get (\Magento \Framework \Validator \StringLength::class);
63
+ $ this ->pathValidator = $ pathValidator ?: $ this ->_objectManager ->get (\Magento \MediaStorage \Model \File \Validator \AvailablePath::class);
64
+ $ this ->sitemapHelper = $ sitemapHelper ?: $ this ->_objectManager ->get (\Magento \Sitemap \Helper \Data::class);
65
+ $ this ->filesystem = $ filesystem ?: $ this ->_objectManager ->get (\Magento \Framework \Filesystem::class);
66
+ $ this ->sitemapFactory = $ sitemapFactory ?: $ this ->_objectManager ->get (\Magento \Sitemap \Model \SitemapFactory::class);
67
+ }
68
+
14
69
/**
15
70
* Validate path for generation
16
71
*
@@ -23,17 +78,25 @@ protected function validatePath(array $data)
23
78
if (!empty ($ data ['sitemap_filename ' ]) && !empty ($ data ['sitemap_path ' ])) {
24
79
$ data ['sitemap_path ' ] = '/ ' . ltrim ($ data ['sitemap_path ' ], '/ ' );
25
80
$ 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 ) {
81
+ $ this ->pathValidator ->setPaths ($ this ->sitemapHelper ->getValidPaths ());
82
+ if (!$ this ->pathValidator ->isValid ($ path )) {
83
+ foreach ($ this ->pathValidator ->getMessages () as $ message ) {
84
+ $ this ->messageManager ->addErrorMessage ($ message );
85
+ }
86
+ // save data in session
87
+ $ this ->_session ->setFormData ($ data );
88
+ // redirect to edit form
89
+ return false ;
90
+ }
91
+
92
+ $ filename = rtrim ($ data ['sitemap_filename ' ]);
93
+ $ this ->stringValidator ->setMax (self ::MAX_FILENAME_LENGTH );
94
+ if (!$ this ->stringValidator ->isValid ($ filename )) {
95
+ foreach ($ this ->stringValidator ->getMessages () as $ message ) {
33
96
$ this ->messageManager ->addErrorMessage ($ message );
34
97
}
35
98
// save data in session
36
- $ this ->_objectManager -> get (\ Magento \ Backend \ Model \Session::class) ->setFormData ($ data );
99
+ $ this ->_session ->setFormData ($ data );
37
100
// redirect to edit form
38
101
return false ;
39
102
}
@@ -49,9 +112,8 @@ protected function validatePath(array $data)
49
112
*/
50
113
protected function clearSiteMap (\Magento \Sitemap \Model \Sitemap $ model )
51
114
{
52
- /** @var \Magento\Framework\Filesystem\Directory\Write $directory */
53
- $ directory = $ this ->_objectManager ->get (\Magento \Framework \Filesystem::class)
54
- ->getDirectoryWrite (DirectoryList::ROOT );
115
+ /** @var \Magento\Framework\Filesystem $directory */
116
+ $ directory = $ this ->filesystem ->getDirectoryWrite (DirectoryList::ROOT );
55
117
56
118
if ($ this ->getRequest ()->getParam ('sitemap_id ' )) {
57
119
$ model ->load ($ this ->getRequest ()->getParam ('sitemap_id ' ));
@@ -74,7 +136,7 @@ protected function saveData($data)
74
136
{
75
137
// init model and set data
76
138
/** @var \Magento\Sitemap\Model\Sitemap $model */
77
- $ model = $ this ->_objectManager ->create (\ Magento \ Sitemap \ Model \Sitemap::class );
139
+ $ model = $ this ->sitemapFactory ->create ();
78
140
$ this ->clearSiteMap ($ model );
79
141
$ model ->setData ($ data );
80
142
@@ -85,13 +147,13 @@ protected function saveData($data)
85
147
// display success message
86
148
$ this ->messageManager ->addSuccessMessage (__ ('You saved the sitemap. ' ));
87
149
// clear previously saved data from session
88
- $ this ->_objectManager -> get (\ Magento \ Backend \ Model \Session::class) ->setFormData (false );
150
+ $ this ->_session ->setFormData (false );
89
151
return $ model ->getId ();
90
152
} catch (\Exception $ e ) {
91
153
// display error message
92
154
$ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
93
155
// save data in session
94
- $ this ->_objectManager -> get (\ Magento \ Backend \ Model \Session::class) ->setFormData ($ data );
156
+ $ this ->_session ->setFormData ($ data );
95
157
}
96
158
return false ;
97
159
}
0 commit comments