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

eliashaeussler / typo3-warming / 13216923178

08 Feb 2025 03:22PM UTC coverage: 91.424% (+0.08%) from 91.341%
13216923178

push

github

web-flow
[TASK] Update all dependencies

1098 of 1201 relevant lines covered (91.42%)

8.42 hits per line

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

76.34
/Classes/Configuration/Configuration.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\Configuration;
25

26
use EliasHaeussler\CacheWarmup;
27
use EliasHaeussler\Typo3Warming\Crawler;
28
use EliasHaeussler\Typo3Warming\Extension;
29
use Symfony\Component\DependencyInjection;
30
use TYPO3\CMS\Core;
31
use TYPO3\CMS\Extbase;
32

33
/**
34
 * Configuration
35
 *
36
 * @author Elias Häußler <elias@haeussler.dev>
37
 * @license GPL-2.0-or-later
38
 */
39
#[DependencyInjection\Attribute\Autoconfigure(public: true)]
40
final class Configuration
41
{
42
    private const DEFAULT_CRAWLER = Crawler\ConcurrentUserAgentCrawler::class;
43
    private const DEFAULT_VERBOSE_CRAWLER = Crawler\OutputtingUserAgentCrawler::class;
44
    private const DEFAULT_LIMIT = 250;
45
    private const DEFAULT_SUPPORTED_DOKTYPES = [
46
        Core\Domain\Repository\PageRepository::DOKTYPE_DEFAULT,
47
    ];
48

49
    private readonly string $userAgent;
50

51
    public function __construct(
67✔
52
        private readonly Core\Configuration\ExtensionConfiguration $configuration,
53
        private readonly CacheWarmup\Crawler\CrawlerFactory $crawlerFactory,
54
        private readonly Crawler\Strategy\CrawlingStrategyFactory $crawlingStrategyFactory,
55
    ) {
56
        $this->userAgent = $this->generateUserAgent();
67✔
57
    }
58

59
    /**
60
     * @return class-string<CacheWarmup\Crawler\Crawler>
61
     */
62
    public function getCrawler(): string
9✔
63
    {
64
        try {
65
            /** @var class-string<CacheWarmup\Crawler\Crawler>|null $crawler */
66
            $crawler = $this->configuration->get(Extension::KEY, 'crawler');
9✔
67

68
            if (!\is_string($crawler)) {
9✔
69
                return self::DEFAULT_CRAWLER;
1✔
70
            }
71

72
            if (!is_a($crawler, CacheWarmup\Crawler\Crawler::class, true)) {
8✔
73
                return self::DEFAULT_CRAWLER;
2✔
74
            }
75

76
            return $crawler;
6✔
77
        } catch (Core\Exception) {
×
78
            return self::DEFAULT_CRAWLER;
×
79
        }
80
    }
81

82
    /**
83
     * @return array<string, mixed>
84
     * @throws CacheWarmup\Exception\CrawlerOptionIsInvalid
85
     */
86
    public function getCrawlerOptions(): array
8✔
87
    {
88
        try {
89
            $json = $this->configuration->get(Extension::KEY, 'crawlerOptions');
8✔
90

91
            // Early return if no crawler options are configured
92
            if (!\is_string($json) || $json === '') {
8✔
93
                return [];
6✔
94
            }
95

96
            return $this->crawlerFactory->parseCrawlerOptions($json);
2✔
97
        } catch (Core\Exception) {
1✔
98
            return [];
×
99
        }
100
    }
101

102
    /**
103
     * @return class-string<CacheWarmup\Crawler\VerboseCrawler>
104
     */
105
    public function getVerboseCrawler(): string
5✔
106
    {
107
        try {
108
            /** @var class-string<CacheWarmup\Crawler\VerboseCrawler>|null $crawler */
109
            $crawler = $this->configuration->get(Extension::KEY, 'verboseCrawler');
5✔
110

111
            if (!\is_string($crawler)) {
5✔
112
                return self::DEFAULT_VERBOSE_CRAWLER;
1✔
113
            }
114

115
            if (!is_a($crawler, CacheWarmup\Crawler\VerboseCrawler::class, true)) {
4✔
116
                return self::DEFAULT_VERBOSE_CRAWLER;
2✔
117
            }
118

119
            return $crawler;
2✔
120
        } catch (Core\Exception) {
×
121
            return self::DEFAULT_VERBOSE_CRAWLER;
×
122
        }
123
    }
124

125
    /**
126
     * @return array<string, mixed>
127
     * @throws CacheWarmup\Exception\CrawlerOptionIsInvalid
128
     */
129
    public function getVerboseCrawlerOptions(): array
4✔
130
    {
131
        try {
132
            $json = $this->configuration->get(Extension::KEY, 'verboseCrawlerOptions');
4✔
133

134
            // Early return if no crawler options are configured
135
            if (!\is_string($json) || $json === '') {
4✔
136
                return [];
2✔
137
            }
138

139
            return $this->crawlerFactory->parseCrawlerOptions($json);
2✔
140
        } catch (Core\Exception) {
1✔
141
            return [];
×
142
        }
143
    }
144

145
    /**
146
     * @return array<string, mixed>
147
     * @throws CacheWarmup\Exception\CrawlerOptionIsInvalid
148
     */
149
    public function getParserClientOptions(): array
8✔
150
    {
151
        try {
152
            $json = $this->configuration->get(Extension::KEY, 'parserClientOptions');
8✔
153

154
            // Early return if no parser client options are configured
155
            if (!\is_string($json) || $json === '') {
8✔
156
                return [];
6✔
157
            }
158

159
            return $this->crawlerFactory->parseCrawlerOptions($json);
2✔
160
        } catch (Core\Exception) {
1✔
161
            return [];
×
162
        }
163
    }
164

165
    public function getLimit(): int
12✔
166
    {
167
        try {
168
            $limit = $this->configuration->get(Extension::KEY, 'limit');
12✔
169

170
            if (!is_numeric($limit)) {
12✔
171
                return self::DEFAULT_LIMIT;
1✔
172
            }
173

174
            return abs((int)$limit);
11✔
175
        } catch (Core\Exception) {
×
176
            return self::DEFAULT_LIMIT;
×
177
        }
178
    }
179

180
    /**
181
     * @return list<string>
182
     */
183
    public function getExcludePatterns(): array
8✔
184
    {
185
        try {
186
            $exclude = $this->configuration->get(Extension::KEY, 'exclude');
8✔
187

188
            // Early return if no exclude patterns are configured
189
            if (!\is_string($exclude) || $exclude === '') {
8✔
190
                return [];
7✔
191
            }
192

193
            return Core\Utility\GeneralUtility::trimExplode(',', $exclude, true);
1✔
194
        } catch (Core\Exception) {
×
195
            return [];
×
196
        }
197
    }
198

199
    public function getStrategy(): ?string
13✔
200
    {
201
        try {
202
            $strategy = $this->configuration->get(Extension::KEY, 'strategy');
13✔
203

204
            // Early return if no crawling strategy is configured
205
            if (!\is_string($strategy) || $strategy === '') {
13✔
206
                return null;
11✔
207
            }
208

209
            // Early return if configured crawling strategy is invalid
210
            if (!$this->crawlingStrategyFactory->has($strategy)) {
2✔
211
                return null;
1✔
212
            }
213

214
            return $strategy;
1✔
215
        } catch (Core\Exception) {
×
216
            return null;
×
217
        }
218
    }
219

220
    public function isEnabledInPageTree(): bool
14✔
221
    {
222
        try {
223
            $enablePageTree = $this->configuration->get(Extension::KEY, 'enablePageTree');
14✔
224

225
            return (bool)$enablePageTree;
14✔
226
        } catch (Core\Exception) {
×
227
            return true;
×
228
        }
229
    }
230

231
    /**
232
     * @return list<int>
233
     */
234
    public function getSupportedDoktypes(): array
13✔
235
    {
236
        try {
237
            $doktypes = $this->configuration->get(Extension::KEY, 'supportedDoktypes');
13✔
238

239
            if (!\is_string($doktypes)) {
13✔
240
                return self::DEFAULT_SUPPORTED_DOKTYPES;
1✔
241
            }
242

243
            return array_values(Core\Utility\GeneralUtility::intExplode(',', $doktypes, true));
12✔
244
        } catch (Core\Exception) {
×
245
            return self::DEFAULT_SUPPORTED_DOKTYPES;
×
246
        }
247
    }
248

249
    public function isEnabledInToolbar(): bool
25✔
250
    {
251
        try {
252
            $enableToolbar = $this->configuration->get(Extension::KEY, 'enableToolbar');
25✔
253

254
            return (bool)$enableToolbar;
25✔
255
        } catch (Core\Exception) {
×
256
            return true;
×
257
        }
258
    }
259

260
    public function getUserAgent(): string
9✔
261
    {
262
        return $this->userAgent;
9✔
263
    }
264

265
    private function generateUserAgent(): string
67✔
266
    {
267
        $string = 'TYPO3/tx_warming_crawler';
67✔
268

269
        if (class_exists(Core\Crypto\HashService::class)) {
67✔
270
            return Core\Utility\GeneralUtility::makeInstance(Core\Crypto\HashService::class)->appendHmac(
67✔
271
                $string,
67✔
272
                self::class,
67✔
273
            );
67✔
274
        }
275

276
        // @todo Remove once support for TYPO3 v12 is dropped
277
        return Core\Utility\GeneralUtility::makeInstance(Extbase\Security\Cryptography\HashService::class)->appendHmac(
×
278
            $string,
×
279
        );
×
280
    }
281
}
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