Passed
Push — master ( 4164b7...fac48b )
by Stavros
23:01 queued 07:51
created

Config::loadConfigFile()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 18
rs 9.6111
1
<?php
2
3
4
namespace Stadem\VivaPayments\Config;
5
6
class Config
7
{
8
    private $settings;
9
    private $config;
10
    private $logger;
11
12
    public function __construct($configKey = null)
13
    {
14
        
15
        // $this->settings = require dirname(__DIR__, 2) . '/viva-config.php';
16
        
17
        $paths = [
18
            dirname(__DIR__, 2) . '/viva-config.php',
19
            dirname(__DIR__, 5) . '/viva-config.php'];
20
21
        $this->settings = $this->loadConfigFile($paths);
22
23
        
24
25
        if ($configKey) {
26
            $this->config = $this->get($configKey);
27
        } else {
28
            $this->config = $this->settings;
29
        }
30
    }
31
32
    public function get($key)
33
    {
34
        return $this->settings[$key] ?? null;
35
    }
36
37
    public function getEnvConfig($key)
38
    {
39
        return $this->config[$key] ?? null;
40
    }
41
42
43
    private function loadConfigFile(array $paths)
44
    {
45
        foreach ($paths as $path) {
46
            if (file_exists($path)) {
47
                if ($this->logger) {
48
                    $this->logger->info("The file is found on: $path");
49
                }
50
                return require $path;
51
            }
52
        }
53
54
     
55
        $message = 'The file viva-config.php was not found in any of the specified paths.';
56
57
        if ($this->logger) {
58
            $this->logger->error($message, ['paths' => $paths]);
59
        }
60
        throw new \RuntimeException($message);
61
    }
62
}
63