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

eliashaeussler / typo3-warming / 9887257374

11 Jul 2024 07:25AM UTC coverage: 91.224%. Remained the same
9887257374

Pull #671

github

eliashaeussler
[BUGFIX] Require lodash-es npm package
Pull Request #671: [TASK] Update stylelint-header to v2

1081 of 1185 relevant lines covered (91.22%)

8.21 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 TYPO3\CMS\Backend;
34
use TYPO3\CMS\Core;
35

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

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

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

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

68
            $siteGroup = $this->createSiteGroup($site, $row);
6✔
69

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

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

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

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

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

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

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

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

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

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

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