Files
SkinbaseNova/app/Services/Sitemaps/Builders/GoogleNewsSitemapBuilder.php

52 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Sitemaps\Builders;
use App\Services\Sitemaps\AbstractSitemapBuilder;
use App\Services\Sitemaps\GoogleNewsSitemapUrl;
use DateTimeInterface;
use cPad\Plugins\News\Models\NewsArticle;
final class GoogleNewsSitemapBuilder extends AbstractSitemapBuilder
{
public function name(): string
{
return (string) \config('sitemaps.news.google_variant_name', 'news-google');
}
public function items(): array
{
return NewsArticle::query()
->published()
->where('published_at', '>=', now()->subHours(max(1, (int) \config('sitemaps.news.google_lookback_hours', 48))))
->orderByDesc('published_at')
->limit(max(1, (int) \config('sitemaps.news.google_max_items', 1000)))
->get()
->map(function (NewsArticle $article): ?GoogleNewsSitemapUrl {
if (trim((string) $article->slug) === '' || $article->published_at === null) {
return null;
}
return new GoogleNewsSitemapUrl(
route('news.show', ['slug' => $article->slug]),
trim((string) $article->title),
$article->published_at,
(string) \config('sitemaps.news.google_publication_name', 'Skinbase Nova'),
(string) \config('sitemaps.news.google_language', 'en'),
);
})
->filter(fn (?GoogleNewsSitemapUrl $item): bool => $item !== null && $item->title !== '')
->values()
->all();
}
public function lastModified(): ?DateTimeInterface
{
return $this->dateTime(NewsArticle::query()
->published()
->where('published_at', '>=', now()->subHours(max(1, (int) \config('sitemaps.news.google_lookback_hours', 48))))
->max('published_at'));
}
}