Config::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
cc 1
eloc 3
c 4
b 1
f 1
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
namespace Sovereign\Lib;
3
use Monolog\Logger;
4
5
/**
6
 * Class Config
7
 * @package Sovereign\Lib
8
 */
9
class Config
10
{
11
    /**
12
     * @var mixed
13
     */
14
    private $config;
15
16
    /**
17
     * @var Logger
18
     */
19
    private $logger;
20
21
    /**
22
     * Config constructor.
23
     * @param $configFile
24
     * @param Logger $logger
25
     */
26
    public function __construct($configFile, Logger $logger) {
27
        $this->logger = $logger;
28
        $this->loadFile($configFile);
29
    }
30
31
    /**
32
     * @param $configFile
33
     */
34
    public function loadFile ($configFile) {
35
36
        if (!file_exists(realpath($configFile))) {
37
            $this->logger->addError('Config file '.realpath($configFile).' not found.');
38
            return;
39
        }
40
41
        try {
42
            $this->config = array_change_key_case(include($configFile), \CASE_LOWER);
43
            $this->logger->addDebug('Config file loaded: '.realpath($configFile));
44
        } catch (\Exception $e) {
45
            $this->logger->addError('Failed loading config file ('.realpath($configFile).'): '.$e->getMessage());
46
        }
47
    }
48
49
    /**
50
     * @param string $key
51
     * @param string|null $type
52
     * @param string|null $default
53
     * @return null|string
54
     */
55
    public function get($key, $type = null, $default = null)
56
    {
57
        $type = strtolower($type);
58
        if (!empty($this->config[$type][$key])) {
59
            return $this->config[$type][$key];
60
        }
61
62
        $this->logger->addWarning('Config setting not found: ['.$type.']['.$key.']');
63
64
        return $default;
65
    }
66
67
    /**
68
     * @param string|null $type
69
     * @return array
70
     */
71
    public function getAll($type = null)
72
    {
73
        $type = strtolower($type);
74
        if (!empty($this->config[$type])) {
75
            return $this->config[$type];
76
        }
77
78
        $this->logger->addWarning('Config group not found: ['.$type.']');
79
80
        return array();
81
    }
82
}
83