Lines 73.33% 22 / 30
Methods 75.00% 3 / 4
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 isEnabled 100.00% 1 / 1 100.00% 1 / 1 1
 collectUrls 100.00% 18 / 18 100.00% 1 / 1 4
 generateXml 20.00% 2 / 10 0.00% 0 / 1 4.05
28class SitemapXmlService
29{
30    private const int PMF_SITEMAP_GOOGLE_MAX_URLS = 50_000;
31
32    public function __construct(
33        private readonly Configuration $configuration,
34        private readonly FaqStatistics $faqStatistics,
35        private readonly CustomPage $customPage,
36    ) {
37    }
38
39    public function isEnabled(): bool
40    {
41        return (bool) $this->configuration->get(item: 'seo.enableXMLSitemap');
42    }
43
44    /**
45     * Collects all URLs for the sitemap from FAQs and active custom pages.
46     *
47     * @return array<int, array{loc: string, lastmod: string, priority: string}>
48     */
49    public function collectUrls(): array
50    {
51        $items = $this->faqStatistics->getTopTenData(self::PMF_SITEMAP_GOOGLE_MAX_URLS - 1);
52
53        $urls = [];
54        foreach ($items as $item) {
55            $urls[] = [
56                'loc' => $item['url'],
57                'lastmod' => $item['date'],
58                'priority' => '1.00',
59            ];
60        }
61
62        $pages = $this->customPage->getAllPages();
63
64        foreach ($pages as $page) {
65            if ($page['active'] !== 'y') {
66                continue;
67            }
68
69            $urls[] = [
70                'loc' => $this->configuration->getDefaultUrl() . 'page/' . (string) $page['slug'] . '.html',
71                'lastmod' => (string) ($page['updated'] ?? $page['created']),
72                'priority' => '0.80',
73            ];
74        }
75
76        return $urls;
77    }
78
79    /**
80     * Generates the sitemap XML content.
81     *
82     * @throws Exception|\Exception
83     * @return string|null Returns XML content or null if sitemap is disabled
84     */
85    public function generateXml(): ?string
86    {
87        if (!$this->isEnabled()) {
88            return null;
89        }
90
91        $urls = $this->collectUrls();
92
93        $twigWrapper = new TwigWrapper(
94            (string) PMF_ROOT_DIR . '/assets/templates',
95            false,
96            $this->configuration->getTemplateSet(),
97        );
98
99        $template = $twigWrapper->loadTemplate('./sitemap.xml.twig');
100
101        return $template->render(['urls' => $urls]);
102    }
103}