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

eliashaeussler / cache-warmup / 8011192704

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

Pull #333

github

web-flow
Merge f455f7e16 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/Adapter/EnvironmentVariablesConfigAdapter.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

30
use function array_filter;
31
use function array_map;
32
use function explode;
33
use function getenv;
34
use function gettype;
35
use function in_array;
36
use function preg_replace;
37
use function strtoupper;
38

39
/**
40
 * EnvironmentVariablesConfigAdapter.
41
 *
42
 * @author Elias Häußler <elias@haeussler.dev>
43
 * @license GPL-3.0-or-later
44
 */
45
final class EnvironmentVariablesConfigAdapter implements ConfigAdapter
46
{
47
    private const ENV_VAR_PREFIX = 'CACHE_WARMUP_';
48
    private const BOOLEAN_VALUES = [
49
        'true',
50
        'yes',
51
        '1',
52
    ];
53
    private readonly Valinor\Mapper\TreeMapper $mapper;
54

NEW
55
    public function __construct()
×
56
    {
NEW
57
        $this->mapper = (new ConfigMapperFactory())->get();
×
58
    }
59

60
    /**
61
     * @throws Exception\InvalidEnvironmentVariablesException
62
     */
NEW
63
    public function get(): Config\CacheWarmupConfig
×
64
    {
NEW
65
        $config = new Config\CacheWarmupConfig();
×
NEW
66
        $configOptions = $config->toArray();
×
67

NEW
68
        $resolvedVariables = [];
×
NEW
69
        $nameMapping = [];
×
70

NEW
71
        foreach ($configOptions as $name => $defaultValue) {
×
NEW
72
            $envVarName = $this->resolveName($name);
×
NEW
73
            $envVarValue = getenv($envVarName);
×
74

75
            // Skip non-existent env variables
NEW
76
            if (false === $envVarValue) {
×
NEW
77
                continue;
×
78
            }
79

NEW
80
            $resolvedVariables[$name] = $this->processValue($envVarValue, $defaultValue);
×
NEW
81
            $nameMapping[$name] = $envVarName;
×
82
        }
83

84
        try {
NEW
85
            return $this->mapper->map(
×
NEW
86
                Config\CacheWarmupConfig::class,
×
NEW
87
                Valinor\Mapper\Source\Source::array($resolvedVariables),
×
NEW
88
            );
×
NEW
89
        } catch (Valinor\Mapper\MappingError $error) {
×
NEW
90
            throw Exception\InvalidEnvironmentVariablesException::create($error, $nameMapping);
×
91
        }
92
    }
93

NEW
94
    private function resolveName(string $propertyName): string
×
95
    {
NEW
96
        return self::ENV_VAR_PREFIX.strtoupper((string) preg_replace('/[[:upper:]]/', '_$1', $propertyName));
×
97
    }
98

NEW
99
    private function processValue(string $envVarValue, mixed $defaultValue): mixed
×
100
    {
NEW
101
        return match (gettype($defaultValue)) {
×
NEW
102
            'array' => array_filter(
×
NEW
103
                array_map(
×
NEW
104
                    'trim',
×
NEW
105
                    explode(',', $envVarValue),
×
NEW
106
                ),
×
NEW
107
            ),
×
NEW
108
            'boolean' => in_array($envVarValue, self::BOOLEAN_VALUES, true),
×
NEW
109
            default => $envVarValue,
×
NEW
110
        };
×
111
    }
112
}
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

© 2024 Coveralls, Inc