Completed
Push — master ( 42128a...09a28f )
by vistart
19:27
created

BaseOrganization::getNoInitMember()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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\traits\SelfBlameableTrait;
16
use rhosocial\base\models\queries\BaseUserQuery;
17
use rhosocial\user\User;
18
use rhosocial\organization\queries\MemberQuery;
19
use rhosocial\organization\queries\OrganizationQuery;
20
21
/**
22
 * Organization.
23
 * The organization can open sub-organizations & departments.
24
 * An organization can exist either alone or as a sub-organization of an
25
 * organization but not department.
26
 *
27
 * @property integer $type Whether indicate this instance is an organization or a department.
28
 
29
 * @version 1.0
30
 * @author vistart <[email protected]>
31
 */
32
abstract class BaseOrganization extends User
33
{
34
    use SelfBlameableTrait;
35
36
    const TYPE_ORGANIZATION = 1;
37
    const TYPE_DEPARTMENT = 2;
38
39
    /**
40
     * @var boolean Organization does not need password and corresponding features.
41
     */
42
    public $passwordHashAttribute = false;
43
44
    /**
45
     * @var boolean Organization does not need password and corresponding features.
46
     */
47
    public $passwordResetTokenAttribute = false;
48
49
    /**
50
     * @var boolean Organization does not need password and corresponding features.
51
     */
52
    public $passwordHistoryClass = false;
53
54
    /**
55
     * @var boolean Organization does not need source.
56
     */
57
    public $sourceAttribute = false;
58
59
    /**
60
     * @var boolean Organization does not need auth key.
61
     */
62
    public $authKeyAttribute = false;
63
64
    /**
65
     * @var boolean Organization does not need access token.
66
     */
67
    public $accessTokenAttribute = false;
68
69
    /**
70
     *
71
     * @var boolean Organization does not need login log.
72
     */
73
    public $loginLogClass = false;
74
75
    public $profileClass = Profile::class;
76
77
    public $memberClass = Member::class;
78
    private $noInitMember;
79
    /**
80
     * @return Member
81
     */
82
    protected function getNoInitMember()
83
    {
84
        if (!$this->noInitMember) {
85
            $class = $this->memberClass;
86
            $this->noInitMember = $class::buildNoInitMember();
87
        }
88
        return $this->noInitMember;
89
    }
90
91
    public function init()
92
    {
93
        if (!is_string($this->queryClass)) {
94
            $this->queryClass = OrganizationQuery::class;
95
        }
96
        if ($this->skipInit) {
97
            return;
98
        }
99
        parent::init();
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105
    public function attributeLabels()
106
    {
107
        return [
108
            'guid' => Yii::t('app', 'GUID'),
109
            'id' => Yii::t('app', 'ID'),
110
            'ip' => Yii::t('app', 'IP'),
111
            'ip_type' => Yii::t('app', 'IP Address Type'),
112
            'parent' => Yii::t('app', 'Parent'),
113
            'created_at' => Yii::t('app', 'Create Time'),
114
            'updated_at' => Yii::t('app', 'Update Time'),
115
            'status' => Yii::t('app', 'Status'),
116
            'type' => Yii::t('app', 'Type'),
117
        ];
118
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123
    public static function tableName()
124
    {
125
        return '{{%organization}}';
126
    }
127
128
    abstract protected function typeAttributeBehavior();
129
130
    /**
131
     * @inheritdoc
132
     */
133
    public function behaviors()
134
    {
135
        return array_merge(parent::behaviors(), $this->typeAttributeBehavior());
136
    }
137
138
    abstract protected function getTypeRules();
139
140
    public function rules()
141
    {
142
        return array_merge(parent::rules(), $this->getTypeRules(), $this->getSelfBlameableRules());
143
    }
144
145
    /**
146
     * Get Member Query.
147
     * @return MemberQuery
148
     */
149
    public function getMembers()
150
    {
151
        return $this->hasMany($this->memberClass, [$this->guidAttribute => $this->getNoInitMember()->createdByAttribute])->inverseOf('organization');
152
    }
153
154
    /**
155
     * 
156
     * @return BaseUserQuery
157
     */
158
    public function getMemberUsers()
159
    {
160
        $noInit = $this->getNoInitMember();
161
        $class = $noInit->memberUserClass;
162
        $noInitUser = $class::buildNoInitModel();
163
        return $this->hasMany($class, [$this->guidAttribute => $noInitUser->guidAttribute])->via('members');
164
    }
165
}
166