Completed
Pull Request — master (#52)
by Tom
04:13
created

PHPFileReader::read()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace TomPHP\ContainerConfigurator\FileReader;
4
5
use TomPHP\ContainerConfigurator\Exception\FileNotFoundException;
6
use TomPHP\ContainerConfigurator\Exception\InvalidConfigException;
7
8
final class PHPFileReader implements FileReader
9
{
10
    private $filename;
11
12
    public function read($filename)
13
    {
14
        $this->filename = $filename;
15
16
        $this->assertFileExists();
17
18
        $config = include $this->filename;
19
20
        $this->assertConfigIsValid($config);
21
22
        return $config;
23
    }
24
25
    private function assertFileExists()
26
    {
27
        if (!file_exists($this->filename)) {
28
            throw FileNotFoundException::fromFileName($this->filename);
29
        }
30
    }
31
32
    private function assertConfigIsValid($config)
33
    {
34
        if (!is_array($config)) {
35
            throw InvalidConfigException::fromPHPFileError($this->filename);
36
        }
37
    }
38
}
39