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

PHPFileReader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 31
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 12 1
A assertFileExists() 0 6 2
A assertConfigIsValid() 0 6 2
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