Completed
Push — develop ( 671a92...719295 )
by Mohamed
10:02
created

User   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.62%

Importance

Changes 10
Bugs 4 Features 2
Metric Value
wmc 11
c 10
b 4
f 2
lcom 1
cbo 3
dl 0
loc 145
ccs 41
cts 42
cp 0.9762
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A actions() 0 10 2
B fields() 0 37 2
A passwordFields() 0 19 1
B innerFields() 0 24 2
A rules() 0 17 2
A getRedirectUrl() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Form;
13
14
use Tinyissue\Model\Role;
15
use Tinyissue\Model\User as UserModel;
16
17
/**
18
 * User is a class to defines fields & rules for add/edit user form.
19
 *
20
 * @author Mohamed Alsharaf <[email protected]>
21
 */
22
class User extends FormAbstract
23
{
24
    /**
25
     * @return array
26
     */
27 2
    public function actions()
28
    {
29 2
        if ($this->isEditing()) {
30
            return [
31 1
                'submit' => 'update',
32
            ];
33
        }
34
35 1
        return ['submit' => 'add_user'];
36
    }
37
38
    /**
39
     * @return array
40
     */
41 5
    public function fields()
42
    {
43
        $fields = [
44
            'firstname' => [
45
                'type'  => 'text',
46
                'label' => 'first_name',
47 5
            ],
48
            'lastname'  => [
49
                'type'  => 'text',
50
                'label' => 'last_name',
51
            ],
52
            'email'     => [
53
                'type'  => 'text',
54
                'label' => 'email',
55
            ],
56
            'private'   => [
57 5
                'type'    => 'select',
58 5
                'label'   => 'visibility',
59
                'options' => [
60 5
                    UserModel::PRIVATE_YES => trans('tinyissue.private'),
61 5
                    UserModel::PRIVATE_NO  => trans('tinyissue.public'),
62
                ],
63
            ],
64
        ];
65
66 5
        if (!$this->isEditing()) {
67 1
            $fields['password'] = [
68
                'type'         => 'password',
69
                'label'        => 'password',
70
                'autocomplete' => 'off',
71
            ];
72
        }
73
74 5
        $fields += $this->innerFields();
75
76 5
        return $fields;
77
    }
78
79
    /**
80
     * Return password fields.
81
     *
82
     * @return array
83
     */
84 4
    protected function passwordFields()
85
    {
86 4
        $fields                                       = [];
87 4
        $fields['only_complete_if_changing_password'] = [
88
            'type' => 'legend',
89
        ];
90 4
        $fields['password'] = [
91
            'type'         => 'password',
92
            'label'        => 'new_password',
93
            'autocomplete' => 'off',
94
        ];
95 4
        $fields['password_confirmation'] = [
96
            'type'         => 'password',
97
            'label'        => 'confirm',
98
            'autocomplete' => 'off',
99
        ];
100
101 4
        return $fields;
102
    }
103
104
    /**
105
     * For sub-classes to add extra fields or remove fields.
106
     *
107
     * @return array
108
     */
109 2
    protected function innerFields()
110
    {
111
        $fields = [
112
            'extended_user_settings' => [
113
                'type' => 'legend',
114 2
            ],
115
            'role_id'                => [
116 2
                'type'    => 'select',
117 2
                'label'   => 'role',
118 2
                'options' => Role::dropdown(),
119
            ],
120
            'status'                 => [
121 2
                'type'    => 'select',
122 2
                'label'   => 'Status',
123 2
                'options' => UserModel::getStatuses(),
124
            ],
125
        ];
126
127 2
        if ($this->isEditing()) {
128 1
            $fields += $this->passwordFields();
129
        }
130
131 2
        return $fields;
132
    }
133
134
    /**
135
     * @return array
136
     */
137 5
    public function rules()
138
    {
139
        $rules = [
140 5
            'firstname' => 'required|max:50',
141
            'lastname'  => 'required|max:50',
142
            'email'     => 'required|email',
143
        ];
144
145 5
        if ($this->isEditing()) {
146 4
            $rules['email'] .= '|unique:users,email,' . $this->getModel()->id;
147 4
            $rules['password'] = 'confirmed';
148
        } else {
149 1
            $rules['email'] .= '|unique:users,email';
150
        }
151
152 5
        return $rules;
153
    }
154
155
    /**
156
     * @return string
157
     */
158 1
    public function getRedirectUrl()
159
    {
160 1
        if ($this->isEditing()) {
161
            return 'administration/users/edit/' . $this->getModel()->id;
162
        }
163
164 1
        return 'administration/users/add/';
165
    }
166
}
167