YAMLFileReader::read()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace TomPHP\ContainerConfigurator\FileReader;
4
5
use Assert\Assertion;
6
use Symfony\Component\Yaml;
7
use TomPHP\ContainerConfigurator\Exception\InvalidConfigException;
8
use TomPHP\ContainerConfigurator\Exception\MissingDependencyException;
9
10
/**
11
 * @internal
12
 */
13
final class YAMLFileReader implements FileReader
14
{
15
    /**
16
     * @throws MissingDependencyException
17
     */
18
    public function __construct()
19
    {
20
        if (!class_exists(Yaml\Yaml::class)) {
21
            throw MissingDependencyException::fromPackageName('symfony/yaml');
22
        }
23
    }
24
25
    public function read($filename)
26
    {
27
        Assertion::file($filename);
28
29
        try {
30
            $config = Yaml\Yaml::parse(file_get_contents($filename));
31
        } catch (Yaml\Exception\ParseException $exception) {
32
            throw InvalidConfigException::fromYAMLFileError($filename, $exception->getMessage());
33
        }
34
35
        return $config;
36
    }
37
}
38