Completed
Push — master ( 26336c...0097a9 )
by Vadim
03:19
created

m160219_091156_createUser::up()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 8.5906
cc 5
eloc 16
nc 3
nop 0
1
<?php
2
3
use yii\db\Migration;
4
5
class m160219_091156_createUser extends Migration
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
    public function up()
0 ignored issues
show
Coding Style introduced by
up uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
8
    {
9
        if(isset($_SERVER['TRAVIS'])||(isset($_SERVER['PWD'])&&strpos($_SERVER['PWD'], '/tests/')))
10
            return null;
11
12
        $userName = 'webmaster';
13
14
        $tableName = \dektrium\user\models\User::tableName();
15
        $query = 'SELECT COUNT(*) FROM '.$tableName.' WHERE `username`=:username';
16
        $count = \Yii::$app->db->createCommand($query, [':username'=>$userName])->queryScalar();
17
        if($count>0)
18
            return null;
19
20
        $user = \Yii::createObject([
21
            'class'    => \dektrium\user\models\User::className(),
22
            'scenario' => 'create',
23
            'username' => $userName,
24
            'password' => $userName,
25
            'email' => $userName.'@yii2enterprise.dev',
26
        ]);
27
28
        return $user->create();
29
    }
30
31
    public function down()
32
    {
33
        echo "m160219_091156_createUser cannot be reverted.\n";
34
35
        return false;
36
    }
37
38
    /*
39
    // Use safeUp/safeDown to run migration code within a transaction
40
    public function safeUp()
41
    {
42
    }
43
44
    public function safeDown()
45
    {
46
    }
47
    */
48
}
49