Completed
Push — master ( 234d72...5d41c2 )
by Novikov
01:19
created

EDbFixtureManager::actionLoad()   C

Complexity

Conditions 14
Paths 42

Size

Total Lines 57
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 6.5728
c 0
b 0
f 0
cc 14
eloc 33
nc 42
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * EDbFixtureManager represent the console command which helps you to manage your basic fixtures.
5
 * Available command properties:
6
 *    pathToFixtures - path to your fixtures file
7
 *    modelsFolder   - path to folder where your models classes lay, default to `application.models.*`
8
 *
9
 * Note:
10
 * 1) All attributes you want to fill via fixtures data, must be defined with `safe` validation rule (`rules()` method);
11
 * 2) Don't forget configure `tablePrefix` option for `db` connection definition;
12
 * 3) Your tables will be purged when you loading fixtures;
13
 *
14
 * For more complex info about usage, see README.md
15
 */
16
class EDbFixtureManager extends CConsoleCommand
17
{
18
    public $pathToFixtures;
19
    public $modelsFolder;
20
21
    /**
22
     * Load fixtures into database from fixtures file
23
     */
24
    public function actionLoad()
25
    {
26
        echo "\033[36m Are you sure you want to load fixtures? Your database will be purged! [Y/N] \033[0m";
27
28
        $phpStdin     = fopen("php://stdin", "r");
29
        $inputValue   = fgets($phpStdin);
30
        $purifiedLine = preg_replace('/[^A-Za-z0-9\-]/', '', $inputValue);
31
32
        if (strtolower($purifiedLine) == 'n') {
33
            echo "\033[34m Stopping the executing... Done. \033[0m \n";
34
            die;
35
        }
36
37
        if (empty($this->pathToFixtures) || !file_exists($this->pathToFixtures)) {
38
            echo "\033[33m There is no file with fixtures to load! Make sure that you create file with fixtures,
39
                  or pass correct file name \033[0m \n";
40
            die;
41
        }
42
43
        // import models classes to make available create new instances
44
        if (is_array($this->modelsFolder)) {
45
            foreach ($this->modelsFolder as $folder) {
46
                Yii::import($folder); //import each folder form array
47
            }
48
        } else {
49
            Yii::import(empty($this->modelsFolder) ? 'application.models.*' : $this->modelsFolder);
50
        }
51
52
        $fixtures  = require_once $this->pathToFixtures; // require that file with fixtures, will be array
53
        $errorList = array();
54
55
        foreach ($fixtures as $modelClass => $instances) { // run through the array with fixtures
56
            $modelClass::model()->deleteAll(); // removing old rows from database if database is not truncated
57
            foreach ($instances as $key => $instance) { // go through all instances for certain model, and save it into db
58
                $model = new $modelClass();
59
                $model->attributes = $instances[$key];
60
                if (!$model->save()) { // if model can't be saved append errors into error list array
61
                    $errorList[] = $model->getErrors();
62
                }
63
            }
64
        }
65
66
        if (!empty($errorList)) { // if error list not empty
67
            echo "\033[31m Validation errors occurs during loading the fixtures, some fixtures wasn't loaded to database \033[0m  \n
68
                    \033[33m  The next errors occur \033[0m \n";
69
            foreach ($errorList as $errors) { // run over all errors and display error what occur during saving into db
70
                foreach ($errors as $error) {
71
                    foreach ($error as $value) {
72
                        echo "\033[37;41m" . $value . "\033[0m   \n"; //display error
73
                    }
74
                }
75
            }
76
            die;
77
        }
78
79
        echo "\033[37;42m All fixtures loaded properly \033[0m \n";
80
    }
81
82
    /**
83
     * Show a some info about `fixtures` command
84
     *
85
     * @return string
86
     */
87
    public function getHelp()
88
    {
89
        $output = "\033[34m This command will allow you to manage your fixtures in a simple way.
90
 Be careful all rows from database will be removed! \033[0m \n\n";
91
92
        return $output . parent::getHelp();
93
    }
94
}
95