Lines 100.00% 4 / 4
Functions and Methods 100.00% 1 / 1
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
SecretFileReader 100.00% 4 / 4 100.00% 1 / 1 4 100.00% 1 / 1
 readSecretFile 100.00% 4 / 4 100.00% 1 / 1 4
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Config;
6
7use Stewart\Runtime\Exception\ConfigurationError;
8use Stewart\Runtime\Exception\ConfigurationException;
9
10final readonly class SecretFileReader
11{
12    private const string TRAILING_LINE_BREAK = '/\r?\n\z/';
13
14    /** @throws ConfigurationException */
15    public function readSecretFile(string $variable, string $path): string
16    {
17        $contents = is_file($path) && is_readable($path) ? file_get_contents($path) : false;
18
19        if ($contents === false) {
20            throw ConfigurationException::createForReason(ConfigurationError::EnvironmentSecretFileUnreadable, variable: $variable, path: $path);
21        }
22
23        return preg_replace(self::TRAILING_LINE_BREAK, '', $contents, 1) ?? $contents;
24    }
25}