BaseUserRelationModel::init()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 7
nc 4
nop 0
crap 3
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license https://vistart.name/license/
11
 */
12
13
namespace vistart\Models\models;
14
15
use vistart\Models\queries\BaseUserRelationQuery;
16
use vistart\Models\traits\UserRelationTrait;
17
18
/**
19
 * This abstract class helps you build user relation model.
20
 * If you want to use group feature, the BaseUserRelationGroup's extended class
21
 * must be used coordinately.
22
 * Basic usage:
23
 * - Implementation:
24
 * ~~~php
25
 * class SingleRelation extends BaseUserRelationModel {
26
 *     public $relationType = 0; // 0 represents single relation, 1 represents mutual relation. 1 is default.
27
 *     public $multiBlamesAttribute = 'groups'; // you should redeclare this property if needed. 'blames' is default.
28
 *
29
 *     public function init()
30
 *     {
31
 *         $this->multiBlamesClass = UserRelationGroup::className(); // if you need relation group feature, `$multiBlamesClass` property should be specified.
32
 *         parent::init(); // parent's call should be placed at the end of init() method.
33
 *     }
34
 * }
35
 * ~~~
36
 * or:
37
 * ~~~php
38
 * class Relation extends BaseUserRelationModel {
39
 *     public $multiBlamesAttribute = 'groups'; // you should redeclare this property if needed. 'blames' is default.
40
 *
41
 *     public function init()
42
 *     {
43
 *         $this->multiBlamesClass = UserRelationGroup::className(); // if you need relation group feature, `$multiBlamesClass` property should be specified.
44
 *         parent::init(); // parent's call should be placed at the end of init() method.
45
 *     }
46
 * }
47
 * ~~~
48
 * Then you can implememt your own `tableName()` for the name of specified table,
49
 * and `find()` for convenient of IDE.
50
 * - Instantiation:
51
 * ~~~php
52
 * $relation = $user->findOneOrCreate(SingleRelation, ['other_guid' => $other->guid]);
53
 * ~~~
54
 * The above is same as the following:
55
 * ~~~php
56
 * $relation = SingleRelation::buildRelation($user, $other);
57
 * ~~~
58
 * @version 2.0
59
 * @author vistart <[email protected]>
60
 */
61
abstract class BaseUserRelationModel extends BaseBlameableModel
62
{
63
    use UserRelationTrait;
64
65
    /**
66
     * @var false|string this model will not need to be confirmed any more.
67
     * If you consider it required, please redeclare it by yourself.
68
     */
69
    public $confirmationAttribute = false;
70
71
    /**
72
     * @var false|string this model will not record content any more.
73
     * If you consider it required, please redeclare it by yourself.
74
     */
75
    public $contentAttribute = false;
76
77
    /**
78
     * @var false|string this model will not record updater when being updated.
79
     * If you consider it required, please redeclare it by yourself.
80
     */
81
    public $updatedByAttribute = false;
82
83
    /**
84
     * This method will assign the `$queryClass` property if it is not string.
85
     */
86 10
    public function init()
87
    {
88 10
        if (!is_string($this->queryClass)) {
89 10
            $this->queryClass = BaseUserRelationQuery::className();
90 10
        }
91 10
        if ($this->skipInit) {
92 10
            return;
93
        }
94 10
        $this->initUserRelationEvents();
95 10
        parent::init();
96 10
    }
97
}
98