Passed
Push — master ( bf8e81...016379 )
by Vsevolods
06:13
created

ConfigFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 24
ccs 9
cts 15
cp 0.6
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B createFromFile() 0 16 5
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Config;
4
5
use Venta\Config\Parser\Json;
6
use Venta\Contracts\Config\Config as ConfigContract;
7
use Venta\Contracts\Config\ConfigFactory as ConfigFactoryContract;
8
9
/**
10
 * Class ConfigFactory
11
 *
12
 * @package Venta\Config
13
 */
14
class ConfigFactory implements ConfigFactoryContract
15
{
16
17
    /**
18
     * @inheritDoc
19
     */
20 4
    public function createFromFile($filename): ConfigContract
21
    {
22 4
        if (!is_file($filename) || !is_readable($filename)) {
23 1
            throw new \InvalidArgumentException(sprintf('File "%s" does not exist or is not readable', $filename));
24
        }
25
26 3
        $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
27
        switch ($extension) {
28 3
            case 'php':
29 1
                return new Config(include $filename);
30 2
            case 'json':
31 1
                return (new Json())->parse(file_get_contents($filename));
32
            default:
33 1
                throw new \RuntimeException(sprintf('Unknown config format "%s"', $extension));
34
        }
35
    }
36
37
}