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

eliashaeussler / cache-warmup / 8010698128

22 Feb 2024 08:38PM UTC coverage: 79.13% (-18.5%) from 97.674%
8010698128

Pull #333

github

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

41 of 303 new or added lines in 12 files covered. (13.53%)

8 existing lines in 1 file now uncovered.

1092 of 1380 relevant lines covered (79.13%)

6.91 hits per line

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

0.0
/src/Config/Adapter/FileConfigAdapter.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\Adapter;
25

26
use CuyZ\Valinor;
27
use EliasHaeussler\CacheWarmup\Config;
28
use EliasHaeussler\CacheWarmup\Exception;
29
use EliasHaeussler\CacheWarmup\Sitemap;
30
use SplFileObject;
31
use Symfony\Component\Yaml;
32

33
use function file_exists;
34
use function pathinfo;
35

36
/**
37
 * FileConfigAdapter.
38
 *
39
 * @author Elias Häußler <elias@haeussler.dev>
40
 * @license GPL-3.0-or-later
41
 */
42
final class FileConfigAdapter implements ConfigAdapter
43
{
44
    private readonly Valinor\Mapper\TreeMapper $mapper;
45

NEW
46
    public function __construct(
×
47
        private readonly string $file,
48
    ) {
NEW
49
        $this->mapper = $this->createMapper();
×
50
    }
51

52
    /**
53
     * @throws Exception\InvalidConfigFileException
54
     * @throws Exception\MissingConfigFileException
55
     */
NEW
56
    public function get(): Config\CacheWarmupConfig
×
57
    {
NEW
58
        if (!file_exists($this->file)) {
×
NEW
59
            throw Exception\MissingConfigFileException::create($this->file);
×
60
        }
61

NEW
62
        $source = match (pathinfo($this->file, PATHINFO_EXTENSION)) {
×
NEW
63
            'yaml', 'yml' => Valinor\Mapper\Source\Source::iterable(Yaml\Yaml::parseFile($this->file)),
×
NEW
64
            default => Valinor\Mapper\Source\Source::file(new SplFileObject($this->file)),
×
NEW
65
        };
×
66

67
        try {
NEW
68
            return $this->mapper->map(Config\CacheWarmupConfig::class, $source);
×
NEW
69
        } catch (Valinor\Mapper\MappingError $error) {
×
NEW
70
            throw Exception\InvalidConfigFileException::create($this->file, $error);
×
71
        }
72
    }
73

NEW
74
    private function createMapper(): Valinor\Mapper\TreeMapper
×
75
    {
NEW
76
        return (new Valinor\MapperBuilder())
×
NEW
77
            ->registerConstructor(
×
NEW
78
                Sitemap\Sitemap::createFromString(...),
×
NEW
79
            )
×
NEW
80
            ->allowPermissiveTypes()
×
NEW
81
            ->mapper()
×
NEW
82
        ;
×
83
    }
84
}
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