Completed
Push — master ( 80fd60...6beedd )
by Igor
01:49
created

CreateLocalConfigController::getSettings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
3
namespace app\commands;
4
5
use Yii;
6
use yii\base\InvalidConfigException;
7
use yii\console\{Controller, ExitCode};
8
use yii\helpers\{Console, ArrayHelper};
9
10
/**
11
 * Command for create local config
12
 */
13
class CreateLocalConfigController extends Controller
14
{
15
    /**
16
     * @var string MySQL host
17
     */
18
    public $MYSQL_HOST;
19
    /**
20 1
     * @var string MySQL database
21
     */
22 1
    public $MYSQL_DATABASE;
23
    /**
24
     * @var string MySQL user
25 2
     */
26
    public $MYSQL_USER;
27 2
    /**
28 2
     * @var string MySQL password
29 1
     */
30
    public $MYSQL_PASSWORD;
31
    /**
32 1
     * @var string Config file path
33
     */
34
    public $path;
35 1
36
    public function options($actionId = '')
37 1
    {
38 1
        return [
39
            'path', 
40 1
            'MYSQL_HOST',
41 1
            'MYSQL_DATABASE',
42 1
            'MYSQL_USER',
43
            'MYSQL_PASSWORD',
44
        ];
45
    }
46 1
47
    public function beforeAction($action)
48
    {
49
        if (parent::beforeAction($action)) {
50
            if (empty($this->path)) {
51
                throw new InvalidConfigException('`path` should be specified');
52
            }
53
        }
54
        return true;
55
    }
56
57
    public function actionIndex()
58
    {
59
        $source = Yii::getAlias('@app/config/local/main.dist');
60
        $dist = Yii::getAlias($this->path);
61
62
        if (!file_exists($dist)) {
63
            copy($source, $dist);
64
            $this->specifySettings($dist);
0 ignored issues
show
Bug introduced by
It seems like $dist defined by \Yii::getAlias($this->path) on line 60 can also be of type boolean; however, app\commands\CreateLocal...ller::specifySettings() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
65
66
            $this->stdout("Created successfully!\n", Console::FG_GREEN);
67
        } else {
68
            $this->stdout("Config file is exist!\n", Console::FG_RED);
69
        }
70
71
        return ExitCode::OK;
72
    }
73
74
    private function specifySettings(string $file)
75
    {
76
        $contents = file_get_contents($file);
77
        $settings = $this->getSettings();
78
79
        foreach ($settings as $var => $value) {
80
            $contents = str_replace('%' . $var  . '%', $value, $contents);
81
        }
82
83
        file_put_contents($file, $contents);
84
    }
85
86
    private function getSettings()
87
    {
88
        $envFile = Yii::getAlias('@app/.env');
89
        if (file_exists($envFile)) {
90
            return parse_ini_file($envFile);
91
        }
92
93
        return $this->getOptionValues('');
94
    }
95
}
96