Completed
Push — master ( 6ff8ac...850b68 )
by Tom
86:44 queued 66:31
created

PHPFileReader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

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\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