Test Failed
Push — master ( 4a086f...4834ec )
by vistart
05:02
created

SetUpForm   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 129
rs 10
c 1
b 0
f 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getUser() 0 7 2
A setUser() 0 9 2
A getParent() 0 7 2
A setParent() 0 13 4
A attributeLabels() 0 11 1
A rules() 0 12 1
A setUpOrganization() 0 4 1
A setUpDepartment() 0 4 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\forms;
14
15
use rhosocial\user\User;
16
use rhosocial\organization\Organization;
17
use Yii;
18
use yii\base\Model;
19
20
/**
21
 * @version 1.0
22
 * @author vistart <[email protected]>
23
 */
24
class SetUpForm extends Model
25
{
26
    public $name;
27
    public $nickname = '';
28
    public $gravatar_type = 0;
29
    public $gravatar = '';
30
    public $timezone = 'UTC';
31
    public $description = '';
32
    /**
33
     * @var Organization 
34
     */
35
    private $_parent;
36
    /**
37
     * @var User 
38
     */
39
    private $_user;
40
41
    /**
42
     * Finds user.
43
     *
44
     * @return User|null
45
     */
46
    public function getUser()
47
    {
48
        if ($this->_user instanceof Yii::$app->user->identityClass) {
49
            return $this->_user;
50
        }
51
        throw new \yii\base\InvalidConfigException('User Class Invalid.');
52
    }
53
54
    /**
55
     * Set user.
56
     * @param User $user
57
     */
58
    public function setUser($user)
59
    {
60
        if ($user instanceof Yii::$app->user->identityClass) {
61
            $this->_user = $user;
62
            return true;
63
        }
64
        $this->_user = null;
65
        return false;
66
    }
67
68
    /**
69
     * Get parent organization or department.
70
     * @return Organization
71
     */
72
    public function getParent()
73
    {
74
        if ($this->_parent instanceof Organization) {
75
            return $this->_parent;
76
        }
77
        return null;
78
    }
79
80
    /**
81
     * Set parent organization or department.
82
     * If you want to set up organization, please set it null.
83
     * @param Organization|string|integer $parent
84
     * @return boolean
85
     */
86
    public function setParent($parent)
87
    {
88
        if (is_numeric($parent) || is_int($parent)) {
89
            $class = $this->getUser()->organizationClass;
90
            $parent = $class::find()->id($parent)->one();
91
        }
92
        if ($parent instanceof Organization) {
93
            $this->_parent = $parent;
94
            return true;
95
        }
96
        $this->_parent = null;
97
        return false;
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103
    public function attributeLabels()
104
    {
105
        return [
106
            'name' => Yii::t('organization', 'Name'),
107
            'nickname' => Yii::t('organization', 'Nickname'),
108
            'gravatar_type' => Yii::t('organization', 'Gravatar Type'),
109
            'gravatar' => Yii::t('organization', 'Gravatar'),
110
            'timezone' => Yii::t('organization', 'Timezone'),
111
            'description' => Yii::t('organization', 'Description'),
112
        ];
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118
    public function rules()
119
    {
120
        return [
121
            ['name', 'required'],
122
            [['nickname', 'gravatar', 'description'], 'default', 'value' => ''],
123
            ['gravatar_type', 'default', 'value' => 0],
124
            ['timezone', 'default', 'UTC'],
125
            [['name', 'nickname', 'gravatar', 'timezone'], 'string', 'max' => 255],
126
            ['description', 'string', 'max' => 65535],
127
            ['gravatar_type', 'integer'],
128
        ];
129
    }
130
131
    /**
132
     * Set up organization.
133
     * You need to make sure that the user who want to set up the organization
134
     * who has the `orgCreator` permission.
135
     * @return boolean
136
     */
137
    public function setUpOrganization()
138
    {
139
        return $this->getUser()->setUpOrganization($this->name, $this->nickname, $this->gravatar_type, $this->gravatar, $this->timezone, $this->descritption);
0 ignored issues
show
Bug introduced by
The property descritption does not seem to exist. Did you mean description?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
140
    }
141
142
    /**
143
     * Set up department.
144
     * You need to make sure that the user who want to set up the department who
145
     * is the creator or administrator of parent one.
146
     * @return boolean
147
     */
148
    public function setUpDepartment()
149
    {
150
        return $this->getUser()->setUpDepartment($this->name, $this->getParent(), $this->nickname, $this->gravatar_type, $this->gravatar, $this->timezone, $this->description);
151
    }
152
}
153