Config   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 87
ccs 10
cts 14
cp 0.7143
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A configurationPath() 0 13 2
A get() 0 4 1
A configurationEnvFile() 0 5 1
A setConfig() 0 10 2
1
<?php
2
3
namespace guastallaigor\PhpBattlerite;
4
5
use Dotenv\Dotenv;
6
use Illuminate\Config\Repository;
7
8
/**
9
 * PHP-Battlerite easy API.
10
 *
11
 * @category  Games
12
 *
13
 * @author    Igor Guastalla de Lima  <[email protected]>
14
 * @copyright 2018 PHP Battlerite
15
 * @license   MIT https://github.com/guastallaigor/php-battlerite/blob/master/LICENSE
16
 *
17
 * @link      https://github.com/guastallaigor/php-battlerite
18
 */
19
class Config
20
{
21
    /**
22
     * Config file name.
23
     */
24
25
    private static $configFileNames = [
26
        'phpbattlerite',
27
        'apikey',
28
    ];
29
30
    /**
31
     * @var \Illuminate\Config\Repository
32
     */
33
    private $config;
34 4
35
    /**
36 4
     * Config constructor.
37
     * @param string $pathEnvFile
38 4
     * @throws ConfigFileNotFoundException
39
     */
40 4
    public function __construct($pathEnvFile = __DIR__)
41
    {
42
        $configPath = $this->configurationPath();
43
        $pathFile = $pathEnvFile;
44 4
        if ($pathEnvFile == __DIR__) {
45 4
            $pathFile = $pathEnvFile . '/../';
46
        }
47
        $this->configurationEnvFile($pathFile);
48
        foreach (self::$configFileNames as $configFileName) {
49
            $this->setConfig($configPath, $configFileName);
50
        }
51
    }
52 4
53
    /**
54
     * return the correct config directory path.
55 4
     *
56
     * @return mixed|string
57
     */
58
    private function configurationPath()
59 4
    {
60
        // the config file of the package directory
61
        $config_path = __DIR__.'/Config';
62
63 4
        // check if this laravel specific function `config_path()` exist (means this package is used inside
64
        // a laravel framework). If so then load then try to load the laravel config file if it exist.
65
        if (function_exists('config_path')) {
66
            $config_path = config_path();
67
        }
68
69
        return $config_path;
70
    }
71
72
    /**
73
     * @param $key
74
     *
75
     * @return mixed
76
     */
77
    public function get($key)
78
    {
79
        return $this->config->get($key);
80
    }
81
82
    /**
83
     * @param $pathFile
84
     */
85
    private function configurationEnvFile($pathFile)
86
    {
87
        $dotenv = new Dotenv($pathFile);
88
        $dotenv->load();
89
    }
90
91
    /**
92
     * @param $configPath
93
     * @throws ConfigFileNotFoundException
94
     */
95
    public function setConfig($configPath, $configFileName)
96
    {
97
        $config_file = $configPath . '/' . $configFileName . '.php';
98
99
        if (!file_exists($config_file)) {
100
            throw new ConfigFileNotFoundException();
101
        }
102
103
        $this->config = new Repository(require $config_file);
104
    }
105
}
106