• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / typo3-warming / 19009676364

02 Nov 2025 08:26AM UTC coverage: 92.98% (+1.3%) from 91.643%
19009676364

push

github

eliashaeussler
[TASK] Automatically rebuild frontend assets

1735 of 1866 relevant lines covered (92.98%)

12.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.92
/Classes/Controller/FetchSitesController.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "warming".
7
 *
8
 * Copyright (C) 2021-2025 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\Typo3Warming\Controller;
25

26
use EliasHaeussler\CacheWarmup;
27
use EliasHaeussler\Typo3SitemapLocator;
28
use EliasHaeussler\Typo3Warming\Configuration;
29
use EliasHaeussler\Typo3Warming\Domain;
30
use EliasHaeussler\Typo3Warming\Http;
31
use EliasHaeussler\Typo3Warming\Utility;
32
use EliasHaeussler\Typo3Warming\ValueObject;
33
use Psr\Http\Message;
34
use Symfony\Component\DependencyInjection;
35
use TYPO3\CMS\Backend;
36
use TYPO3\CMS\Core;
37

38
/**
39
 * FetchSitesController
40
 *
41
 * @author Elias Häußler <elias@haeussler.dev>
42
 * @license GPL-2.0-or-later
43
 */
44
#[DependencyInjection\Attribute\Autoconfigure(public: true)]
45
final readonly class FetchSitesController
46
{
47
    public function __construct(
13✔
48
        private Configuration\Configuration $configuration,
49
        private CacheWarmup\Crawler\Strategy\CrawlingStrategyFactory $crawlingStrategyFactory,
50
        private Core\Imaging\IconFactory $iconFactory,
51
        private Http\Message\Request\RequestOptions $requestOptions,
52
        private Http\Message\ResponseFactory $responseFactory,
53
        private Domain\Repository\SiteRepository $siteRepository,
54
        private Domain\Repository\SiteLanguageRepository $siteLanguageRepository,
55
        private Typo3SitemapLocator\Sitemap\SitemapLocator $sitemapLocator,
56
    ) {}
13✔
57

58
    /**
59
     * @throws Typo3SitemapLocator\Exception\BaseUrlIsNotSupported
60
     * @throws Typo3SitemapLocator\Exception\SitemapIsMissing
61
     */
62
    public function __invoke(Message\ServerRequestInterface $request): Message\ResponseInterface
13✔
63
    {
64
        $limitToSite = $request->getQueryParams()['limitToSite'] ?? null;
13✔
65
        $crawlingStrategy = $this->configuration->crawlingStrategy;
13✔
66
        $siteGroups = [];
13✔
67

68
        if (\is_string($limitToSite) && $limitToSite !== '') {
13✔
69
            $site = $this->siteRepository->findOneByIdentifier($limitToSite);
2✔
70

71
            if ($site !== null) {
2✔
72
                $sites = [$site];
2✔
73
            } else {
74
                $sites = [];
×
75
            }
76
        } else {
77
            $sites = $this->siteRepository->findAll();
12✔
78
        }
79

80
        foreach ($sites as $site) {
13✔
81
            $row = Backend\Utility\BackendUtility::getRecord('pages', $site->getRootPageId(), '*', ' AND hidden = 0');
13✔
82

83
            if (!\is_array($row)) {
13✔
84
                continue;
85
            }
86

87
            $siteGroup = $this->createSiteGroup($site, $row);
13✔
88

89
            if ($siteGroup !== null) {
13✔
90
                $siteGroups[] = $siteGroup;
13✔
91
            }
92
        }
93

94
        return $this->responseFactory->htmlTemplate('Modal/SitesModal', [
13✔
95
            'siteGroups' => $siteGroups,
13✔
96
            'userAgent' => $this->requestOptions->getUserAgent(),
13✔
97
            'configuration' => [
13✔
98
                'limit' => $this->configuration->limit,
13✔
99
                'strategy' => $crawlingStrategy !== null ? $crawlingStrategy::getName() : null,
13✔
100
            ],
13✔
101
            'availableStrategies' => $this->crawlingStrategyFactory->getAll(),
13✔
102
            'isAdmin' => Utility\BackendUtility::getBackendUser()->isAdmin(),
13✔
103
            'isFiltered' => $limitToSite !== null,
13✔
104
        ]);
13✔
105
    }
106

107
    /**
108
     * @param array<string, mixed> $page
109
     * @throws Typo3SitemapLocator\Exception\BaseUrlIsNotSupported
110
     * @throws Typo3SitemapLocator\Exception\SitemapIsMissing
111
     */
112
    private function createSiteGroup(Core\Site\Entity\Site $site, array $page): ?ValueObject\Modal\SiteGroup
13✔
113
    {
114
        $items = [];
13✔
115

116
        // Check all available languages for possible sitemaps
117
        foreach ($this->sitemapLocator->locateAllBySite($site) as $sitemaps) {
13✔
118
            foreach ($sitemaps as $sitemap) {
13✔
119
                $siteLanguage = $this->siteLanguageRepository->findOneByLanguageId(
13✔
120
                    $sitemap->getSite(),
13✔
121
                    $sitemap->getSiteLanguage()->getLanguageId(),
13✔
122
                );
13✔
123

124
                // Skip sitemap if site language is inaccessible
125
                if ($siteLanguage === null) {
13✔
126
                    continue;
127
                }
128

129
                // Check if sitemap exists
130
                if ($this->sitemapLocator->isValidSitemap($sitemap)) {
13✔
131
                    $url = (string)$sitemap->getUri();
13✔
132
                } else {
133
                    $url = null;
9✔
134
                }
135

136
                $items[] = new ValueObject\Modal\SiteGroupItem(
13✔
137
                    $siteLanguage,
13✔
138
                    $siteLanguage === $site->getDefaultLanguage(),
13✔
139
                    $url,
13✔
140
                );
13✔
141
            }
142
        }
143

144
        // Early return if no languages are available
145
        if ($items === []) {
13✔
146
            return null;
×
147
        }
148

149
        return new ValueObject\Modal\SiteGroup(
13✔
150
            $site,
13✔
151
            $this->resolvePageTitle($site, $page),
13✔
152
            $this->iconFactory->getIconForRecord('pages', $page)->getIdentifier(),
13✔
153
            $items,
13✔
154
        );
13✔
155
    }
156

157
    /**
158
     * @param array<string, mixed> $page
159
     */
160
    private function resolvePageTitle(Core\Site\Entity\Site $site, array $page): string
13✔
161
    {
162
        $websiteTitle = $site->getConfiguration()['websiteTitle'] ?? null;
13✔
163

164
        if (\is_string($websiteTitle) && trim($websiteTitle) !== '') {
13✔
165
            return $websiteTitle;
×
166
        }
167

168
        return Backend\Utility\BackendUtility::getRecordTitle('pages', $page);
13✔
169
    }
170
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc