Passed
Push — master ( 69c27f...aa13d6 )
by Stavros
12:52
created

Config::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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