-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSitemapController.php
More file actions
109 lines (91 loc) · 3.39 KB
/
SitemapController.php
File metadata and controls
109 lines (91 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace App\Http\Controllers\Sitemap;
use App\Actions\PageAction;
use App\DTO\PageDTO;
use App\Enums\LocaleEnum;
use App\Http\Controllers\Controller;
use App\Models\News;
use App\Models\Product;
use App\Models\Service;
use App\Sitemap\SitemapBuilder;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;
class SitemapController extends Controller
{
protected ?SitemapBuilder $sitemap = null;
protected const array DEFAULT_ROUTES = [
'start.index',
'about-us.index',
'products.index',
'services.index',
'contact.index',
'legal.imprint.index',
];
protected const array DEFAULT_LOCALES = [
LocaleEnum::DE->value,
LocaleEnum::EN->value,
];
public function __invoke(): Response
{
$content = Cache::rememberForever(key: 'sitemap_xml', callback: function (): string {
$this->sitemap = new SitemapBuilder;
$this->builder();
return $this->sitemap->toXml();
});
return response(content: $content)
->header('Content-Type', 'application/xml');
}
private function builder(): void
{
$this->addDefaultRoutesToSitemap();
News::whereNotNull('published_at')
->with('references')
->each(function (News $news): void {
$this->addLocalizedPageSet(
page: (new PageAction(locale: null, routeName: null))->news(news: $news, withReferences: true),
);
});
Service::where('published', true)
->with('references')
->each(function (Service $service): void {
$this->addLocalizedPageSet(
page: (new PageAction(locale: null, routeName: null))->service(service: $service, withReferences: true),
);
});
Product::where('published', true)
->with('references')
->each(function (Product $product): void {
$this->addLocalizedPageSet(
page: (new PageAction(locale: null, routeName: null))->product(product: $product, withReferences: true),
);
});
}
private function addDefaultRoutesToSitemap(): void
{
collect(value: self::DEFAULT_ROUTES)->each(function (string $routeName): void {
$pages = collect(self::DEFAULT_LOCALES)
->map(function (string $locale) use ($routeName): ?PageDTO {
return (new PageAction(locale: $locale, routeName: $routeName))->default();
})
->filter();
$pages->each(function (PageDTO $page) use ($pages): void {
$page->referencePages = $pages;
});
$pages->each(function (PageDTO $page): void {
$this->sitemap->addItem(page: $page);
});
});
}
private function addLocalizedPageSet(PageDTO $page): void
{
$pages = collect(value: $page->referencePages)
->prepend(value: $page)
->unique(fn (PageDTO $p): string => $p->locale);
$pages->each(function (PageDTO $page) use ($pages): void {
$page->referencePages = collect(value: [$page])
->merge($pages->reject(fn (PageDTO $ref): bool => $ref->locale === $page->locale))
->values();
$this->sitemap->addItem(page: $page);
});
}
}