| Conditions | 13 |
| Paths | 48 |
| Total Lines | 62 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 69 | public function actionGenerate() |
||
| 70 | { |
||
| 71 | echo 'Generating sitemap' . PHP_EOL; |
||
| 72 | if ($this->siteId !== null) { |
||
| 73 | $siteIds[] = $this->siteId; |
||
|
|
|||
| 74 | } else { |
||
| 75 | $siteIds = Craft::$app->getSites()->getAllSiteIds(); |
||
| 76 | if (!\is_array($siteIds)) { |
||
| 77 | $siteIds = [$siteIds]; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | // This might take a while |
||
| 81 | App::maxPowerCaptain(); |
||
| 82 | // Process the sitemap generation |
||
| 83 | foreach ($siteIds as $siteId) { |
||
| 84 | $metaBundles = Seomatic::$plugin->metaBundles->getContentMetaBundlesForSiteId($siteId); |
||
| 85 | Seomatic::$plugin->metaBundles->pruneVestigialMetaBundles($metaBundles); |
||
| 86 | |||
| 87 | /** @var MetaBundle $metaBundle */ |
||
| 88 | foreach ($metaBundles as $metaBundle) { |
||
| 89 | $process = false; |
||
| 90 | if ($this->handle === null || $this->handle === $metaBundle->sourceHandle) { |
||
| 91 | $process = true; |
||
| 92 | } |
||
| 93 | if ($metaBundle->metaSitemapVars->sitemapUrls && $process) { |
||
| 94 | echo 'Generating sitemap for ' |
||
| 95 | . $metaBundle->sourceType |
||
| 96 | . ' ' |
||
| 97 | . $metaBundle->sourceName |
||
| 98 | . ', siteId ' |
||
| 99 | . $siteId |
||
| 100 | . PHP_EOL; |
||
| 101 | |||
| 102 | $seoElement = Seomatic::$plugin->seoElements->getSeoElementByMetaBundleType($metaBundle->sourceBundleType); |
||
| 103 | $elementQuery = $seoElement::sitemapElementsQuery($metaBundle); |
||
| 104 | $pageSize = (int) $metaBundle->metaSitemapVars->sitemapPageSize; |
||
| 105 | $sitemapLimit = (int) $metaBundle->metaSitemapVars->sitemapLimit; |
||
| 106 | |||
| 107 | if (!empty($pageSize)) { |
||
| 108 | $total = empty($sitemapLimit) ? $elementQuery->count() : min($elementQuery->count(), $sitemapLimit); |
||
| 109 | $pageCount = ceil($total / $pageSize); |
||
| 110 | } else { |
||
| 111 | $pageCount = 1; |
||
| 112 | } |
||
| 113 | |||
| 114 | $site = Craft::$app->getSites()->getSiteById($metaBundle->sourceSiteId); |
||
| 115 | $sitemap = SitemapTemplate::create(); |
||
| 116 | if ($site) { |
||
| 117 | for ($pageNum = 1; $pageNum <= $pageCount; $pageNum++) { |
||
| 118 | echo sprintf('Generating page %d of %d' . PHP_EOL, $pageNum, $pageCount); |
||
| 119 | $sitemap->render([ |
||
| 120 | 'groupId' => $site->groupId, |
||
| 121 | 'siteId' => $metaBundle->sourceSiteId, |
||
| 122 | 'handle' => $metaBundle->sourceHandle, |
||
| 123 | 'type' => $metaBundle->sourceBundleType, |
||
| 124 | 'page' => $pageNum, |
||
| 125 | ]); |
||
| 126 | } |
||
| 127 | // Generate the sitemap so it is in the cache |
||
| 128 | } |
||
| 129 | |||
| 130 | echo '---' . PHP_EOL; |
||
| 131 | } |
||
| 139 |