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

eliashaeussler / typo3-warming / 11907268323

19 Nov 2024 06:08AM UTC coverage: 91.341%. Remained the same
11907268323

Pull #750

github

github-actions[bot]
[TASK] Automatically rebuild frontend assets
Pull Request #750: [TASK] Update rollup to v4.27.3

1097 of 1201 relevant lines covered (91.34%)

8.36 hits per line

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

93.62
/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-2024 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\Typo3SitemapLocator;
27
use EliasHaeussler\Typo3Warming\Configuration;
28
use EliasHaeussler\Typo3Warming\Crawler;
29
use EliasHaeussler\Typo3Warming\Http;
30
use EliasHaeussler\Typo3Warming\Utility;
31
use EliasHaeussler\Typo3Warming\ValueObject;
32
use Psr\Http\Message;
33
use Symfony\Component\DependencyInjection;
34
use TYPO3\CMS\Backend;
35
use TYPO3\CMS\Core;
36

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

55
    /**
56
     * @throws Typo3SitemapLocator\Exception\BaseUrlIsNotSupported
57
     * @throws Typo3SitemapLocator\Exception\SitemapIsMissing
58
     */
59
    public function __invoke(): Message\ResponseInterface
6✔
60
    {
61
        $siteGroups = [];
6✔
62

63
        foreach (array_filter($this->siteFinder->getAllSites(), Utility\AccessUtility::canWarmupCacheOfSite(...)) as $site) {
6✔
64
            $row = Backend\Utility\BackendUtility::getRecord('pages', $site->getRootPageId(), '*', ' AND hidden = 0');
6✔
65

66
            if (!\is_array($row)) {
6✔
67
                continue;
×
68
            }
69

70
            $siteGroup = $this->createSiteGroup($site, $row);
6✔
71

72
            if ($siteGroup !== null) {
6✔
73
                $siteGroups[] = $siteGroup;
6✔
74
            }
75
        }
76

77
        return $this->responseFactory->htmlTemplate('Modal/SitesModal', [
6✔
78
            'siteGroups' => $siteGroups,
6✔
79
            'userAgent' => $this->configuration->getUserAgent(),
6✔
80
            'configuration' => [
6✔
81
                'limit' => $this->configuration->getLimit(),
6✔
82
                'strategy' => $this->configuration->getStrategy(),
6✔
83
            ],
6✔
84
            'availableStrategies' => array_keys($this->crawlingStrategyFactory->getAll()),
6✔
85
            'isAdmin' => Utility\BackendUtility::getBackendUser()->isAdmin(),
6✔
86
        ]);
6✔
87
    }
88

89
    /**
90
     * @param array<string, mixed> $page
91
     * @throws Typo3SitemapLocator\Exception\BaseUrlIsNotSupported
92
     * @throws Typo3SitemapLocator\Exception\SitemapIsMissing
93
     */
94
    private function createSiteGroup(Core\Site\Entity\Site $site, array $page): ?ValueObject\Modal\SiteGroup
6✔
95
    {
96
        $items = [];
6✔
97

98
        // Check all available languages for possible sitemaps
99
        foreach ($this->sitemapLocator->locateAllBySite($site) as $i => $sitemaps) {
6✔
100
            foreach ($sitemaps as $sitemap) {
6✔
101
                $siteLanguage = $sitemap->getSiteLanguage();
6✔
102
                $url = null;
6✔
103

104
                // Check if sitemap exists
105
                if ($this->sitemapLocator->isValidSitemap($sitemap)) {
6✔
106
                    $url = (string)$sitemap->getUri();
6✔
107
                }
108

109
                $items[] = new ValueObject\Modal\SiteGroupItem(
6✔
110
                    $siteLanguage,
6✔
111
                    $siteLanguage === $site->getDefaultLanguage(),
6✔
112
                    $url,
6✔
113
                );
6✔
114
            }
115
        }
116

117
        // Early return if no languages are available
118
        if ($items === []) {
6✔
119
            return null;
×
120
        }
121

122
        return new ValueObject\Modal\SiteGroup(
6✔
123
            $site,
6✔
124
            $this->resolvePageTitle($site, $page),
6✔
125
            $this->iconFactory->getIconForRecord('pages', $page)->getIdentifier(),
6✔
126
            $items,
6✔
127
        );
6✔
128
    }
129

130
    /**
131
     * @param array<string, mixed> $page
132
     */
133
    private function resolvePageTitle(Core\Site\Entity\Site $site, array $page): string
6✔
134
    {
135
        $websiteTitle = $site->getConfiguration()['websiteTitle'] ?? null;
6✔
136

137
        if (\is_string($websiteTitle) && trim($websiteTitle) !== '') {
6✔
138
            return $websiteTitle;
×
139
        }
140

141
        return Backend\Utility\BackendUtility::getRecordTitle('pages', $page);
6✔
142
    }
143
}
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

© 2025 Coveralls, Inc