Completed
Push — master ( a39ac3...b1b969 )
by Igor
02:50 queued 01:10
created

Config::configurationEnvFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 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