99
1010use Magento \Cron \Model \Config \Backend \Sitemap ;
1111use Magento \Cron \Model \Config \Source \Frequency ;
12+ use Magento \Framework \App \Config \ScopeConfigInterface ;
13+ use Magento \Framework \App \Config \ValueFactory ;
1214use Magento \Framework \Exception \LocalizedException ;
1315use Magento \Sitemap \Model \Config \Source \GenerationMethod ;
1416
@@ -28,15 +30,15 @@ class SitemapPlugin
2830 private const CRON_MODEL_PATH = 'crontab/default/jobs/sitemap_generate/run/model ' ;
2931
3032 /**
31- * @var \Magento\Framework\App\Config\ ValueFactory
33+ * @var ValueFactory
3234 */
3335 private $ configValueFactory ;
3436
3537 /**
36- * @param \Magento\Framework\App\Config\ ValueFactory $configValueFactory
38+ * @param ValueFactory $configValueFactory
3739 */
3840 public function __construct (
39- \ Magento \ Framework \ App \ Config \ ValueFactory $ configValueFactory
41+ ValueFactory $ configValueFactory
4042 ) {
4143 $ this ->configValueFactory = $ configValueFactory ;
4244 }
@@ -55,18 +57,80 @@ public function afterAfterSave(
5557 ) {
5658 $ config = $ subject ->getConfig ();
5759
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+ {
5882 $ time = $ subject ->getData ('groups/generate/fields/time/value ' );
5983 if (!$ time && $ config ) {
6084 $ timeConfig = $ config ->getValue (
6185 'sitemap/generate/time ' ,
6286 $ subject ->getScope (),
6387 $ subject ->getScopeId ()
6488 );
65- $ time = $ timeConfig ? explode (', ' , $ timeConfig ) : [ ' 0 ' , ' 0 ' , ' 0 ' ] ;
89+ $ time = $ timeConfig ? explode (', ' , $ timeConfig ) : null ;
6690 }
6791
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+ }
69123
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+ {
70134 $ generationMethod = $ subject ->getData ('groups/generate/fields/generation_method/value ' );
71135
72136 if (!$ generationMethod && $ config ) {
@@ -75,18 +139,40 @@ public function afterAfterSave(
75139 $ subject ->getScope (),
76140 $ subject ->getScopeId ()
77141 );
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- }
86142 }
87143
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+ }
89166
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+ {
90176 $ cronExprArray = [
91177 (int )($ time [1 ] ?? 0 ), //Minute
92178 (int )($ time [0 ] ?? 0 ), //Hour
@@ -95,23 +181,41 @@ public function afterAfterSave(
95181 $ frequency == Frequency::CRON_WEEKLY ? '1 ' : '* ' , //# Day of the Week
96182 ];
97183
98- $ cronExprString = join (' ' , $ cronExprArray );
184+ return join (' ' , $ cronExprArray );
185+ }
99186
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+ {
100210 try {
101211 $ this ->clearCronConfiguration (self ::CRON_STRING_PATH );
102212 $ this ->clearCronConfiguration (self ::CRON_MODEL_PATH );
103213
104- $ observerModel = $ generationMethod === GenerationMethod::BATCH
105- ? 'Magento\Sitemap\Model\Batch\Observer::scheduledGenerateSitemaps '
106- : 'Magento\Sitemap\Model\Observer::scheduledGenerateSitemaps ' ;
107-
108214 $ this ->setCronConfiguration (self ::CRON_STRING_PATH , $ cronExprString );
109215 $ this ->setCronConfiguration (self ::CRON_MODEL_PATH , $ observerModel );
110216 } catch (\Exception $ e ) {
111217 throw new LocalizedException (__ ('We can \'t save the cron expression. ' ));
112218 }
113-
114- return $ result ;
115219 }
116220
117221 /**
@@ -120,6 +224,7 @@ public function afterAfterSave(
120224 * @param string $path
121225 * @param string $value
122226 * @return void
227+ * @throws LocalizedException
123228 */
124229 private function setCronConfiguration (string $ path , string $ value ): void
125230 {
@@ -138,6 +243,7 @@ private function setCronConfiguration(string $path, string $value): void
138243 *
139244 * @param string $path
140245 * @return void
246+ * @throws LocalizedException
141247 */
142248 private function clearCronConfiguration (string $ path ): void
143249 {
0 commit comments