Total Complexity | 66 |
Total Lines | 494 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Complex classes like Sitemaps often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Sitemaps, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class Sitemaps extends Component implements SitemapInterface |
||
42 | { |
||
43 | // Constants |
||
44 | // ========================================================================= |
||
45 | |||
46 | public const SEOMATIC_SITEMAPINDEX_CONTAINER = Seomatic::SEOMATIC_HANDLE . SitemapIndexTemplate::TEMPLATE_TYPE; |
||
47 | |||
48 | public const SEOMATIC_SITEMAP_CONTAINER = Seomatic::SEOMATIC_HANDLE . SitemapTemplate::TEMPLATE_TYPE; |
||
49 | |||
50 | public const SEOMATIC_SITEMAPCUSTOM_CONTAINER = Seomatic::SEOMATIC_HANDLE . SitemapCustomTemplate::TEMPLATE_TYPE; |
||
51 | |||
52 | public const SEARCH_ENGINE_SUBMISSION_URLS = [ |
||
53 | ]; |
||
54 | |||
55 | // Protected Properties |
||
56 | // ========================================================================= |
||
57 | |||
58 | /** |
||
59 | * @var FrontendTemplateContainer |
||
60 | */ |
||
61 | protected $sitemapTemplateContainer; |
||
62 | |||
63 | // Public Methods |
||
64 | // ========================================================================= |
||
65 | |||
66 | /** |
||
67 | * Load in the sitemap frontend template containers |
||
68 | */ |
||
69 | public function loadSitemapContainers() |
||
95 | ); |
||
96 | } |
||
97 | ); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return array |
||
103 | */ |
||
104 | public function sitemapRouteRules(): array |
||
105 | { |
||
106 | $rules = []; |
||
107 | $groups = Craft::$app->getSites()->getAllGroups(); |
||
108 | $groupId = $groups[0]->id; |
||
109 | $currentSite = null; |
||
110 | try { |
||
111 | $currentSite = Craft::$app->getSites()->getCurrentSite(); |
||
112 | } catch (SiteNotFoundException $e) { |
||
113 | Craft::error($e->getMessage(), __METHOD__); |
||
114 | } |
||
115 | if ($currentSite) { |
||
116 | try { |
||
117 | $groupId = $currentSite->getGroup()->id; |
||
118 | } catch (InvalidConfigException $e) { |
||
119 | Craft::error($e->getMessage(), __METHOD__); |
||
120 | } |
||
121 | } |
||
122 | // Add the route to redirect sitemap.xml to the actual sitemap |
||
123 | $route = |
||
124 | Seomatic::$plugin->handle |
||
125 | . '/' |
||
126 | . 'sitemap' |
||
127 | . '/' |
||
128 | . 'sitemap-index-redirect'; |
||
129 | $rules['sitemap.xml'] = [ |
||
130 | 'route' => $route, |
||
131 | ]; |
||
132 | // Add the route for the sitemap.xsl styles |
||
133 | $route = |
||
134 | Seomatic::$plugin->handle |
||
135 | . '/' |
||
136 | . 'sitemap' |
||
137 | . '/' |
||
138 | . 'sitemap-styles'; |
||
139 | $rules['sitemap.xsl'] = [ |
||
140 | 'route' => $route, |
||
141 | ]; |
||
142 | // Add the route for the sitemap-empty.xsl styles |
||
143 | $route = |
||
144 | Seomatic::$plugin->handle |
||
145 | . '/' |
||
146 | . 'sitemap' |
||
147 | . '/' |
||
148 | . 'sitemap-empty-styles'; |
||
149 | $rules['sitemap-empty.xsl'] = [ |
||
150 | 'route' => $route, |
||
151 | ]; |
||
152 | // Add all of the frontend container routes |
||
153 | foreach ($this->sitemapTemplateContainer->data as $sitemapTemplate) { |
||
154 | $rules = array_merge( |
||
155 | $rules, |
||
156 | $sitemapTemplate->routeRules() |
||
157 | ); |
||
158 | } |
||
159 | |||
160 | return $rules; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * See if any of the entry types have robots enable and sitemap urls enabled |
||
165 | * |
||
166 | * @param MetaBundle $metaBundle |
||
167 | * @return bool |
||
168 | */ |
||
169 | public function anyEntryTypeHasSitemapUrls(MetaBundle $metaBundle): bool |
||
170 | { |
||
171 | $result = false; |
||
172 | $seoElement = Seomatic::$plugin->seoElements->getSeoElementByMetaBundleType($metaBundle->sourceBundleType); |
||
173 | if ($seoElement) { |
||
174 | if (!empty($seoElement::typeMenuFromHandle($metaBundle->sourceHandle))) { |
||
175 | /** @var Section|null $section */ |
||
176 | $section = $seoElement::sourceModelFromHandle($metaBundle->sourceHandle); |
||
177 | if ($section !== null) { |
||
178 | $entryTypes = $section->getEntryTypes(); |
||
179 | // Fetch each meta bundle for each entry type to see if _any_ of them have sitemap URLs |
||
180 | foreach ($entryTypes as $entryType) { |
||
181 | $entryTypeBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceId( |
||
182 | $metaBundle->sourceBundleType, |
||
183 | $metaBundle->sourceId, |
||
184 | $metaBundle->sourceSiteId, |
||
185 | $entryType->id |
||
186 | ); |
||
187 | if ($entryTypeBundle) { |
||
188 | $robotsEnabled = true; |
||
189 | if (!empty($entryTypeBundle->metaGlobalVars->robots)) { |
||
190 | $robotsEnabled = $entryTypeBundle->metaGlobalVars->robots !== 'none' && |
||
191 | $entryTypeBundle->metaGlobalVars->robots !== 'noindex'; |
||
192 | } |
||
193 | if ($entryTypeBundle->metaSitemapVars->sitemapUrls && $robotsEnabled) { |
||
194 | $result = true; |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | |||
202 | return $result; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @param string $template |
||
207 | * @param array $params |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | public function renderTemplate(string $template, array $params = []): string |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Submit the sitemap index to the search engine services |
||
225 | */ |
||
226 | public function submitSitemapIndex() |
||
255 | ); |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | } |
||
261 | } |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Get the URL to the $siteId's sitemap index |
||
266 | * |
||
267 | * @param int|null $siteId |
||
268 | * |
||
269 | * @return string |
||
270 | */ |
||
271 | public function sitemapIndexUrlForSiteId(int $siteId = null): string |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Submit the bundle sitemap to the search engine services |
||
299 | * |
||
300 | * @param ElementInterface $element |
||
301 | */ |
||
302 | public function submitSitemapForElement(ElementInterface $element) |
||
329 | ); |
||
330 | } |
||
331 | } |
||
332 | } |
||
333 | } |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * @param string $sourceBundleType |
||
338 | * @param string $sourceHandle |
||
339 | * @param int|null $siteId |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | public function sitemapUrlForBundle(string $sourceBundleType, string $sourceHandle, int $siteId = null, int $page = null): string |
||
344 | { |
||
345 | $url = ''; |
||
346 | $sites = Craft::$app->getSites(); |
||
347 | if ($siteId === null) { |
||
348 | $siteId = $sites->currentSite->id ?? 1; |
||
349 | } |
||
350 | $site = $sites->getSiteById($siteId); |
||
351 | $metaBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceHandle( |
||
352 | $sourceBundleType, |
||
353 | $sourceHandle, |
||
354 | $siteId |
||
355 | ); |
||
356 | if ($site && $metaBundle && $metaBundle->metaSitemapVars->sitemapUrls) { |
||
357 | try { |
||
358 | $url = UrlHelper::siteUrl( |
||
359 | '/sitemaps-' |
||
360 | . $site->groupId |
||
361 | . '-' |
||
362 | . $metaBundle->sourceBundleType |
||
363 | . '-' |
||
364 | . $metaBundle->sourceHandle |
||
365 | . '-' |
||
366 | . $metaBundle->sourceSiteId |
||
367 | . '-sitemap' |
||
368 | . (!empty($page) ? '-p' . $page : '') |
||
369 | . '.xml', |
||
370 | null, |
||
371 | null, |
||
372 | $siteId |
||
373 | ); |
||
374 | } catch (Exception $e) { |
||
375 | Craft::error($e->getMessage(), __METHOD__); |
||
376 | } |
||
377 | } |
||
378 | |||
379 | return $url; |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * Submit the bundle sitemap to the search engine services |
||
384 | * |
||
385 | * @param int $siteId |
||
386 | */ |
||
387 | public function submitCustomSitemap(int $siteId) |
||
388 | { |
||
389 | if (Seomatic::$settings->sitemapsEnabled && Seomatic::$environment === 'live' && Seomatic::$settings->submitSitemaps) { |
||
390 | // Submit the sitemap to each search engine |
||
391 | $searchEngineUrls = self::SEARCH_ENGINE_SUBMISSION_URLS; |
||
392 | // Array is currently empty, but leave the code in place in case submission urls return |
||
393 | /** @phpstan-ignore-next-line */ |
||
394 | foreach ($searchEngineUrls as &$url) { |
||
395 | $sitemapUrl = $this->sitemapCustomUrlForSiteId($siteId); |
||
396 | if (!empty($sitemapUrl)) { |
||
397 | $submissionUrl = $url . urlencode($sitemapUrl); |
||
398 | // create new guzzle client |
||
399 | $guzzleClient = Craft::createGuzzleClient(['timeout' => 5, 'connect_timeout' => 5]); |
||
400 | // Submit the sitemap index to each search engine |
||
401 | try { |
||
402 | $guzzleClient->post($submissionUrl); |
||
403 | Craft::info( |
||
404 | 'Sitemap Custom submitted to: ' . $submissionUrl, |
||
405 | __METHOD__ |
||
406 | ); |
||
407 | } catch (\Exception $e) { |
||
408 | Craft::error( |
||
409 | 'Error submitting sitemap index to: ' . $submissionUrl . ' - ' . $e->getMessage(), |
||
410 | __METHOD__ |
||
411 | ); |
||
412 | } |
||
413 | } |
||
414 | } |
||
415 | } |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * @param int|null $siteId |
||
420 | * |
||
421 | * @return string |
||
422 | */ |
||
423 | public function sitemapCustomUrlForSiteId(int $siteId = null) |
||
424 | { |
||
425 | $url = ''; |
||
426 | $sites = Craft::$app->getSites(); |
||
427 | if ($siteId === null) { |
||
428 | $siteId = $sites->currentSite->id ?? 1; |
||
429 | } |
||
430 | $site = $sites->getSiteById($siteId); |
||
431 | if ($site) { |
||
432 | try { |
||
433 | $url = UrlHelper::siteUrl( |
||
434 | '/sitemaps-' |
||
435 | . $site->groupId |
||
436 | . '-' |
||
437 | . SitemapCustomTemplate::CUSTOM_SCOPE |
||
438 | . '-' |
||
439 | . SitemapCustomTemplate::CUSTOM_HANDLE |
||
440 | . '-' |
||
441 | . $siteId |
||
442 | . '-sitemap.xml', |
||
443 | null, |
||
444 | null, |
||
445 | $siteId |
||
446 | ); |
||
447 | } catch (Exception $e) { |
||
448 | Craft::error($e->getMessage(), __METHOD__); |
||
449 | } |
||
450 | } |
||
451 | |||
452 | return $url; |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Return all of the sitemap indexes the current group of sites |
||
457 | * |
||
458 | * @return string |
||
459 | */ |
||
460 | public function sitemapIndex(): string |
||
461 | { |
||
462 | $result = ''; |
||
463 | $sites = []; |
||
464 | // If sitemaps aren't enabled globally, return nothing for the sitemap index |
||
465 | if (!Seomatic::$settings->sitemapsEnabled) { |
||
466 | return ''; |
||
467 | } |
||
468 | if (Seomatic::$settings->siteGroupsSeparate) { |
||
469 | // Get only the sites that are in the current site's group |
||
470 | try { |
||
471 | $siteGroup = Craft::$app->getSites()->getCurrentSite()->getGroup(); |
||
472 | } catch (InvalidConfigException $e) { |
||
473 | $siteGroup = null; |
||
474 | Craft::error($e->getMessage(), __METHOD__); |
||
475 | } |
||
476 | // If we can't get a group, just use the current site |
||
477 | if ($siteGroup === null) { |
||
478 | $sites = [Craft::$app->getSites()->getCurrentSite()]; |
||
479 | } else { |
||
480 | $sites = $siteGroup->getSites(); |
||
481 | } |
||
482 | } else { |
||
483 | $sites = Craft::$app->getSites()->getAllSites(); |
||
484 | } |
||
485 | |||
486 | foreach ($sites as $site) { |
||
487 | $result .= 'sitemap: ' . $this->sitemapIndexUrlForSiteId($site->id) . PHP_EOL; |
||
488 | } |
||
489 | |||
490 | return rtrim($result, PHP_EOL); |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * Invalidate all of the sitemap caches |
||
495 | */ |
||
496 | public function invalidateCaches() |
||
503 | ); |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * Invalidate the sitemap cache passed in $handle |
||
508 | * |
||
509 | * @param string $handle |
||
510 | * @param int|null $siteId |
||
511 | * @param string $type |
||
512 | * @param bool $invalidateCache |
||
513 | */ |
||
514 | public function invalidateSitemapCache(string $handle, ?int $siteId, string $type, bool $invalidateCache = true) |
||
522 | ); |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * Invalidate the sitemap index cache |
||
527 | */ |
||
528 | public function invalidateSitemapIndexCache() |
||
538 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths