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

eliashaeussler / typo3-warming / 10905906811

17 Sep 2024 03:10PM UTC coverage: 91.282%. Remained the same
10905906811

push

github

eliashaeussler
[RELEASE] Release of EXT:warming 2.4.1

1089 of 1193 relevant lines covered (91.28%)

8.31 hits per line

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

75.27
/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-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\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\CrawlerInterface>
61
     */
62
    public function getCrawler(): string
9✔
63
    {
64
        try {
65
            /** @var class-string<CacheWarmup\Crawler\CrawlerInterface>|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\CrawlerInterface::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
     */
85
    public function getCrawlerOptions(): array
8✔
86
    {
87
        try {
88
            $json = $this->configuration->get(Extension::KEY, 'crawlerOptions');
8✔
89

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

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

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

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

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

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

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

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

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

143
    /**
144
     * @return array<string, mixed>
145
     */
146
    public function getParserClientOptions(): array
8✔
147
    {
148
        try {
149
            $json = $this->configuration->get(Extension::KEY, 'parserClientOptions');
8✔
150

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

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

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

167
            if (!is_numeric($limit)) {
12✔
168
                return self::DEFAULT_LIMIT;
1✔
169
            }
170

171
            return abs((int)$limit);
11✔
172
        } catch (Core\Exception) {
×
173
            return self::DEFAULT_LIMIT;
×
174
        }
175
    }
176

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

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

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

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

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

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

211
            return $strategy;
1✔
212
        } catch (Core\Exception) {
×
213
            return null;
×
214
        }
215
    }
216

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

222
            return (bool)$enablePageTree;
14✔
223
        } catch (Core\Exception) {
×
224
            return true;
×
225
        }
226
    }
227

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

236
            if (!\is_string($doktypes)) {
13✔
237
                return self::DEFAULT_SUPPORTED_DOKTYPES;
1✔
238
            }
239

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

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

251
            return (bool)$enableToolbar;
25✔
252
        } catch (Core\Exception) {
×
253
            return true;
×
254
        }
255
    }
256

257
    public function getUserAgent(): string
9✔
258
    {
259
        return $this->userAgent;
9✔
260
    }
261

262
    private function generateUserAgent(): string
67✔
263
    {
264
        $string = 'TYPO3/tx_warming_crawler';
67✔
265

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

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