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

eliashaeussler / cache-warmup / 8036670583

25 Feb 2024 08:29AM UTC coverage: 77.824% (-19.9%) from 97.674%
8036670583

Pull #333

github

web-flow
Merge 6a385c811 into b43777e8b
Pull Request #333: [FEATURE] Introduce Config API

79 of 375 new or added lines in 17 files covered. (21.07%)

3 existing lines in 1 file now uncovered.

1130 of 1452 relevant lines covered (77.82%)

7.84 hits per line

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

0.0
/src/Config/CacheWarmupConfig.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "eliashaeussler/cache-warmup".
7
 *
8
 * Copyright (C) 2020-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 3 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\CacheWarmup\Config;
25

26
use EliasHaeussler\CacheWarmup\Crawler;
27
use EliasHaeussler\CacheWarmup\Exception;
28
use EliasHaeussler\CacheWarmup\Formatter;
29
use EliasHaeussler\CacheWarmup\Helper;
30
use EliasHaeussler\CacheWarmup\Sitemap;
31
use Psr\Log;
32
use ReflectionClass;
33

34
use function array_key_exists;
35
use function get_object_vars;
36
use function in_array;
37
use function is_string;
38
use function max;
39
use function preg_match;
40
use function str_starts_with;
41

42
/**
43
 * CacheWarmupConfig.
44
 *
45
 * @author Elias Häußler <elias@haeussler.dev>
46
 * @license GPL-3.0-or-later
47
 */
