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

eliashaeussler / typo3-warming / 15615316958

12 Jun 2025 03:52PM UTC coverage: 91.686% (+0.4%) from 91.289%
15615316958

push

github

eliashaeussler
[TASK] Remove outdated `@throws` annotations

1610 of 1756 relevant lines covered (91.69%)

12.0 hits per line

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

98.67
/Classes/Http/Message/Event/WarmupFinishedEvent.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\Http\Message\Event;
25

26
use EliasHaeussler\CacheWarmup;
27
use EliasHaeussler\SSE;
28
use EliasHaeussler\Typo3Warming\Configuration;
29
use EliasHaeussler\Typo3Warming\Enums;
30
use EliasHaeussler\Typo3Warming\Exception;
31
use EliasHaeussler\Typo3Warming\Result;
32
use EliasHaeussler\Typo3Warming\ValueObject;
33
use TYPO3\CMS\Backend;
34

35
/**
36
 * WarmupFinishedEvent
37
 *
38
 * @author Elias Häußler <elias@haeussler.dev>
39
 * @license GPL-2.0-or-later
40
 * @internal
41
 */
42
final readonly class WarmupFinishedEvent implements SSE\Event\Event
43
{
44
    public function __construct(
20✔
45
        private ValueObject\Request\WarmupRequest $request,
46
        private Result\CacheWarmupResult $result,
47
    ) {}
20✔
48

49
    public function getName(): string
9✔
50
    {
51
        return 'warmupFinished';
9✔
52
    }
53

54
    /**
55
     * @return array{
56
     *     state: string,
57
     *     title: string|null,
58
     *     progress: array{
59
     *         current: int,
60
     *         total: int,
61
     *     },
62
     *     results: array{
63
     *         failed: list<array{url: string, data: array<string, mixed>}>,
64
     *         successful: list<array{url: string, data: array<string, mixed>}>,
65
     *     },
66
     *     excluded: array{
67
     *         sitemaps: list<string>,
68
     *         urls: list<string>,
69
     *     },
70
     *     messages: array<string>,
71
     * }
72
     * @throws Exception\MissingPageIdException
73
     */
74
    public function getData(): array
20✔
75
    {
76
        $state = $this->determineWarmupState();
20✔
77

78
        $failedUrls = $this->result->getResult()->getFailed();
20✔
79
        $successfulUrls = $this->result->getResult()->getSuccessful();
20✔
80

81
        return [
20✔
82
            'state' => $state->value,
20✔
83
            'title' => Configuration\Localization::translate('notification.title.' . $state->value),
20✔
84
            'progress' => [
20✔
85
                'current' => \count($failedUrls) + \count($successfulUrls),
20✔
86
                'total' => \count($failedUrls) + \count($successfulUrls),
20✔
87
            ],
20✔
88
            'results' => [
20✔
89
                'failed' => array_map($this->decorateResult(...), $failedUrls),
20✔
90
                'successful' => array_map($this->decorateResult(...), $successfulUrls),
20✔
91
            ],
20✔
92
            'excluded' => [
20✔
93
                'sitemaps' => array_map(strval(...), $this->result->getExcludedSitemaps()),
20✔
94
                'urls' => array_map(strval(...), $this->result->getExcludedUrls()),
20✔
95
            ],
20✔
96
            'messages' => $this->buildMessages($state),
20✔
97
        ];
20✔
98
    }
99

100
    /**
101
     * @return array<string, mixed>
102
     * @throws Exception\MissingPageIdException
103
     */
104
    public function jsonSerialize(): array
10✔
105
    {
106
        return $this->getData();
10✔
107
    }
108

109
    private function determineWarmupState(): Enums\WarmupState
20✔
110
    {
111
        $failed = \count($this->result->getResult()->getFailed());
20✔
112
        $successful = \count($this->result->getResult()->getSuccessful());
20✔
113

114
        if ($failed > 0 && $successful === 0) {
20✔
115
            return Enums\WarmupState::Failed;
10✔
116
        }
117

118
        if ($failed > 0 && $successful > 0) {
10✔
119
            return Enums\WarmupState::Warning;
3✔
120
        }
121

122
        if ($failed === 0) {
7✔
123
            return Enums\WarmupState::Success;
7✔
124
        }
125

126
        return Enums\WarmupState::Unknown;
×
127
    }
128

129
    /**
130
     * @return array{url: string, data: array<string, mixed>}
131
     */
132
    private function decorateResult(CacheWarmup\Result\CrawlingResult $result): array
14✔
133
    {
134
        return [
14✔
135
            'url' => (string)$result->getUri(),
14✔
136
            'data' => $result->getData(),
14✔
137
        ];
14✔
138
    }
139

140
    /**
141
     * @return array<string>
142
     * @throws Exception\MissingPageIdException
143
     */
144
    private function buildMessages(Enums\WarmupState $state): array
20✔
145
    {
146
        $messages = [];
20✔
147
        $emptyMessage = Configuration\Localization::translate('notification.message.empty');
20✔
148

149
        foreach ($this->request->getSites() as $siteWarmupRequest) {
20✔
150
            foreach ($siteWarmupRequest->getLanguageIds() as $languageId) {
17✔
151
                $site = $siteWarmupRequest->getSite();
17✔
152
                $siteLanguage = $site->getLanguageById($languageId);
17✔
153

154
                ['successful' => $successful, 'failed' => $failed] = $this->result->getCrawlingResultsBySite(
17✔
155
                    $site,
17✔
156
                    $siteLanguage,
17✔
157
                );
17✔
158

159
                $messages[] = Configuration\Localization::translate('notification.message.site', [
17✔
160
                    $this->getPageTitle($site->getRootPageId()),
17✔
161
                    $site->getRootPageId(),
17✔
162
                    $siteLanguage->getTitle(),
17✔
163
                    $languageId,
17✔
164
                    \count($successful),
17✔
165
                    \count($failed),
17✔
166
                ]);
17✔
167
            }
168
        }
169

170
        foreach ($this->request->getPages() as $pageWarmupRequest) {
20✔
171
            $messages[] = Configuration\Localization::translate('notification.message.page.' . $state->value, [
11✔
172
                $this->getPageTitle($pageWarmupRequest->getPage()),
11✔
173
                $pageWarmupRequest->getPage(),
11✔
174
            ]);
11✔
175
        }
176

177
        // Remove invalid messages
178
        $messages = array_filter($messages, static fn(string $message) => \trim($message) !== '');
19✔
179

180
        // Handle no cache warmup
181
        if ($messages === []) {
19✔
182
            $messages[] = $emptyMessage;
2✔
183
        }
184

185
        return $messages;
19✔
186
    }
187

188
    /**
189
     * @throws Exception\MissingPageIdException
190
     */
191
    private function getPageTitle(int $pageId): string
19✔
192
    {
193
        $record = Backend\Utility\BackendUtility::getRecord('pages', $pageId);
19✔
194

195
        if ($record === null) {
19✔
196
            throw Exception\MissingPageIdException::create();
1✔
197
        }
198

199
        return Backend\Utility\BackendUtility::getRecordTitle('pages', $record);
18✔
200
    }
201
}
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