Test Failed
Push — master ( 13dd6b...7f7a95 )
by vistart
03:43
created

Member::getMemberRoleRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\organization;
14
15
use rhosocial\base\models\models\BaseBlameableModel;
16
use rhosocial\user\User;
17
use rhosocial\organization\queries\OrganizationQuery;
18
use rhosocial\organization\queries\MemberQuery;
19
use Yii;
20
21
/**
22
 * Organization member.
23
 *
24
 * @property string $organization_guid
25
 * @property string $user_guid store guid of user who represents this member.
26
 * @property string $nickname
27
 * @property string $role
28
 * @property string $description
29
 * 
30
 * @property string $department_guid
31
 * @property string $member_guid
32
 * @property User $memberUser
33
 *
34
 * @version 1.0
35
 * @author vistart <[email protected]>
36
 */
37
class Member extends BaseBlameableModel
38
{
39
    public $createdByAttribute = 'organization_guid';
40
    public $updatedByAttribute = false;
41
    public $hostClass = Organization::class;
42
43
    public $memberAttribute = 'user_guid';
44
    public $memberUserClass = User::class;
45
    private $noInitMemberUser;
46
    /**
47
     * @return User
48
     */
49
    protected function getNoInitMemberUser()
50
    {
51
        if (!$this->noInitMemberUser) {
52
            $class = $this->memberUserClass;
53
            $this->noInitMemberUser = $class::buildNoInitModel();
54
        }
55
        return $this->noInitMemberUser;
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function init()
62
    {
63
        if (!is_string($this->queryClass)) {
64
            $this->queryClass = MemberQuery::class;
65
        }
66
        if ($this->skipInit) {
67
            return;
68
        }
69
        parent::init();
70
    }
71
72
    public $descriptionAttribute = 'description';
73
74
    public function getMemberUserRules()
75
    {
76
        return [
77
            [$this->memberAttribute, 'required'],
78
            [$this->memberAttribute, 'string', 'max' => 36],
79
        ];
80
    }
81
82
    public function getMemberRoleRules()
83
    {
84
        return [
85
            ['role', 'string', 'max' => 255],
86
        ];
87
    }
88
89
    /**
90
     * Set member user.
91
     * @param User|string|integer $user
92
     */
93
    public function setMemberUser($user)
94
    {
95
        $class = $this->memberUserClass;
96
        if (is_int($user)) {
97
            $user = $class::find()->id($user)->one();
98
        }
99
        if ($user instanceof $class) {
100
            $user = $user->getGUID();
101
        }
102
        $this->user_guid = $user;
103
    }
104
105
    public function getMemberUser()
106
    {
107
        return $this->hasOne($this->memberUserClass, [$this->memberAttribute => $this->getNoInitMemberUser()->guidAttribute]);
108
    }
109
110
    /**
111
     * Get Organization Query.
112
     * Alias of `getHost` method.
113
     * @return OrganizationQuery
114
     */
115
    public function getOrganization()
116
    {
117
        return $this->getHost();
118
    }
119
120
    /**
121
     * Set Organization.
122
     * @param BaseOrganization $organization
123
     * @return boolean
124
     */
125
    public function setOrganization($organization)
126
    {
127
        return $this->setHost($organization);
128
    }
129
130
    public function rules()
131
    {
132
        return array_merge($this->getMemberUserRules(), $this->getMemberRoleRules(), parent::rules());
133
    }
134
135
    /**
136
     * @inheritdoc
137
     */
138
    public static function tableName()
139
    {
140
        return '{{%organization_member}}';
141
    }
142
}
143