9
9
10
10
use Magento \Cron \Model \Config \Backend \Sitemap ;
11
11
use Magento \Cron \Model \Config \Source \Frequency ;
12
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
13
+ use Magento \Framework \App \Config \ValueFactory ;
12
14
use Magento \Framework \Exception \LocalizedException ;
13
15
use Magento \Sitemap \Model \Config \Source \GenerationMethod ;
14
16
@@ -28,15 +30,15 @@ class SitemapPlugin
28
30
private const CRON_MODEL_PATH = 'crontab/default/jobs/sitemap_generate/run/model ' ;
29
31
30
32
/**
31
- * @var \Magento\Framework\App\Config\ ValueFactory
33
+ * @var ValueFactory
32
34
*/
33
35
private $ configValueFactory ;
34
36
35
37
/**
36
- * @param \Magento\Framework\App\Config\ ValueFactory $configValueFactory
38
+ * @param ValueFactory $configValueFactory
37
39
*/
38
40
public function __construct (
39
- \ Magento \ Framework \ App \ Config \ ValueFactory $ configValueFactory
41
+ ValueFactory $ configValueFactory
40
42
) {
41
43
$ this ->configValueFactory = $ configValueFactory ;
42
44
}
@@ -55,18 +57,80 @@ public function afterAfterSave(
55
57
) {
56
58
$ config = $ subject ->getConfig ();
57
59
60
+ $ time = $ this ->getTimeConfiguration ($ subject , $ config );
61
+ $ frequency = $ subject ->getValue ();
62
+ $ generationMethod = $ this ->getGenerationMethod ($ subject , $ config );
63
+
64
+ $ cronExprString = $ this ->buildCronExpression ($ time , $ frequency );
65
+ $ observerModel = $ this ->getObserverModel ($ generationMethod );
66
+
67
+ $ this ->updateCronConfiguration ($ cronExprString , $ observerModel );
68
+
69
+ return $ result ;
70
+ }
71
+
72
+ /**
73
+ * Get time configuration from subject or config
74
+ *
75
+ * @param Sitemap $subject
76
+ * @param ScopeConfigInterface|null $config
77
+ * @return array
78
+ * @throws LocalizedException
79
+ */
80
+ private function getTimeConfiguration (Sitemap $ subject , $ config ): array
81
+ {
58
82
$ time = $ subject ->getData ('groups/generate/fields/time/value ' );
59
83
if (!$ time && $ config ) {
60
84
$ timeConfig = $ config ->getValue (
61
85
'sitemap/generate/time ' ,
62
86
$ subject ->getScope (),
63
87
$ subject ->getScopeId ()
64
88
);
65
- $ time = $ timeConfig ? explode (', ' , $ timeConfig ) : [ ' 0 ' , ' 0 ' , ' 0 ' ] ;
89
+ $ time = $ timeConfig ? explode (', ' , $ timeConfig ) : null ;
66
90
}
67
91
68
- $ frequency = $ subject ->getValue ();
92
+ if (!$ time ) {
93
+ $ recentTimeConfig = $ this ->getRecentlySavedTimeConfiguration ();
94
+ $ time = $ recentTimeConfig ? explode (', ' , $ recentTimeConfig ) : null ;
95
+ }
96
+
97
+ if (!is_array ($ time ) || empty ($ time )) {
98
+ $ time = ['0 ' , '0 ' , '0 ' ];
99
+ }
100
+
101
+ while (count ($ time ) < 3 ) {
102
+ $ time [] = '0 ' ;
103
+ }
104
+
105
+ return $ time ;
106
+ }
107
+
108
+ /**
109
+ * Get recently saved time configuration from config value factory
110
+ *
111
+ * @return string|null
112
+ * @throws LocalizedException
113
+ */
114
+ private function getRecentlySavedTimeConfiguration (): ?string
115
+ {
116
+ $ configValue = $ this ->configValueFactory ->create ()->load (
117
+ 'sitemap/generate/time ' ,
118
+ 'path '
119
+ );
120
+
121
+ return $ configValue ->getId () ? $ configValue ->getValue () : null ;
122
+ }
69
123
124
+ /**
125
+ * Get generation method from various sources
126
+ *
127
+ * @param Sitemap $subject
128
+ * @param ScopeConfigInterface|null $config
129
+ * @return string
130
+ * @throws LocalizedException
131
+ */
132
+ private function getGenerationMethod (Sitemap $ subject , ?ScopeConfigInterface $ config ): string
133
+ {
70
134
$ generationMethod = $ subject ->getData ('groups/generate/fields/generation_method/value ' );
71
135
72
136
if (!$ generationMethod && $ config ) {
@@ -75,18 +139,40 @@ public function afterAfterSave(
75
139
$ subject ->getScope (),
76
140
$ subject ->getScopeId ()
77
141
);
78
- } elseif (!$ generationMethod && !$ config ) {
79
- $ configValue = $ this ->configValueFactory ->create ()->load (
80
- 'sitemap/generate/generation_method ' ,
81
- 'path '
82
- );
83
- if ($ configValue ->getId ()) {
84
- $ generationMethod = $ configValue ->getValue ();
85
- }
86
142
}
87
143
88
- $ generationMethod = $ generationMethod ?: GenerationMethod::STANDARD ;
144
+ if (!$ generationMethod ) {
145
+ $ generationMethod = $ this ->getRecentlySavedGenerationMethod ();
146
+ }
147
+
148
+ return $ generationMethod ?: GenerationMethod::STANDARD ;
149
+ }
150
+
151
+ /**
152
+ * Get recently saved generation method from config value factory
153
+ *
154
+ * @return string|null
155
+ * @throws LocalizedException
156
+ */
157
+ private function getRecentlySavedGenerationMethod (): ?string
158
+ {
159
+ $ configValue = $ this ->configValueFactory ->create ()->load (
160
+ 'sitemap/generate/generation_method ' ,
161
+ 'path '
162
+ );
163
+
164
+ return $ configValue ->getId () ? $ configValue ->getValue () : null ;
165
+ }
89
166
167
+ /**
168
+ * Build cron expression from time and frequency
169
+ *
170
+ * @param array $time
171
+ * @param string $frequency
172
+ * @return string
173
+ */
174
+ private function buildCronExpression (array $ time , string $ frequency ): string
175
+ {
90
176
$ cronExprArray = [
91
177
(int )($ time [1 ] ?? 0 ), //Minute
92
178
(int )($ time [0 ] ?? 0 ), //Hour
@@ -95,23 +181,41 @@ public function afterAfterSave(
95
181
$ frequency == Frequency::CRON_WEEKLY ? '1 ' : '* ' , //# Day of the Week
96
182
];
97
183
98
- $ cronExprString = join (' ' , $ cronExprArray );
184
+ return join (' ' , $ cronExprArray );
185
+ }
99
186
187
+ /**
188
+ * Get observer model based on generation method
189
+ *
190
+ * @param string $generationMethod
191
+ * @return string
192
+ */
193
+ private function getObserverModel (string $ generationMethod ): string
194
+ {
195
+ return $ generationMethod === GenerationMethod::BATCH
196
+ ? 'Magento\Sitemap\Model\Batch\Observer::scheduledGenerateSitemaps '
197
+ : 'Magento\Sitemap\Model\Observer::scheduledGenerateSitemaps ' ;
198
+ }
199
+
200
+ /**
201
+ * Update cron configuration with new values
202
+ *
203
+ * @param string $cronExprString
204
+ * @param string $observerModel
205
+ * @return void
206
+ * @throws LocalizedException
207
+ */
208
+ private function updateCronConfiguration (string $ cronExprString , string $ observerModel ): void
209
+ {
100
210
try {
101
211
$ this ->clearCronConfiguration (self ::CRON_STRING_PATH );
102
212
$ this ->clearCronConfiguration (self ::CRON_MODEL_PATH );
103
213
104
- $ observerModel = $ generationMethod === GenerationMethod::BATCH
105
- ? 'Magento\Sitemap\Model\Batch\Observer::scheduledGenerateSitemaps '
106
- : 'Magento\Sitemap\Model\Observer::scheduledGenerateSitemaps ' ;
107
-
108
214
$ this ->setCronConfiguration (self ::CRON_STRING_PATH , $ cronExprString );
109
215
$ this ->setCronConfiguration (self ::CRON_MODEL_PATH , $ observerModel );
110
216
} catch (\Exception $ e ) {
111
217
throw new LocalizedException (__ ('We can \'t save the cron expression. ' ));
112
218
}
113
-
114
- return $ result ;
115
219
}
116
220
117
221
/**
@@ -120,6 +224,7 @@ public function afterAfterSave(
120
224
* @param string $path
121
225
* @param string $value
122
226
* @return void
227
+ * @throws LocalizedException
123
228
*/
124
229
private function setCronConfiguration (string $ path , string $ value ): void
125
230
{
@@ -138,6 +243,7 @@ private function setCronConfiguration(string $path, string $value): void
138
243
*
139
244
* @param string $path
140
245
* @return void
246
+ * @throws LocalizedException
141
247
*/
142
248
private function clearCronConfiguration (string $ path ): void
143
249
{
0 commit comments