Config   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 23
c 4
b 0
f 0
dl 0
loc 56
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A loadConfigFile() 0 18 5
A getConfig() 0 2 1
A getEnvConfig() 0 4 1
A __construct() 0 14 2
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