Completed
Push — master ( 6ff8ac...850b68 )
by Tom
86:44 queued 66:31
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\ConfigServiceProvider\FileReader;
4
5
use TomPHP\ConfigServiceProvider\Exception\FileNotFoundException;
6
use TomPHP\ConfigServiceProvider\Exception\InvalidConfigException;
7
use TomPHP\ConfigServiceProvider\FileReader\FileReader;
8
9
final class PHPFileReader implements FileReader
10
{
11
    private $filename;
12
13
    public function read($filename)
14
    {
15
        $this->filename = $filename;
16
17
        $this->assertFileExists();
18
19
        $config = include $this->filename;
20
21
        $this->assertConfigIsValid($config);
22
23
        return $config;
24
    }
25
26
    private function assertFileExists()
27
    {
28
        if (!file_exists($this->filename)) {
29
            throw FileNotFoundException::fromFileName($this->filename);
30
        }
31
    }
32
33
    private function assertConfigIsValid($config)
34
    {
35
        if (!is_array($config)) {
36
            throw InvalidConfigException::fromPHPFileError($this->filename);
37
        }
38
    }
39
}
40