Completed
Push — develop ( 1c3d8e...dc7d05 )
by Mohamed
07:44
created

User   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.5%

Importance

Changes 10
Bugs 4 Features 3
Metric Value
wmc 10
c 10
b 4
f 3
lcom 1
cbo 3
dl 0
loc 138
ccs 39
cts 40
cp 0.975
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B fields() 0 30 1
B innerFields() 0 24 2
A rules() 0 17 2
A passwordFields() 0 19 1
A actions() 0 10 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
67 5
        $fields += $this->innerFields();
68
69 5
        return $fields;
70
    }
71
72
    /**
73
     * Return password fields.
74
     *
75
     * @return array
76
     */
77 4
    protected function passwordFields()
78
    {
79 4
        $fields                                       = [];
80 4
        $fields['only_complete_if_changing_password'] = [
81
            'type' => 'legend',
82
        ];
83 4
        $fields['password'] = [
84
            'type'         => 'password',
85
            'label'        => 'new_password',
86
            'autocomplete' => 'off',
87
        ];
88 4
        $fields['password_confirmation'] = [
89
            'type'         => 'password',
90
            'label'        => 'confirm',
91
            'autocomplete' => 'off',
92
        ];
93
94 4
        return $fields;
95
    }
96
97
    /**
98
     * For sub-classes to add extra fields or remove fields.
99
     *
100
     * @return array
101
     */
102 2
    protected function innerFields()
103
    {
104
        $fields = [
105
            'extended_user_settings' => [
106
                'type' => 'legend',
107 2
            ],
108
            'role_id'                => [
109 2
                'type'    => 'select',
110 2
                'label'   => 'role',
111 2
                'options' => Role::dropdown(),
112
            ],
113
            'status'                 => [
114 2
                'type'    => 'select',
115 2
                'label'   => 'Status',
116 2
                'options' => UserModel::getStatuses(),
117
            ],
118
        ];
119
120 2
        if ($this->isEditing()) {
121 1
            $fields += $this->passwordFields();
122
        }
123
124 2
        return $fields;
125
    }
126
127
    /**
128
     * @return array
129
     */
130 5
    public function rules()
131
    {
132
        $rules = [
133 5
            'firstname' => 'required|max:50',
134
            'lastname'  => 'required|max:50',
135
            'email'     => 'required|email',
136
        ];
137
138 5
        if ($this->isEditing()) {
139 4
            $rules['email'] .= '|unique:users,email,' . $this->getModel()->id;
140 4
            $rules['password'] = 'confirmed';
141
        } else {
142 1
            $rules['email'] .= '|unique:users,email';
143
        }
144
145 5
        return $rules;
146
    }
147
148
    /**
149
     * @return string
150
     */
151 1
    public function getRedirectUrl()
152
    {
153 1
        if ($this->isEditing()) {
154
            return 'administration/users/edit/' . $this->getModel()->id;
155
        }
156
157 1
        return 'administration/users/add/';
158
    }
159
}
160