Passed
Push — master ( 28f242...472b4c )
by Gabriel
04:44
created

HasEnviromentsTrait::addEnviroment()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 2
dl 0
loc 19
ccs 7
cts 7
cp 1
crap 3
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\Migrations\Config\Traits;
4
5
/**
6
 * Trait HasEnviromentsTrait
7
 * @package ByTIC\Migrations\Config\Traits
8
 */
9
trait HasEnviromentsTrait
10
{
11
12
    /**
13
     * @param $name
14
     * @param $config
15
     */
16 3
    public function addEnviromentFromEnv($name = 'local', $config = [])
17
    {
18
        $env = [
19 3
            'adapter' => 'DB_ADAPTER',
20
            'host'    => 'DB_HOST',
21
            'name'    => 'DB_DATABASE',
22
            'user'    => 'DB_USERNAME',
23
            'pass'    => 'DB_PASSWORD',
24
        ];
25 3
        foreach ($env as $param => $envName) {
26 3
            if (!empty($config[$param])) {
27
                continue;
28
            }
29 3
            if (empty($_ENV[$envName])) {
30 3
                continue;
31
            }
32 3
            $config[$param] = $_ENV[$envName];
33
        }
34 3
        $this->addEnviroment($name, $config);
35 3
    }
36
37
    /**
38
     * @param $name
39
     * @param $config
40
     */
41 3
    public function addEnviroment($name, $config)
42
    {
43
        $default = [
44 3
            'adapter'   => 'mysql',
45
            'host'      => 'localhost',
46
            'name'      => '',
47
            'user'      => '',
48
            'pass'      => '',
49
            'port'      => '3306',
50
            'charset'   => 'utf8',
51
            'collation' => 'utf8_unicode_ci',
52
        ];
53 3
        foreach ($default as $param => $value) {
54 3
            if (isset($config[$param])) {
55 3
                continue;
56
            }
57 3
            $config[$param] = $value;
58
        }
59 3
        $this->params['environments'][$name] = $config;
0 ignored issues
show
Bug Best Practice introduced by
The property params does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
60
    }
61
}