Passed
Push — master ( 0ba1ce...e1cfa8 )
by Razon
02:25
created

M190725110154Setting   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 58
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A safeUp() 0 13 2
A safeDown() 0 7 2
A getStorages() 0 16 6
1
<?php
2
namespace App\Setting\Storage\Db\Migration;
3
4
use App\Db\Migration;
5
use App\Setting\Storage\Db\Storage;
6
use Yii;
7
use App\Setting\Manager;
8
use yii\base\InvalidConfigException;
9
10
/**
11
 * Class M190725110154Setting
12
 */
13
class M190725110154Setting extends Migration
14
{
15
    /**
16
     * @var Storage[] Targets to create log table for
17
     */
18
    private $storages = [];
19
20
    /**
21
     * @throws InvalidConfigException
22
     * @return Storage[]
23
     */
24
    protected function getStorages()
25
    {
26
        if ($this->storages === []) {
27
            foreach (Yii::$app->getComponents() as $component) {
28
                $component = Yii::createObject($component);
29
                if ($component instanceof Manager && $component->storage instanceof Storage) {
30
                    $this->storages[] = $component->storage;
31
                }
32
            }
33
34
            if ($this->storages === []) {
35
                throw new InvalidConfigException('You should configure setting manager component to use one or more database storages before executing this migration.');
36
            }
37
        }
38
39
        return $this->storages;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function safeUp()
46
    {
47
        foreach ($this->getStorages() as $storage) {
48
            $this->createTable($storage->settingTable, [
49
                'id' => $this->string(32)->notNull()->comment('ID'),
50
                'description' => $this->string()->notNull()->comment('description'),
51
                'value' => $this->text()->notNull()->comment('Value'),
52
                'rules' => $this->text()->notNull()->comment('Validate Rules'),
53
                'is_secret' => $this->tinyInteger()->notNull()->defaultValue(0)->comment('Is Secret'),
54
                'create_time' => $this->createTimestamp(),
55
                'update_time' => $this->updateTimestamp(),
56
                'PRIMARY KEY ([[id]])',
57
            ], $this->tableOptions());
58
        }
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function safeDown()
65
    {
66
        foreach ($this->getStorages() as $storage) {
67
            $this->dropTable($storage->settingTable);
68
        }
69
70
        return true;
71
    }
72
}
73