Completed
Push — master ( f8cbc9...76d295 )
by vistart
03:56
created

Organization::createMemberModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
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\DepartmentQuery;
20
use rhosocial\organization\queries\OrganizationQuery;
21
use Yii;
22
23
/**
24
 * Base Organization.
25
 * This class is an abstract class that can not be instantiated directly.
26
 * You can use [[Organization]] or [[Department]] instead.
27
 *
28
 * @method Member createMember(array $config) Create member who is subordinate to this.
29
 * @property integer $type Whether indicate this instance is an organization or a department.
30
 
31
 * @version 1.0
32
 * @author vistart <[email protected]>
33
 */
34
class Organization extends User
35
{
36
    use SelfBlameableTrait;
37
38
    const TYPE_ORGANIZATION = 1;
39
    const TYPE_DEPARTMENT = 2;
40
41
    /**
42
     * @var boolean Organization does not need password and corresponding features.
43
     */
44
    public $passwordHashAttribute = false;
45
46
    /**
47
     * @var boolean Organization does not need password and corresponding features.
48
     */
49
    public $passwordResetTokenAttribute = false;
50
51
    /**
52
     * @var boolean Organization does not need password and corresponding features.
53
     */
54
    public $passwordHistoryClass = false;
55
56
    /**
57
     * @var boolean Organization does not need source.
58
     */
59
    public $sourceAttribute = false;
60
61
    /**
62
     * @var boolean Organization does not need auth key.
63
     */
64
    public $authKeyAttribute = false;
65
66
    /**
67
     * @var boolean Organization does not need access token.
68
     */
69
    public $accessTokenAttribute = false;
70
71
    /**
72
     *
73
     * @var boolean Organization does not need login log.
74
     */
75
    public $loginLogClass = false;
76
77
    public $profileClass = Profile::class;
78
79
    public $memberClass = Member::class;
80
    private $noInitMember;
81
    /**
82
     * @return Member
83
     */
84 5
    protected function getNoInitMember()
85
    {
86 5
        if (!$this->noInitMember) {
87 5
            $class = $this->memberClass;
88 5
            $this->noInitMember = $class::buildNoInitModel();
89
        }
90 5
        return $this->noInitMember;
91
    }
92
93 18
    public function init()
94
    {
95 18
        $this->parentAttribute = 'parent_guid';
96 18
        if (class_exists($this->memberClass)) {
97 18
            $this->addSubsidiaryClass('Member', ['class' => $this->memberClass]);
98
        }
99 18
        if ($this->skipInit) {
100 18
            return;
101
        }
102 18
        parent::init();
103 18
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function attributeLabels()
109
    {
110
        return [
111
            'guid' => Yii::t('app', 'GUID'),
112
            'id' => Yii::t('app', 'ID'),
113
            'ip' => Yii::t('app', 'IP'),
114
            'ip_type' => Yii::t('app', 'IP Address Type'),
115
            'parent' => Yii::t('app', 'Parent'),
116
            'created_at' => Yii::t('app', 'Create Time'),
117
            'updated_at' => Yii::t('app', 'Update Time'),
118
            'status' => Yii::t('app', 'Status'),
119
            'type' => Yii::t('app', 'Type'),
120
        ];
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126 18
    public static function tableName()
127
    {
128 18
        return '{{%organization}}';
129
    }
130
131 17
    protected function getTypeRules()
132
    {
133
        return [
134 17
            ['type', 'default', 'value' => static::TYPE_ORGANIZATION],
135
            ['type', 'required'],
136 17
            ['type', 'in', 'range' => [static::TYPE_ORGANIZATION, static::TYPE_DEPARTMENT]],
137
        ];
138
    }
139
140 17
    public function rules()
141
    {
142 17
        return array_merge(parent::rules(), $this->getTypeRules(), $this->getSelfBlameableRules());
143
    }
144
145
    /**
146
     * Get Member Query.
147
     * @return MemberQuery
148
     */
149 5
    public function getMembers()
150
    {
151 5
        return $this->hasMany($this->memberClass, [$this->getNoInitMember()->createdByAttribute => $this->guidAttribute])->inverseOf('organization');
152
    }
153
154
    /**
155
     * Get organization member users' query.
156
     * @return BaseUserQuery
157
     */
158 2
    public function getMemberUsers()
159
    {
160 2
        $noInit = $this->getNoInitMember();
161 2
        $class = $noInit->memberUserClass;
162 2
        $noInitUser = $class::buildNoInitModel();
163 2
        return $this->hasMany($class, [$noInitUser->guidAttribute => $this->getNoInitMember()->memberAttribute])->via('members')->inverseOf('atOrganizations');
164
    }
165
166
    /**
167
     * Get member with specified user.
168
     * @param User|string|integer $user
169
     * @return Member Null if `user` is not in this organization.
170
     */
171 3
    public function getMember($user)
172
    {
173 3
        return $this->getMembers()->user($user)->one();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getMembers()->user($user)->one(); of type yii\db\ActiveRecord|array|null adds the type array to the return on line 173 which is incompatible with the return type documented by rhosocial\organization\Organization::getMember of type rhosocial\organization\Member|null.
Loading history...
174
    }
175
176
    /**
177
     * Add member to organization.
178
     * @param Member|User|string|integer $member
179
     * @see createMemberModel
180
     * @see createMemberModelWithUser
181
     * @return boolean
182
     */
183 3
    public function addMember(&$member)
184
    {
185 3
        if ($this->getIsNewRecord()) {
186
            return false;
187
        }
188 3
        $model = null;
189 3
        if ($member instanceof Member) {
190
            $model = $this->createMemberModel($member);
191
        }
192 3
        if (($member instanceof User) || is_string($member) || is_int($member)) {
193 3
            $model = $this->createMemberModelWithUser($member);
194
        }
195 3
        $member = $model;
196 3
        return ($member instanceof Member) ? $member->save() : false;
197
    }
198
199
    /**
200
     * Create member model, and set organization with this.
201
     * @param Member $member If this parameter is not new record, it's organization
202
     * will be set with this, and return it. Otherwise, it will extract `User`
203
     * model and create new `Member` model.
204
     * @see createMemberModelWithUser
205
     * @return Member
206
     */
207
    public function createMemberModel($member)
208
    {
209
        if (!$member->getIsNewRecord()) {
210
            $member->setOrganization($this);
0 ignored issues
show
Documentation introduced by
$this is of type this<rhosocial\organization\Organization>, but the function expects a object<rhosocial\organization\BaseOrganization>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
211
            return $member;
212
        }
213
        return $this->createMemberModelWithUser($member->memberUser);
214
    }
215
216
    /**
217
     * Create member model with user, and set organization with this.
218
     * @param User|string|integer $user
219
     * @return Member
220
     */
221 16
    public function createMemberModelWithUser($user)
222
    {
223
        $config = [
224 16
            'memberUser' => $user,
225 16
            'organization' => $this,
226 16
            'nickname' => '',
227
        ];
228 16
        if ($user->profile) {
229 16
            $config['nickname'] = $user->profile->nickname;
230
        }
231 16
        return $this->createMember($config);
232
    }
233
234
    /**
235
     * Remove member.
236
     * @param Member|User $member
237
     * @return boolean
238
     */
239 1
    public function removeMember(&$member)
240
    {
241 1
        if ($this->getIsNewRecord()) {
242
            return false;
243
        }
244 1
        if ($member instanceof $this->memberClass) {
245 1
            $member = $member->{$member->memberAttribute};
246
        }
247 1
        $member = $this->getMember($member);
248 1
        return $member && $member->delete() > 0;
249
    }
250
}
251