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

PHPFileReader::assertFileExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
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