M180121215558Users   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 27
dl 0
loc 47
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A safeUp() 0 18 1
A init() 0 6 1
A safeDown() 0 3 1
1
<?php
2
namespace console\migrations;
3
4
use Yii;
5
use DateTime;
6
use yii\db\Migration;
7
use yii\base\Security;
8
use common\models\User as UserModel;
9
use console\migrations\M180121204747Init as InitMigration;
10
11
class M180121215558Users extends Migration
12
{
13
    const DEFAULT_USER_NAME = 'admin';
14
15
    const DEFAULT_USER_PASS = '123';
16
17
    /** @var string */
18
    protected $tableName;
19
20
    /** @var Security */
21
    protected $security;
22
23
    /** @var DateTime */
24
    protected $now;
25
26
    public function init()
27
    {
28
        parent::init();
29
        $this->security = Yii::$app->security;
30
        $this->now = new DateTime();
31
        $this->tableName = UserModel::tableName();
32
    }
33
34
    public function safeUp()
35
    {
36
        $this->createTable($this->tableName, [
37
            'id' => $this->primaryKey(),
38
            'username' => $this->string()->notNull()->unique(),
39
            'auth_key' => $this->string(32)->notNull(),
40
            'password_hash' => $this->string()->notNull(),
41
            'status' => $this->smallInteger()->notNull()->defaultValue(10),
42
            'created_at' => $this->integer()->notNull(),
43
            'updated_at' => $this->integer()->notNull(),
44
        ], InitMigration::TABLE_OPTION);
45
        $this->insert($this->tableName, [
46
            'username' => self::DEFAULT_USER_NAME,
47
            'password_hash' => $this->security->generatePasswordHash(self::DEFAULT_USER_PASS),
48
            'status' => UserModel::STATUS_ACTIVE,
49
            'auth_key' => $this->security->generateRandomString(),
50
            'created_at' => $this->now->getTimestamp(),
51
            'updated_at' => $this->now->getTimestamp(),
52
        ]);
53
    }
54
55
    public function safeDown()
56
    {
57
        $this->dropTable($this->tableName);
58
    }
59
}
60