Completed
Push — master ( 033d43...b96e2f )
by vistart
07:33
created

SettingsForm::submit()   B

Complexity

Conditions 11
Paths 9

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 7.1162
cc 11
eloc 11
nc 9
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\organization\Organization;
16
use Yii;
17
use yii\base\InvalidConfigException;
18
use yii\base\Model;
19
20
/**
21
 * Class SettingsForm
22
 * @package rhosocial\organization\forms
23
 * @version 1.0
24
 * @author vistart <[email protected]>
25
 */
26
class SettingsForm extends Model
27
{
28
    /**
29
     * @var string
30
     */
31
    public $exclude_other_members;
32
33
    /**
34
     * @var string
35
     */
36
    public $disallow_member_join_other;
37
38
    /**
39
     * @var string
40
     */
41
    public $only_accept_current_org_member;
42
43
    /**
44
     * @var string
45
     */
46
    public $only_accept_superior_org_member;
47
48
    const SCENARIO_ORGANIZATION = 'organization';
49
    const SCENARIO_DEPARTMENT = 'department';
50
51
    /**
52
     * @var Organization
53
     */
54
    public $organization;
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function init()
60
    {
61
        if (!$this->organization) {
62
            throw new InvalidConfigException('Invalid Organization Model.');
63
        }
64
        $this->scenario = $this->organization->isOrganization() ? static::SCENARIO_ORGANIZATION : static::SCENARIO_DEPARTMENT;
65
        $this->loadSettings();
66
    }
67
68
    /**
69
     * Load settings.
70
     */
71
    protected function loadSettings()
72
    {
73
        if ($this->organization->isOrganization()) {
74
            $this->exclude_other_members = $this->organization->isExcludeOtherMembers ? '1' : '0';
75
            $this->disallow_member_join_other = $this->organization->isDisallowMemberJoinOther ? '1' : '0';
76
        } elseif ($this->organization->isDepartment()) {
77
            $this->only_accept_current_org_member = $this->organization->isOnlyAcceptCurrentOrgMember ? '1' : '0';
78
            $this->only_accept_superior_org_member = $this->organization->isOnlyAcceptSuperiorOrgMember ? '1' : '0';
79
        }
80
    }
81
82
    /**
83
     * Submit settings.
84
     */
85
    public function submit()
86
    {
87
        if ($this->organization->isOrganization()) {
88
            if ($this->exclude_other_members != ($this->organization->isExcludeOtherMembers ? '1' : '0')) {
89
                $this->organization->isExcludeOtherMembers = ($this->exclude_other_members == '1');
90
            }
91
            if ($this->disallow_member_join_other != ($this->organization->isDisallowMemberJoinOther ? '1' : '0')) {
92
                $this->organization->isDisallowMemberJoinOther = ($this->disallow_member_join_other == '1');
93
            }
94
        } elseif ($this->organization->isDepartment()) {
95
            if ($this->only_accept_current_org_member != ($this->organization->isOnlyAcceptCurrentOrgMember ? '1' : '0')) {
96
                $this->organization->isOnlyAcceptCurrentOrgMember = ($this->only_accept_current_org_member == '1');
97
            }
98
            if ($this->only_accept_superior_org_member != ($this->organization->isOnlyAcceptSuperiorOrgMember ? '1' : '0')) {
99
                $this->organization->isOnlyAcceptSuperiorOrgMember = ($this->only_accept_superior_org_member == '1');
100
            }
101
        }
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107
    public function attributeLabels()
108
    {
109
        return [
110
            'exclude_other_members' => Yii::t('organization', 'Exclude other members'),
111
            'disallow_member_join_other' => Yii::t('organization', 'Disallow members to join other'),
112
            'only_accept_current_org_member' => Yii::t('organization', 'Only accept organization members'),
113
            'only_accept_superior_org_member' => Yii::t('organization', 'Only accept superior members'),
114
        ];
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120
    public function attributeHints()
121
    {
122
        $topName = $this->organization->isDepartment() ? $this->organization->topOrganization->profile->name . ' (' . $this->organization->topOrganization->getID() . ')' : '';
123
        $parentName = $this->organization->isDepartment() ? $this->organization->parent->profile->name . ' (' . $this->organization->parent->getID() . ')' : '';
0 ignored issues
show
Bug introduced by
The property profile does not seem to exist in rhosocial\base\models\traits\SelfBlameableTrait.

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...
Bug introduced by
It seems like getID() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
124
        return [
125
            'exclude_other_members' => Yii::t('organization', 'This organization does not allow other organizations and their subordinates\' members to join.') . "\n" . Yii::t('organization', 'All members of the other organizations (including their subordinates) who have joined this organization (including subordinate departments) are not affected.'),
126
            'disallow_member_join_other' => Yii::t('organization', 'This organization does not allow the organization and its subordinates\' members to join other organizations or their subordinates.') . "\n" . Yii::t('organization', 'All members of this organization (including subordinate departments) who have joined other organizations (including their subordinates) are not affected.') . "\n" . Yii::t('organization', 'If this option is enabled, all members of the organization (including subordinate departments) who have the "Set Up Organization" permission will not be able to set up a new organization.'),
127
            'only_accept_current_org_member' => Yii::t('organization', 'This department is only accepted by members of the organization.') . "\n" . Yii::t('organization', 'That is to say, only the members of {name} are accepted.', ['name' => $topName]),
128
            'only_accept_superior_org_member' => Yii::t('organization', 'This department only accepts members of the parent organization or department.') . "\n" . Yii::t('organization', 'That is to say, only the members of {name} are accepted.', ['name' => $parentName]),
129
        ];
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135
    public function rules()
136
    {
137
        return [
138
            [['exclude_other_members', 'disallow_member_join_other', 'only_accept_current_org_member', 'only_accept_superior_org_member'], 'boolean', 'trueValue' => '1', 'falseValue' => '0'],
139
        ];
140
    }
141
142
    /**
143
     * @inheritdoc
144
     */
145
    public function scenarios()
146
    {
147
        return [
148
            static::SCENARIO_ORGANIZATION => ['exclude_other_members', 'disallow_member_join_other'],
149
            static::SCENARIO_DEPARTMENT => ['only_accept_current_org_member', 'only_accept_superior_org_member'],
150
        ];
151
    }
152
}
153