Skip to content

Commit

Permalink
refactor: Phpstan code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Jul 19, 2024
1 parent cf23b42 commit 2627301
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
16 changes: 9 additions & 7 deletions src/gql/resolvers/SitemapResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected static function getSitemapItemByFilename($filename)
* @param string $bundleType
* @param string $bundleHandle
* @param int $siteId
* @return string
* @return array
*/
protected static function createSitemapFilenamesFromComponents(int $groupId, string $bundleType, string $bundleHandle, int $siteId): array
{
Expand All @@ -182,13 +182,15 @@ protected static function createSitemapFilenamesFromComponents(int $groupId, str
return ["sitemaps-$groupId-$bundleType-$bundleHandle-$siteId-sitemap.xml"];
}

$seoElementClass = Seomatic::$plugin->seoElements->getSeoElementByMetaBundleType($metaBundle->sourceBundleType);
$totalElements = Sitemap::getTotalElementsInSitemap($seoElementClass, $metaBundle);
$pageCount = (!empty($pageSize) && $pageSize > 0) ? ceil($totalElements / $pageSize) : 1;

$sitemapFilenames = [];
for ($page = 1; $page <= $pageCount; $page++) {
$sitemapFilenames[] = sprintf('sitemaps-%d-%s-%s-%d-sitemap-p%d.xml', $groupId, $bundleType, $bundleHandle, $siteId, $page);
$seoElement = Seomatic::$plugin->seoElements->getSeoElementByMetaBundleType($metaBundle->sourceBundleType);
if ($seoElement) {
$totalElements = Sitemap::getTotalElementsInSitemap($seoElement, $metaBundle);
$pageCount = $pageSize > 0 ? ceil($totalElements / $pageSize) : 1;

for ($page = 1; $page <= $pageCount; $page++) {
$sitemapFilenames[] = sprintf('sitemaps-%d-%s-%s-%d-sitemap-p%d.xml', $groupId, $bundleType, $bundleHandle, $siteId, $page);
}
}

return $sitemapFilenames;
Expand Down
35 changes: 17 additions & 18 deletions src/helpers/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,23 @@ public static function generateSitemap(array $params): ?string
return implode('', $lines);
}

/**
* Return the total number of elements in a sitemap, respecting metabundle settings.
*
* @param SeoElementInterface $seoElementClass
* @param MetaBundle $metaBundle
* @return int|null
*/
public static function getTotalElementsInSitemap(SeoElementInterface $seoElementClass, MetaBundle $metaBundle): ?int
{
$totalElements = $seoElementClass::sitemapElementsQuery($metaBundle)->count();

if ($metaBundle->metaSitemapVars->sitemapLimit && ($totalElements > $metaBundle->metaSitemapVars->sitemapLimit)) {
$totalElements = $metaBundle->metaSitemapVars->sitemapLimit;
}

return $totalElements;
}

/**
* Combine any per-entry type field settings from $element with the passed in
Expand Down Expand Up @@ -610,22 +627,4 @@ protected static function assetFilesSitemapLink(Asset $asset, MetaBundle $metaBu
}
}
}

/**
* Return the total number of elements in a sitemap, respecting metabundle settings.
*
* @param string $seoElementClass
* @param MetaBundle $metaBundle
* @return int|null
*/
public static function getTotalElementsInSitemap(string $seoElementClass, MetaBundle $metaBundle)
{
$totalElements = $seoElementClass::sitemapElementsQuery($metaBundle)->count();

if ($metaBundle->metaSitemapVars->sitemapLimit && ($totalElements > $metaBundle->metaSitemapVars->sitemapLimit)) {
$totalElements = $metaBundle->metaSitemapVars->sitemapLimit;
}

return $totalElements;
}
}

0 comments on commit 2627301

Please sign in to comment.