BaseUserModel   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 1 Features 0
Metric Value
wmc 3
c 8
b 1
f 0
lcom 1
cbo 3
dl 0
loc 29
ccs 14
cts 14
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A init() 0 16 3
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link http://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license http://vistart.name/license/
11
 */
12
13
namespace vistart\Models\models;
14
15
use vistart\Models\queries\BaseUserQuery;
16
use vistart\Models\traits\UserTrait;
17
use yii\web\IdentityInterface;
18
19
/**
20
 * The abstract BaseUserModel is used for user identity class.
21
 * For example, you should create a table for user model before you want to
22
 * define a User class used for representing a user. Then, you can use base
23
 * user model generator to generate a new user model, like following:
24
 * ~~~php
25
 * * @property string $guid
26
 * class User extends \vistart\Models\models\BaseUserModel {
27
 *     public static function tableName() {
28
 *         return <table_name>;
29
 *     }
30
 *     public static function attributeLabels() {
31
 *         return [
32
 *             <All labels.>
33
 *         ];
34
 *     }
35
 * }
36
 * ~~~
37
 *
38
 * Well, if you want to register a new user, you should create a new user
39
 * instance, and prepare attributes for it. then call the `register()` method.
40
 * like following:
41
 * ~~~php
42
 * $user = new User(['password' => '123456']);
43
 * $user->register();
44
 * ~~~
45
 * If there is not only one user instance to be stored in database, but also
46
 * other associated models, such as Profile class, should be stored
47
 * synchronously, you can prepare their models and give them to parameter of
48
 * `register()` method, like following:
49
 * ~~~php
50
 * $profile = new Profile();
51
 * $user->register([$profile]);
52
 * ~~~
53
 * Note: you should supplement `get<ModelName>()` method(s) by yourself, or by
54
 * generator.
55
 * @see vistart\Models\models\BaseEntityModel
56
 * @version 2.0
57
 * @author vistart <[email protected]>
58
 */
59
abstract class BaseUserModel extends BaseEntityModel implements IdentityInterface
60
{
61
    use UserTrait;
62
63
    /**
64
     * Initialize user model.
65
     * This procedure will append events used for initialization of `status` and
66
     * `source` attributes.
67
     * When `$skipInit` is assigned to `false`, the above processes will be skipped.
68
     * If you want to modify or override this method, you should add `parent::init()`
69
     * statement at the end of your init() method.
70
     */
71 60
    public function init()
72
    {
73 60
        if (!is_string($this->queryClass)) {
74 60
            $this->queryClass = BaseUserQuery::className();
75 60
        }
76 60
        if ($this->skipInit) {
77 60
            return;
78
        }
79 60
        $this->on(self::$eventNewRecordCreated, [$this, 'onInitStatusAttribute']);
80 60
        $this->on(self::$eventNewRecordCreated, [$this, 'onInitSourceAttribute']);
81 60
        $this->on(self::$eventNewRecordCreated, [$this, 'onInitAuthKey']);
82 60
        $this->on(self::$eventNewRecordCreated, [$this, 'onInitAccessToken']);
83 60
        $this->on(self::$eventNewRecordCreated, [$this, 'onInitPasswordResetToken']);
84 60
        $this->on(self::$eventAfterSetPassword, [$this, 'onAfterSetNewPassword']);
85 60
        parent::init();
86 60
    }
87
}
88