Completed
Push — master ( 573b33...6d07d9 )
by Tom
11s
created

YAMLFileReader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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