Passed
Push — master ( 36d94c...6f4de6 )
by Sathish
01:39
created

UserManagementConfigExtension::updateCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 64
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 47
nc 1
nop 1
dl 0
loc 64
rs 9.1563
c 0
b 0
f 0

How to fix   Long Method   

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
namespace UserManagement\Extension;
3
4
use SilverStripe\CMS\Model\SiteTree;
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\Tab;
7
use SilverStripe\Forms\TreeDropdownField;
8
use SilverStripe\ORM\DataExtension;
9
use SilverStripe\Security\Group;
10
use SilverStripe\SiteConfig\SiteConfig;
11
use SilverStripe\Forms\TextareaField;
12
use SilverStripe\Forms\CheckboxField;
13
use SilverStripe\Forms\ListboxField;
14
use SilverStripe\Security\Member;
15
16
/**
17
 * Class UserManagementConfigExtension
18
 *
19
 * @package user-management
20
 */
21
class UserManagementConfigExtension extends DataExtension
22
{
23
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
24
        "ProfileUpdateSuccess" => 'Text',
25
        "ProfileUpdatError" => 'Text',
26
        "EnableSpamProtection" => 'Boolean',
27
        "ExportFields" => 'Text'
28
29
    ];
30
    
31
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
32
        'LoginCallBackUrl' => SiteTree::class,
33
        'LoginUrl' => SiteTree::class,
34
        'LostPasswordUrl' => SiteTree::class,
35
        'CustomerGroup' => Group::class
36
    ];
37
    
38
    public function updateCMSFields(FieldList $fields)
39
    {
40
        $fields->insertBefore('Access', $usertab = Tab::create('UserManagement', 'User Management'));
41
        $fields->addFieldToTab(
42
            'Root.UserManagement',
43
            TreeDropdownField::create(
44
                'CustomerGroupID',
45
                _t(__CLASS__ . '.CustomerGroup', 'Group to add new customers to'),
46
                Group::class
47
            )
48
        );
49
        $fields->addFieldToTab(
50
            'Root.UserManagement',
51
            TreeDropdownField::create(
52
                'LoginUrlID',
53
                _t(__CLASS__ . '.LoginUrl', 'Login Url'),
54
                SiteTree::class
55
            )
56
        );
57
        $fields->addFieldToTab(
58
            'Root.UserManagement',
59
            TreeDropdownField::create(
60
                'LoginCallBackUrlID',
61
                _t(__CLASS__ . '.LoginCallBackUrl', 'Login Call Back Url'),
62
                SiteTree::class
63
            )
64
        );
65
        $fields->addFieldToTab(
66
            'Root.UserManagement',
67
            TreeDropdownField::create(
68
                'LostPasswordUrlID',
69
                _t(__CLASS__ . '.LostPasswordUrl', 'Lost Password Url'),
70
                SiteTree::class
71
            )
72
        );
73
        $fields->addFieldToTab(
74
            'Root.UserManagement',
75
            TextareaField::create(
76
                'ProfileUpdateSuccess',
77
                _t(__CLASS__ . '.ProfileUpdateSuccess', 'Profile update Success Message')
78
            )
79
        );
80
        $fields->addFieldToTab(
81
            'Root.UserManagement',
82
            TextareaField::create(
83
                'ProfileUpdatError',
84
                _t(__CLASS__ . '.ProfileUpdatError', 'Profile update Error Message')
85
            )
86
        );
87
        $fields->addFieldToTab(
88
            'Root.UserManagement',
89
            CheckboxField::create(
90
                'EnableSpamProtection',
91
                _t(__CLASS__ . '.EnableSpamProtection', 'Enable Spam Protection')
92
            )
93
        );
94
        
95
        $fields->addFieldToTab(
96
            'Root.UserManagement',
97
            ListboxField::create(
98
                'ExportFields',
99
                'Select Data fields for the report',
100
                $this->getExportFieldNames(),
101
                json_decode($this->owner->ExportFields)
102
            )
103
        );
104
    }
105
    
106
    /**
107
     * Returns login page id
108
     *
109
     * @return integer
110
     */
111
    public function getLoginUrlID()
112
    {
113
        if (!$this->owner->LoginUrl()) {
114
            return SiteTree::get()
0 ignored issues
show
Bug Best Practice introduced by
The expression return SilverStripe\CMS\...serLoginPage')->first() returns the type SilverStripe\ORM\DataObject which is incompatible with the documented return type integer.
Loading history...
115
            ->filter('ClassName', 'UserManagement\Page\UserLoginPage')->first();
116
        } else {
117
            return $this->owner->LoginUrl()->ID;
118
        }
119
    }
120
    
121
    /**
122
     * Returns call back page id
123
     *
124
     * @return integer
125
     */
126
    public function getLoginCallBackUrlID()
127
    {
128
        if (!$this->owner->LoginCallBackUrl()) {
129
            return SiteTree::get()
0 ignored issues
show
Bug Best Practice introduced by
The expression return SilverStripe\CMS\...rProfilePage')->first() returns the type SilverStripe\ORM\DataObject which is incompatible with the documented return type integer.
Loading history...
130
            ->filter('ClassName', 'UserManagement\Page\UserProfilePage')->first();
131
        } else {
132
            return $this->owner->LoginCallBackUrl()->ID;
133
        }
134
    }
135
136
    /**
137
     * Returns lost password page id
138
     *
139
     * @return integer
140
     */
141
    public function getLostPasswordUrlID()
142
    {
143
        if (!$this->owner->LostPasswordUrl()) {
144
            return SiteTree::get()->filter('ClassName', 'UserManagement\Page\LostPasswordPage')->first()->ID;
145
        } else {
146
            return $this->owner->LostPasswordUrl()->ID;
147
        }
148
    }
149
    
150
    
151
    /**
152
     * Returns customer group id
153
     *
154
     * @return integer
155
     */
156
    public function getCustomerGroupID()
157
    {
158
        if (!$this->owner->CustomerGroup()) {
159
            return Group::get()->filter('Title', 'general')->first()->ID;
160
        } else {
161
            return $this->owner->CustomerGroup()->ID;
162
        }
163
    }
164
165
    public function getExportFieldNames()
166
    {
167
        $memberFields = Member::create()->getFrontEndFields()->dataFieldNames();
168
        $memberFields = array_diff($memberFields, ["FirstName", "Surname", "Email", "TempIDHash", "TempIDExpired", "AutoLoginHash", "AutoLoginExpired","PasswordEncryption","Salt","Locale", "FailedLoginCount", "LockedOutUntil", "Password", "PasswordExpiry"]);
169
        return array_combine($memberFields, $memberFields);
170
    }
171
}
172