Passed
Push — master ( 0367eb...a274db )
by vistart
03:21
created

Organization::isDepartment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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\user\rbac\Role;
19
use rhosocial\organization\queries\MemberQuery;
20
use rhosocial\organization\queries\DepartmentQuery;
21
use rhosocial\organization\queries\OrganizationQuery;
22
use Yii;
23
24
/**
25
 * Base Organization.
26
 * This class is an abstract class that can not be instantiated directly.
27
 * You can use [[Organization]] or [[Department]] instead.
28
 *
29
 * @method Member createMember(array $config) Create member who is subordinate to this.
30
 * @property integer $type Whether indicate this instance is an organization or a department.
31
 
32
 * @version 1.0
33
 * @author vistart <[email protected]>
34
 */
35
class Organization extends User
36
{
37
    use SelfBlameableTrait;
38
39
    const TYPE_ORGANIZATION = 1;
40
    const TYPE_DEPARTMENT = 2;
41
42
    /**
43
     * @var boolean Organization does not need password and corresponding features.
44
     */
45
    public $passwordHashAttribute = false;
46
47
    /**
48
     * @var boolean Organization does not need password and corresponding features.
49
     */
50
    public $passwordResetTokenAttribute = false;
51
52
    /**
53
     * @var boolean Organization does not need password and corresponding features.
54
     */
55
    public $passwordHistoryClass = false;
56
57
    /**
58
     * @var boolean Organization does not need source.
59
     */
60
    public $sourceAttribute = false;
61
62
    /**
63
     * @var boolean Organization does not need auth key.
64
     */
65
    public $authKeyAttribute = false;
66
67
    /**
68
     * @var boolean Organization does not need access token.
69
     */
70
    public $accessTokenAttribute = false;
71
72
    /**
73
     *
74
     * @var boolean Organization does not need login log.
75
     */
76
    public $loginLogClass = false;
77
78
    public $profileClass = Profile::class;
79
80
    public $memberClass = Member::class;
81
    private $noInitMember;
82
    /**
83
     * @return Member
84
     */
85 7
    protected function getNoInitMember()
86
    {
87 7
        if (!$this->noInitMember) {
88 7
            $class = $this->memberClass;
89 7
            $this->noInitMember = $class::buildNoInitModel();
90
        }
91 7
        return $this->noInitMember;
92
    }
93
94 21
    public function init()
95
    {
96 21
        $this->parentAttribute = 'parent_guid';
97 21
        if (class_exists($this->memberClass)) {
98 21
            $this->addSubsidiaryClass('Member', ['class' => $this->memberClass]);
99
        }
100 21
        if ($this->skipInit) {
101 21
            return;
102
        }
103 21
        parent::init();
104 21
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109 1
    public function attributeLabels()
110
    {
111
        return [
112 1
            'guid' => Yii::t('app', 'GUID'),
113 1
            'id' => Yii::t('app', 'ID'),
114 1
            'ip' => Yii::t('app', 'IP'),
115 1
            'ip_type' => Yii::t('app', 'IP Address Type'),
116 1
            'parent' => Yii::t('app', 'Parent'),
117 1
            'created_at' => Yii::t('app', 'Create Time'),
118 1
            'updated_at' => Yii::t('app', 'Update Time'),
119 1
            'status' => Yii::t('app', 'Status'),
120 1
            'type' => Yii::t('app', 'Type'),
121
        ];
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127 21
    public static function tableName()
128
    {
129 21
        return '{{%organization}}';
130
    }
131
132 20
    protected function getTypeRules()
133
    {
134
        return [
135 20
            ['type', 'default', 'value' => static::TYPE_ORGANIZATION],
136
            ['type', 'required'],
137 20
            ['type', 'in', 'range' => [static::TYPE_ORGANIZATION, static::TYPE_DEPARTMENT]],
138
        ];
139
    }
140
141 20
    public function rules()
142
    {
143 20
        return array_merge(parent::rules(), $this->getTypeRules(), $this->getSelfBlameableRules());
144
    }
145
146
    /**
147
     * Get Member Query.
148
     * @return MemberQuery
149
     */
150 7
    public function getMembers()
151
    {
152 7
        return $this->hasMany($this->memberClass, [$this->getNoInitMember()->createdByAttribute => $this->guidAttribute])->inverseOf('organization');
153
    }
154
155
    /**
156
     * Get organization member users' query.
157
     * @return BaseUserQuery
158
     */
159 2
    public function getMemberUsers()
160
    {
161 2
        $noInit = $this->getNoInitMember();
162 2
        $class = $noInit->memberUserClass;
163 2
        $noInitUser = $class::buildNoInitModel();
164 2
        return $this->hasMany($class, [$noInitUser->guidAttribute => $this->getNoInitMember()->memberAttribute])->via('members')->inverseOf('atOrganizations');
165
    }
166
167
    /**
168
     * Get member with specified user.
169
     * @param User|string|integer $user
170
     * @return Member Null if `user` is not in this organization.
171
     */
172 4
    public function getMember($user)
173
    {
174 4
        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 174 which is incompatible with the return type documented by rhosocial\organization\Organization::getMember of type rhosocial\organization\Member|null.
Loading history...
175
    }
176
177
    /**
178
     * Add member to organization.
179
     * @param Member|User|string|integer $member
180
     * @see createMemberModel
181
     * @see createMemberModelWithUser
182
     * @return boolean
183
     */
184 3
    public function addMember(&$member)
185
    {
186 3
        if ($this->getIsNewRecord()) {
187
            return false;
188
        }
189 3
        $model = null;
190 3
        if ($member instanceof Member) {
191
            $model = $this->createMemberModel($member);
192
        }
193 3
        if (($member instanceof User) || is_string($member) || is_int($member)) {
194 3
            $model = $this->createMemberModelWithUser($member);
195
        }
196 3
        $member = $model;
197 3
        return ($member instanceof Member) ? $member->save() : false;
198
    }
199
200
    /**
201
     * Create member model, and set organization with this.
202
     * @param Member $member If this parameter is not new record, it's organization
203
     * will be set with this, and return it. Otherwise, it will extract `User`
204
     * model and create new `Member` model.
205
     * @see createMemberModelWithUser
206
     * @return Member
207
     */
208
    public function createMemberModel($member)
209
    {
210
        if (!$member->getIsNewRecord()) {
211
            $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...
212
            return $member;
213
        }
214
        return $this->createMemberModelWithUser($member->memberUser);
215
    }
216
217
    /**
218
     * Create member model with user, and set organization with this.
219
     * @param User|string|integer $user
220
     * @return Member
221
     */
222 19
    public function createMemberModelWithUser($user)
223
    {
224
        $config = [
225 19
            'memberUser' => $user,
226 19
            'organization' => $this,
227 19
            'nickname' => '',
228
        ];
229 19
        if ($user->profile) {
230 19
            $config['nickname'] = $user->profile->nickname;
231
        }
232 19
        return $this->createMember($config);
233
    }
234
235
    /**
236
     * Remove member.
237
     * @param Member|User $member
238
     * @return boolean
239
     */
240 1
    public function removeMember(&$member)
241
    {
242 1
        if ($this->getIsNewRecord()) {
243
            return false;
244
        }
245 1
        if ($member instanceof $this->memberClass) {
246 1
            $member = $member->{$member->memberAttribute};
247
        }
248 1
        $member = $this->getMember($member);
249 1
        return $member && $member->delete() > 0;
250
    }
251
252
    /**
253
     * 
254
     * @return boolean
255
     */
256 2
    public function isOrganization()
257
    {
258 2
        return $this->type == static::TYPE_ORGANIZATION;
259
    }
260
261
    /**
262
     * 
263
     * @return boolean
264
     */
265 2
    public function isDepartment()
266
    {
267 2
        return $this->type == static::TYPE_DEPARTMENT;
268
    }
269
270
    /**
271
     * Check whether the current organization has a member.
272
     * @param User $user
273
     * @return boolean
274
     */
275 1
    public function hasMember($user)
276
    {
277 1
        return !is_null($this->getMember($user));
278
    }
279
}
280