Lines 80.00% 8 / 10
Functions and Methods 0.00% 0 / 1
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ConfigFileReader 80.00% 8 / 10 0.00% 0 / 1 5.20 0.00% 0 / 1
 readYamlMap 80.00% 8 / 10 0.00% 0 / 1 5.20
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Config;
6
7use Stewart\Runtime\Exception\ConfigurationError;
8use Stewart\Runtime\Exception\ConfigurationException;
9use Symfony\Component\Yaml\Exception\ParseException;
10use Symfony\Component\Yaml\Yaml;
11
12final readonly class ConfigFileReader
13{
14    /**
15     * @return array<array-key, mixed>
16     * @throws ConfigurationException
17     */
18    public function readYamlMap(string $path): array
19    {
20        if (!is_file($path)) {
21            throw ConfigurationException::createForReason(ConfigurationError::ConfigFileMissing, path: $path);
22        }
23
24        try {
25            $parsed = Yaml::parseFile($path);
26        } catch (ParseException $e) {
27            throw ConfigurationException::createForReason(ConfigurationError::ConfigFileUnparsable, $e, path: $path);
28        }
29
30        if ($parsed === null) {
31            return [];
32        }
33
34        if (!\is_array($parsed)) {
35            throw ConfigurationException::createForReason(ConfigurationError::ConfigFileNotAMap, path: $path, actualType: get_debug_type($parsed));
36        }
37
38        return $parsed;
39    }
40}