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

eliashaeussler / typo3-warming / 16240124771

12 Jul 2025 04:56PM UTC coverage: 91.643% (+0.6%) from 91.04%
16240124771

push

github

eliashaeussler
[TASK] Automatically rebuild frontend assets

1634 of 1783 relevant lines covered (91.64%)

12.56 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\ResponseFactory $responseFactory,
52
        private Domain\Repository\SiteRepository $siteRepository,
53
        private Domain\Repository\SiteLanguageRepository $siteLanguageRepository,
54
        private Typo3SitemapLocator\Sitemap\SitemapLocator $sitemapLocator,
55
    ) {}
13✔
56

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

167
        return Backend\Utility\BackendUtility::getRecordTitle('pages', $page);
13✔
168
    }
169
}
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