Completed
Push — master ( 52a46f...98e19a )
by Petrică
02:29
created

ConfigLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Petrica
5
 * Date: 3/26/2016
6
 * Time: 1:23
7
 */
8
namespace Petrica\StatsdSystem\Config;
9
10
use Petrica\StatsdSystem\Config\Definition\ConfigDefinition;
11
use Symfony\Component\Config\Definition\Processor;
12
use Symfony\Component\Yaml\Parser;
13
14
class ConfigLoader
15
{
16
    /**
17
     * Filepath to Yaml configuration file
18
     * @var String
19
     */
20
    private $filepath;
21
22
    /**
23
     * ConfigLoader constructor.
24
     * @param $filepath
25
     */
26 1
    public function __construct($filepath)
27
    {
28 1
        $this->filepath = $filepath;
29 1
    }
30
31
    /**
32
     * Process configuration and make sure the configuration format is as expected
33
     *
34
     * @return array
35
     */
36 1
    public function load()
37
    {
38 1
        if (file_exists($this->filepath) && ($contents = file_get_contents($this->filepath))) {
39 1
            $yaml = new Parser();
40 1
            $config = $yaml->parse($contents);
41
42 1
            if (null === $config) {
43
                $config = array();
44
            }
45
46 1
            $processor = new Processor();
47 1
            $configDefinition = new ConfigDefinition();
48 1
            $processedConfiguration = $processor->processConfiguration(
49 1
                $configDefinition,
50
                $config
51 1
            );
52
53 1
            if (empty($processedConfiguration)) {
54
                throw new Exception(
55
                    'You need to specify at least one gaguge in the configuration file'
56
                );
57
            }
58
59 1
            return $processedConfiguration;
60
        }
61
        else {
62
            throw new \RuntimeException(sprintf('Configuration file does not exist or is not accessible %s',
63
                $this->filepath));
64
        }
65
66
    }
67
}
68