Conditions | 22 |
Paths | 3 |
Total Lines | 113 |
Code Lines | 71 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
128 | public function render(array $params = []): string |
||
129 | { |
||
130 | $cache = Craft::$app->getCache(); |
||
131 | $groupId = $params['groupId']; |
||
132 | $siteId = $params['siteId']; |
||
133 | if (Seomatic::$settings->siteGroupsSeparate) { |
||
134 | /** @var SiteGroup|null $siteGroup */ |
||
135 | $siteGroup = Craft::$app->getSites()->getGroupById($groupId); |
||
136 | if ($siteGroup === null) { |
||
137 | throw new NotFoundHttpException(Craft::t('seomatic', 'Sitemap.xml not found for groupId {groupId}', [ |
||
138 | 'groupId' => $groupId, |
||
139 | ])); |
||
140 | } |
||
141 | $groupSiteIds = $siteGroup->getSiteIds(); |
||
142 | } else { |
||
143 | $groupSiteIds = Craft::$app->getSites()->allSiteIds; |
||
144 | } |
||
145 | |||
146 | $dependency = new TagDependency([ |
||
147 | 'tags' => [ |
||
148 | self::GLOBAL_SITEMAP_CACHE_TAG, |
||
149 | self::SITEMAP_INDEX_CACHE_TAG, |
||
150 | ], |
||
151 | ]); |
||
152 | |||
153 | return $cache->getOrSet(self::CACHE_KEY . $groupId . '.' . $siteId, function() use ($groupSiteIds, $siteId) { |
||
154 | Craft::info( |
||
155 | 'Sitemap index cache miss', |
||
156 | __METHOD__ |
||
157 | ); |
||
158 | $lines = []; |
||
159 | // Sitemap index XML header and opening tag |
||
160 | $lines[] = '<?xml version="1.0" encoding="UTF-8"?>'; |
||
161 | $lines[] = '<?xml-stylesheet type="text/xsl" href="sitemap.xsl"?>'; |
||
162 | $lines[] = '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; |
||
163 | // One sitemap entry for each MeteBundle |
||
164 | $metaBundles = Seomatic::$plugin->metaBundles->getContentMetaBundlesForSiteId($siteId); |
||
165 | Seomatic::$plugin->metaBundles->pruneVestigialMetaBundles($metaBundles); |
||
166 | /** @var MetaBundle $metaBundle */ |
||
167 | foreach ($metaBundles as $metaBundle) { |
||
168 | $sitemapUrls = $metaBundle->metaSitemapVars->sitemapUrls; |
||
169 | // Check to see if robots is `none` or `no index` |
||
170 | $robotsEnabled = true; |
||
171 | if (!empty($metaBundle->metaGlobalVars->robots)) { |
||
172 | $robotsEnabled = $metaBundle->metaGlobalVars->robots !== 'none' && |
||
173 | $metaBundle->metaGlobalVars->robots !== 'noindex'; |
||
174 | } |
||
175 | if (Seomatic::$plugin->sitemaps->anyEntryTypeHasSitemapUrls($metaBundle)) { |
||
176 | $robotsEnabled = true; |
||
177 | $sitemapUrls = true; |
||
178 | } |
||
179 | // Only add in a sitemap entry if it meets our criteria |
||
180 | if (in_array($metaBundle->sourceSiteId, $groupSiteIds, false) |
||
181 | && $sitemapUrls |
||
182 | && $robotsEnabled) { |
||
183 | // Get all of the elements for this meta bundle type |
||
184 | $seoElement = Seomatic::$plugin->seoElements->getSeoElementByMetaBundleType($metaBundle->sourceBundleType); |
||
185 | $totalElements = 0; |
||
186 | $pageCount = 0; |
||
187 | |||
188 | if ($seoElement !== null) { |
||
189 | // Ensure `null` so that the resulting element query is correct |
||
190 | if (empty($metaBundle->metaSitemapVars->sitemapLimit)) { |
||
191 | $metaBundle->metaSitemapVars->sitemapLimit = null; |
||
192 | } |
||
193 | |||
194 | $totalElements = $seoElement::sitemapElementsQuery($metaBundle)->count(); |
||
195 | |||
196 | if ($metaBundle->metaSitemapVars->sitemapLimit && ($totalElements > $metaBundle->metaSitemapVars->sitemapLimit)) { |
||
197 | $totalElements = $metaBundle->metaSitemapVars->sitemapLimit; |
||
198 | } |
||
199 | |||
200 | $pageSize = $metaBundle->metaSitemapVars->sitemapPageSize; |
||
201 | $pageCount = (!empty($pageSize) && $pageSize > 0) ? ceil($totalElements / $pageSize) : 1; |
||
202 | } |
||
203 | |||
204 | // Only add a sitemap to the sitemap index if there's at least 1 element in the resulting sitemap |
||
205 | if ($totalElements > 0 && $pageCount > 0) { |
||
206 | for ($page = 1; $page <= $pageCount; $page++) { |
||
207 | $sitemapUrl = Seomatic::$plugin->sitemaps->sitemapUrlForBundle( |
||
208 | $metaBundle->sourceBundleType, |
||
209 | $metaBundle->sourceHandle, |
||
210 | $metaBundle->sourceSiteId, |
||
211 | $pageCount > 1 ? $page : 0 // No paging, if only one page |
||
212 | ); |
||
213 | |||
214 | $lines[] = '<sitemap>'; |
||
215 | $lines[] = '<loc>'; |
||
216 | $lines[] = Html::encode($sitemapUrl); |
||
217 | $lines[] = '</loc>'; |
||
218 | |||
219 | if ($metaBundle->sourceDateUpdated !== null) { |
||
220 | $lines[] = '<lastmod>'; |
||
221 | $lines[] = $metaBundle->sourceDateUpdated->format(DateTime::W3C); |
||
222 | $lines[] = '</lastmod>'; |
||
223 | } |
||
224 | |||
225 | $lines[] = '</sitemap>'; |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | // Custom sitemap entries |
||
231 | $metaBundle = Seomatic::$plugin->metaBundles->getGlobalMetaBundle($siteId, false); |
||
232 | if ($metaBundle !== null) { |
||
233 | $this->addAdditionalSitemapUrls($metaBundle, $siteId, $lines); |
||
234 | $this->addAdditionalSitemaps($metaBundle, $siteId, $lines); |
||
235 | } |
||
236 | // Sitemap index closing tag |
||
237 | $lines[] = '</sitemapindex>'; |
||
238 | |||
239 | return implode('', $lines); |
||
240 | }, Seomatic::$cacheDuration, $dependency); |
||
241 | } |
||
351 |