48
final class CacheWarmupConfig
49
{
50
    /**
51
     * @param list<Sitemap\Sitemap>                                                $sitemaps
52
     * @param list<Sitemap\Url>                                                    $urls
53
     * @param list<string>                                                         $excludePatterns
54
     * @param int<0, max>                                                          $limit
55
     * @param Crawler\CrawlerInterface|class-string<Crawler\CrawlerInterface>|null $crawler
56
     * @param array<string, mixed>                                                 $crawlerOptions
57
     * @param int<0, max>                                                          $repeatAfter
58
     */
NEW
59
    public function __construct(
×
60
        private array $sitemaps = [],
61
        private array $urls = [],
62
        private array $excludePatterns = [],
63
        private int $limit = 0,
64
        private bool $progress = false,
65
        private Crawler\CrawlerInterface|string|null $crawler = null,
66
        private array $crawlerOptions = [],
67
        private Crawler\Strategy\CrawlingStrategy|string|null $strategy = null,
68
        private string $format = 'text',
69
        private ?string $logFile = null,
70
        private string $logLevel = Log\LogLevel::ERROR,
71
        private bool $allowFailures = false,
72
        private bool $stopOnFailure = false,
73
        private int $repeatAfter = 0,
NEW
74
    ) {}
×
75

76
    /**
77
     * @return list<Sitemap\Sitemap>
78
     */
NEW
79
    public function getSitemaps(): array
×
80
    {
NEW
81
        return $this->sitemaps;
×
82
    }
83

84
    /**
85
     * @param list<Sitemap\Sitemap> $sitemaps
86
     */
NEW
87
    public function setSitemaps(array $sitemaps): self
×
88
    {
NEW
89
        $this->sitemaps = $sitemaps;
×
90

NEW
91
        return $this;
×
92
    }
93

NEW
94
    public function addSitemap(Sitemap\Sitemap|string $sitemap): self
×
95
    {
NEW
96
        if (is_string($sitemap)) {
×
NEW
97
            $sitemap = Sitemap\Sitemap::createFromString($sitemap);
×
98
        }
99

NEW
100
        $this->sitemaps[] = $sitemap;
×
101

NEW
102
        return $this;
×
103
    }
104

105
    /**
106
     * @return list<Sitemap\Url>
107
     */
NEW
108
    public function getUrls(): array
×
109
    {
NEW
110
        return $this->urls;
×
111
    }
112

113
    /**
114
     * @param list<Sitemap\Url> $urls
115
     */
NEW
116
    public function setUrls(array $urls): self
×
117
    {
NEW
118
        $this->urls = $urls;
×
119

NEW
120
        return $this;
×
121
    }
122

NEW
123
    public function addUrl(Sitemap\Url|string $url): self
×
124
    {
NEW
125
        if (is_string($url)) {
×
NEW
126
            $url = new Sitemap\Url($url);
×
127
        }
128

NEW
129
        $this->urls[] = $url;
×
130

NEW
131
        return $this;
×
132
    }
133

134
    /**
135
     * @return list<string>
136
     */
NEW
137
    public function getExcludePatterns(): array
×
138
    {
NEW
139
        return $this->excludePatterns;
×
140
    }
141

142
    /**
143
     * @param list<string> $excludePatterns
144
     */
NEW
145
    public function setExcludePatterns(array $excludePatterns): self
×
146
    {
NEW
147
        $this->excludePatterns = $excludePatterns;
×
148

NEW
149
        return $this;
×
150
    }
151

NEW
152
    public function addExcludePattern(string $excludePattern): self
×
153
    {
NEW
154
        if (!in_array($excludePattern, $this->excludePatterns, true)) {
×
NEW
155
            $this->excludePatterns[] = $excludePattern;
×
156
        }
157

NEW
158
        return $this;
×
159
    }
160

161
    /**
162
     * @throws Exception\InvalidRegularExpressionException
163
     */
NEW
164
    public function addRegularExpressionExcludePattern(string $regex): self
×
165
    {
NEW
166
        if (!str_starts_with($regex, '#')) {
×
NEW
167
            throw Exception\InvalidRegularExpressionException::create($regex);
×
168
        }
169

NEW
170
        if (false === preg_match($regex, '')) {
×
NEW
171
            throw Exception\InvalidRegularExpressionException::create($regex);
×
172
        }
173

NEW
174
        return $this->addExcludePattern($regex);
×
175
    }
176

177
    /**
178
     * @return non-negative-int
179
     */
NEW
180
    public function getLimit(): int
×
181
    {
NEW
182
        return $this->limit;
×
183
    }
184

185
    /**
186
     * @param non-negative-int $limit
187
     */
NEW
188
    public function setLimit(int $limit): self
×
189
    {
NEW
190
        $this->limit = max($limit, 0);
×
191

NEW
192
        return $this;
×
193
    }
194

NEW
195
    public function disableLimit(): self
×
196
    {
NEW
197
        $this->limit = 0;
×
198

NEW
199
        return $this;
×
200
    }
201

NEW
202
    public function isProgressBarEnabled(): bool
×
203
    {
NEW
204
        return $this->progress;
×
205
    }
206

NEW
207
    public function enableProgressBar(): self
×
208
    {
NEW
209
        $this->progress = true;
×
210

NEW
211
        return $this;
×
212
    }
213

NEW
214
    public function disableProgressBar(): self
×
215
    {
NEW
216
        $this->progress = false;
×
217

NEW
218
        return $this;
×
219
    }
220

221
    /**
222
     * @return Crawler\CrawlerInterface|class-string<Crawler\CrawlerInterface>|null
223
     */
NEW
224
    public function getCrawler(): Crawler\CrawlerInterface|string|null
×
225
    {
NEW
226
        return $this->crawler;
×
227
    }
228

229
    /**
230
     * @param Crawler\CrawlerInterface|class-string<Crawler\CrawlerInterface> $crawler
231
     */
NEW
232
    public function setCrawler(Crawler\CrawlerInterface|string $crawler): self
×
233
    {
NEW
234
        $this->crawler = $crawler;
×
235

NEW
236
        return $this;
×
237
    }
238

239
    /**
240
     * @return array<string, mixed>
241
     */
NEW
242
    public function getCrawlerOptions(): array
×
243
    {
NEW
244
        return $this->crawlerOptions;
×
245
    }
246

247
    /**
248
     * @param array<string, mixed> $crawlerOptions
249
     */
NEW
250
    public function setCrawlerOptions(array $crawlerOptions): self
×
251
    {
NEW
252
        $this->crawlerOptions = $crawlerOptions;
×
253

NEW
254
        return $this;
×
255
    }
256

NEW
257
    public function setCrawlerOption(string $name, mixed $value): self
×
258
    {
NEW
259
        $this->crawlerOptions[$name] = $value;
×
260

NEW
261
        return $this;
×
262
    }
263

NEW
264
    public function removeCrawlerOption(string $name): self
×
265
    {
NEW
266
        unset($this->crawlerOptions[$name]);
×
267

NEW
268
        return $this;
×
269
    }
270

NEW
271
    public function getStrategy(): Crawler\Strategy\CrawlingStrategy|string|null
×
272
    {
NEW
273
        return $this->strategy;
×
274
    }
275

NEW
276
    public function setStrategy(Crawler\Strategy\CrawlingStrategy|string $strategy): self
×
277
    {
NEW
278
        $this->strategy = $strategy;
×
279

NEW
280
        return $this;
×
281
    }
282

NEW
283
    public function getFormat(): string
×
284
    {
NEW
285
        return $this->format;
×
286
    }
287

NEW
288
    public function setFormat(string $format): self
×
289
    {
NEW
290
        $this->format = $format;
×
291

NEW
292
        return $this;
×
293
    }
294

NEW
295
    public function useJsonFormat(): self
×
296
    {
NEW
297
        return $this->setFormat(Formatter\JsonFormatter::getType());
×
298
    }
299

NEW
300
    public function useTextFormat(): self
×
301
    {
NEW
302
        return $this->setFormat(Formatter\TextFormatter::getType());
×
303
    }
304

NEW
305
    public function getLogFile(): ?string
×
306
    {
NEW
307
        return $this->logFile;
×
308
    }
309

NEW
310
    public function setLogFile(string $logFile): self
×
311
    {
NEW
312
        $this->logFile = $logFile;
×
313

NEW
314
        return $this;
×
315
    }
316

NEW
317
    public function getLogLevel(): string
×
318
    {
NEW
319
        return $this->logLevel;
×
320
    }
321

NEW
322
    public function setLogLevel(string $logLevel): self
×
323
    {
NEW
324
        $this->logLevel = $logLevel;
×
325

NEW
326
        return $this;
×
327
    }
328

NEW
329
    public function areFailuresAllowed(): bool
×
330
    {
NEW
331
        return $this->allowFailures;
×
332
    }
333

NEW
334
    public function allowFailures(): self
×
335
    {
NEW
336
        $this->allowFailures = true;
×
337

NEW
338
        return $this;
×
339
    }
340

NEW
341
    public function disallowFailures(): self
×
342
    {
NEW
343
        $this->allowFailures = false;
×
344

NEW
345
        return $this;
×
346
    }
347

NEW
348
    public function shouldStopOnFailure(): bool
×
349
    {
NEW
350
        return $this->stopOnFailure;
×
351
    }
352

NEW
353
    public function stopOnFailure(): self
×
354
    {
NEW
355
        $this->stopOnFailure = true;
×
356

NEW
357
        return $this;
×
358
    }
359

NEW
360
    public function dontStopOnFailure(): self
×
361
    {
NEW
362
        $this->stopOnFailure = false;
×
363

NEW
364
        return $this;
×
365
    }
366

367
    /**
368
     * @return non-negative-int
369
     */
NEW
370
    public function getRepeatAfter(): int
×
371
    {
NEW
372
        return $this->repeatAfter;
×
373
    }
374

375
    /**
376
     * @param non-negative-int $seconds
377
     */
NEW
378
    public function repeatAfter(int $seconds): self
×
379
    {
NEW
380
        $this->repeatAfter = max($seconds, 0);
×
381

NEW
382
        return $this;
×
383
    }
384

NEW
385
    public function merge(self $other): self
×
386
    {
NEW
387
        $parameters = $this->toArray(true);
×
388

NEW
389
        Helper\ArrayHelper::mergeRecursive($parameters, $other->toArray(true));
×
390

NEW
391
        foreach ($parameters as $name => $value) {
×
NEW
392
            $this->$name = $value;
×
393
        }
394

NEW
395
        return $this;
×
396
    }
397

398
    /**
399
     * @return array{
400
     *     sitemaps?: list<Sitemap\Sitemap>,
401
     *     urls?: list<Sitemap\Url>,
402
     *     excludePatterns?: list<string>,
403
     *     limit?: non-negative-int,
404
     *     progress?: bool,
405
     *     crawler?: Crawler\CrawlerInterface|class-string<Crawler\CrawlerInterface>|null,
406
     *     crawlerOptions?: array<string, mixed>,
407
     *     strategy?: Crawler\Strategy\CrawlingStrategy|string|null,
408
     *     format?: string,
409
     *     logFile?: string|null,
410
     *     logLevel?: string,
411
     *     allowFailures?: bool,
412
     *     stopOnFailure?: bool,
413
     *     repeatAfter?: non-negative-int,
414
     * }
415
     */
NEW
416
    public function toArray(bool $omitDefaultValues = false): array
×
417
    {
NEW
418
        $config = get_object_vars($this);
×
419

NEW
420
        if (!$omitDefaultValues) {
×
NEW
421
            return $config;
×
422
        }
423

NEW
424
        $reflection = new ReflectionClass(self::class);
×
NEW
425
        $parameters = $reflection->getConstructor()?->getParameters() ?? [];
×
426

NEW
427
        foreach ($parameters as $parameter) {
×
NEW
428
            $name = $parameter->getName();
×
429

NEW
430
            if (!$parameter->isOptional()) {
×
NEW
431
                continue;
×
432
            }
433

NEW
434
            if (array_key_exists($name, $config) && $config[$name] === $parameter->getDefaultValue()) {
×
NEW
435
                unset($config[$name]);
×
436
            }
437
        }
438

NEW
439
        return $config;
×
440
    }
441
}
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