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

Member::init()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
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
20
/**
21
 * Organization member.
22
 *
23
 * @property string $organization_guid
24
 * @proprtyy string $user_guid
25
 * @property string $nickname
26
 * @property string $description
27
 * 
28
 * @property string $department_guid
29
 * @property string $member_guid
30
 * @property User $memberUser
31
 *
32
 * @version 1.0
33
 * @author vistart <[email protected]>
34
 */
35
class Member extends BaseBlameableModel
36
{
37
    public $createdByAttribute = 'organization_guid';
38
    public $updatedByAttribute = false;
39
    public $hostClass = Organization::class;
40
41
    public $memberAttribute = 'user_guid';
42
    public $memberUserClass = User::class;
43
    private $noInitMemberUser;
44
    /**
45
     * @return User
46
     */
47
    protected function getNoInitMemberUser()
48
    {
49
        if (!$this->noInitMemberUser) {
50
            $class = $this->memberUserClass;
51
            $this->noInitMemberUser = $class::buildNoInitModel();
52
        }
53
        return $this->noInitMemberUser;
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function init()
60
    {
61
        if (!is_string($this->queryClass)) {
62
            $this->queryClass = MemberQuery::class;
63
        }
64
        if ($this->skipInit) {
65
            return;
66
        }
67
        parent::init();
68
    }
69
70
    public $descriptionAttribute = 'description';
71
72
    public function getMemberUserRules()
73
    {
74
        return [
75
            [$this->memberAttribute, 'required'],
76
            [$this->memberAttribute, 'string', 'max' => 36],
77
        ];
78
    }
79
80
    /**
81
     * 
82
     * @param User $user
83
     */
84
    public function setMemberUser($user)
85
    {
86
        $this->user_guid = $user->getGUID();
0 ignored issues
show
Documentation introduced by
The property user_guid does not exist on object<rhosocial\organization\Member>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
87
    }
88
89
    public function getMemberUser()
90
    {
91
        return $this->hasOne($this->memberUserClass, [$this->memberAttribute => $this->getNoInitMemberUser()->guidAttribute]);
92
    }
93
94
    /**
95
     * Get Organization Query.
96
     * Alias of `getHost` method.
97
     * @return OrganizationQuery
98
     */
99
    public function getOrganization()
100
    {
101
        return $this->getHost();
102
    }
103
104
    public function rules()
105
    {
106
        return array_merge($this->getMemberUserRules(), parent::rules());
107
    }
108
109
    /**
110
     * @inheritdoc
111
     */
112
    public static function tableName()
113
    {
114
        return '{{%organization_member}}';
115
    }
116
}
117