| Conditions | 19 |
| Paths | 158 |
| Total Lines | 92 |
| Code Lines | 57 |
| 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 |
||
| 103 | public function render(array $params = []): string |
||
| 104 | { |
||
| 105 | $groupId = $params['groupId']; |
||
| 106 | $type = $params['type']; |
||
| 107 | $handle = $params['handle']; |
||
| 108 | $siteId = $params['siteId']; |
||
| 109 | $page = $params['page'] ?? 0; |
||
| 110 | |||
| 111 | $request = Craft::$app->getRequest(); |
||
| 112 | $metaBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceHandle($type, $handle, $siteId); |
||
| 113 | // If it doesn't exist, throw a 404 |
||
| 114 | if ($metaBundle === null) { |
||
| 115 | if ($request->isCpRequest || $request->isConsoleRequest) { |
||
| 116 | return ''; |
||
| 117 | } |
||
| 118 | throw new NotFoundHttpException(Craft::t('seomatic', 'Page not found.')); |
||
| 119 | } |
||
| 120 | // Check to see if robots is `none` or `no index` |
||
| 121 | $robotsEnabled = true; |
||
| 122 | if (!empty($metaBundle->metaGlobalVars->robots)) { |
||
| 123 | $robotsEnabled = $metaBundle->metaGlobalVars->robots !== 'none' && |
||
| 124 | $metaBundle->metaGlobalVars->robots !== 'noindex'; |
||
| 125 | } |
||
| 126 | $sitemapUrls = $metaBundle->metaSitemapVars->sitemapUrls; |
||
| 127 | if (Seomatic::$plugin->sitemaps->anyEntryTypeHasSitemapUrls($metaBundle)) { |
||
| 128 | $robotsEnabled = true; |
||
| 129 | $sitemapUrls = true; |
||
| 130 | } |
||
| 131 | if ($sitemapUrls && !SiteHelper::siteEnabledWithUrls($siteId)) { |
||
| 132 | $sitemapUrls = false; |
||
| 133 | } |
||
| 134 | // If it's disabled, just throw a 404 |
||
| 135 | if (!$sitemapUrls || !$robotsEnabled) { |
||
| 136 | if ($request->isCpRequest || $request->isConsoleRequest) { |
||
| 137 | return ''; |
||
| 138 | } |
||
| 139 | throw new NotFoundHttpException(Craft::t('seomatic', 'Page not found.')); |
||
| 140 | } |
||
| 141 | |||
| 142 | $cache = Craft::$app->getCache(); |
||
| 143 | $pageCacheSuffix = 's' . (int)$metaBundle->metaSitemapVars->sitemapPageSize . 'p' . $page; |
||
| 144 | |||
| 145 | $uniqueKey = $groupId . $type . $handle . $siteId . $pageCacheSuffix; |
||
| 146 | $cacheKey = self::CACHE_KEY . $uniqueKey; |
||
| 147 | $result = $cache->get($cacheKey); |
||
| 148 | |||
| 149 | // If the sitemap isn't cached, render it immediately |
||
| 150 | if ($result === false) { |
||
| 151 | $sitemap = Sitemap::generateSitemap([ |
||
| 152 | 'groupId' => $groupId, |
||
| 153 | 'type' => $type, |
||
| 154 | 'handle' => $handle, |
||
| 155 | 'siteId' => $siteId, |
||
| 156 | 'page' => $page, |
||
| 157 | ]); |
||
| 158 | |||
| 159 | if ($sitemap) { |
||
| 160 | $dependency = new TagDependency([ |
||
| 161 | 'tags' => [ |
||
| 162 | self::GLOBAL_SITEMAP_CACHE_TAG, |
||
| 163 | self::SITEMAP_CACHE_TAG . $handle . $siteId, |
||
| 164 | self::SITEMAP_CACHE_TAG . $handle . $siteId . $pageCacheSuffix, |
||
| 165 | ], |
||
| 166 | ]); |
||
| 167 | |||
| 168 | $cacheDuration = Seomatic::$devMode |
||
| 169 | ? Seomatic::DEVMODE_CACHE_DURATION |
||
| 170 | : null; |
||
| 171 | |||
| 172 | $result = $cache->set($cacheKey, $sitemap, $cacheDuration, $dependency); |
||
| 173 | |||
| 174 | // Output some info if this is a console app |
||
| 175 | if (Craft::$app instanceof ConsoleApplication) { |
||
| 176 | echo 'Sitemap cache result: ' . print_r($result, true) . ' for cache key: ' . $cacheKey . PHP_EOL; |
||
|
|
|||
| 177 | } |
||
| 178 | |||
| 179 | // If the FastCGI Cache Bust plugin is installed, clear its caches too |
||
| 180 | /** @var FastcgiCacheBust $plugin */ |
||
| 181 | $plugin = Craft::$app->getPlugins()->getPlugin('fastcgi-cache-bust'); |
||
| 182 | if ($plugin !== null) { |
||
| 183 | $plugin->cache->clearAll(); |
||
| 184 | } |
||
| 185 | |||
| 186 | return $sitemap; |
||
| 187 | } |
||
| 188 | } else { |
||
| 189 | if (Craft::$app instanceof ConsoleApplication) { |
||
| 190 | echo 'Found in cache' . PHP_EOL; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | return $result; |
||
| 195 | } |
||
| 213 |