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

eliashaeussler / cache-warmup / 8011103897

22 Feb 2024 09:17PM UTC coverage: 76.466% (-21.2%) from 97.674%
8011103897

Pull #333

github

web-flow
Merge ca3e396ce into 0442a6074
Pull Request #333: [FEATURE] Introduce Config API

44 of 355 new or added lines in 15 files covered. (12.39%)

6 existing lines in 1 file now uncovered.

1095 of 1432 relevant lines covered (76.47%)

6.73 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\Sitemap;
30
use Psr\Log;
31
use ReflectionClass;
32

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

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

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

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

NEW
90
        return $this;
×
91
    }
92

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

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

NEW
101
        return $this;
×
102
    }
103

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

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

NEW
119
        return $this;
×
120
    }
121

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

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

NEW
130
        return $this;
×
131
    }
132

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

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

NEW
148
        return $this;
×
149
    }
150

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

NEW
157
        return $this;
×
158
    }
159

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

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

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

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

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

NEW
191
        return $this;
×
192
    }
193

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

NEW
198
        return $this;
×
199
    }
200

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

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

NEW
210
        return $this;
×
211
    }
212

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

NEW
217
        return $this;
×
218
    }
219

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

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

NEW
235
        return $this;
×
236
    }
237

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

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

NEW
253
        return $this;
×
254
    }
255

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

NEW
260
        return $this;
×
261
    }
262

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

NEW
267
        return $this;
×
268
    }
269

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

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

NEW
279
        return $this;
×
280
    }
281

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

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

NEW
291
        return $this;
×
292
    }
293

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

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

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

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

NEW
313
        return $this;
×
314
    }
315

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

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

NEW
325
        return $this;
×
326
    }
327

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

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

NEW
337
        return $this;
×
338
    }
339

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

NEW
344
        return $this;
×
345
    }
346

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

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

NEW
356
        return $this;
×
357
    }
358

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

NEW
363
        return $this;
×
364
    }
365

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

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

NEW
381
        return $this;
×
382
    }
383

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

NEW
388
        foreach ($otherValues as $name => $value) {
×
NEW
389
            $this->$name = $value;
×
390
        }
391

NEW
392
        return $this;
×
393
    }
394

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

NEW
415
        if (!$omitDefaultValues) {
×
NEW
416
            return $config;
×
417
        }
418

NEW
419
        $reflection = new ReflectionClass(self::class);
×
NEW
420
        $parameters = $reflection->getConstructor()?->getParameters() ?? [];
×
421

NEW
422
        foreach ($parameters as $parameter) {
×
NEW
423
            $name = $parameter->getName();
×
424

NEW
425
            if (!$parameter->isOptional()) {
×
NEW
426
                continue;
×
427
            }
428

NEW
429
            if (array_key_exists($name, $config) && $config[$name] === $parameter->getDefaultValue()) {
×
NEW
430
                unset($config[$name]);
×
431
            }
432
        }
433

NEW
434
        return $config;
×
435
    }
436
}
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