Completed
Push — develop ( f7252d...97603d )
by Mohamed
04:44
created

User   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.86%

Importance

Changes 5
Bugs 2 Features 2
Metric Value
wmc 10
c 5
b 2
f 2
lcom 1
cbo 2
dl 0
loc 119
ccs 26
cts 28
cp 0.9286
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fields() 0 21 1
A passwordFields() 0 17 1
A rules() 0 17 2
A actions() 0 10 2
A innerFields() 0 16 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
16
/**
17
 * User is a class to defines fields & rules for add/edit user form
18
 *
19
 * @author Mohamed Alsharaf <[email protected]>
20
 */
21
class User extends FormAbstract
22
{
23
    /**
24
     * @return array
25
     */
26 1
    public function actions()
27
    {
28 1
        if ($this->isEditing()) {
29
            return [
30
                'submit' => 'update',
31
            ];
32
        }
33
34 1
        return ['submit' => 'add_user'];
35
    }
36
37
    /**
38
     * @return array
39
     */
40 4
    public function fields()
41
    {
42
        $fields = [
43
            'firstname' => [
44
                'type'  => 'text',
45
                'label' => 'first_name',
46 4
            ],
47
            'lastname' => [
48
                'type'  => 'text',
49
                'label' => 'last_name',
50
            ],
51
            'email' => [
52
                'type'  => 'text',
53
                'label' => 'email',
54
            ],
55
        ];
56
57 4
        $fields += $this->innerFields();
58
59 4
        return $fields;
60
    }
61
62
    /**
63
     * Return password fields
64
     *
65
     * @return array
66
     */
67 3
    protected function passwordFields()
68
    {
69 3
        $fields = [];
70 3
        $fields['only_complete_if_changing_password'] = [
71
            'type' => 'legend',
72
        ];
73 3
        $fields['password'] = [
74
            'type'  => 'password',
75
            'label' => 'new_password',
76
        ];
77 3
        $fields['password_confirmation'] = [
78
            'type'  => 'password',
79
            'label' => 'confirm',
80
        ];
81
82 3
        return $fields;
83
    }
84
85
    /**
86
     * For sub-classes to add extra fields or remove fields
87
     *
88
     * @return array
89
     */
90 1
    protected function innerFields()
91
    {
92
        $fields = [
93
            'role_id' => [
94 1
                'type'    => 'select',
95 1
                'label'   => 'role',
96 1
                'options' => Role::dropdown(),
97
            ],
98
        ];
99
100 1
        if ($this->isEditing()) {
101
            $fields += $this->passwordFields();
102
        }
103
104 1
        return $fields;
105
    }
106
107
    /**
108
     * @return array
109
     */
110 4
    public function rules()
111
    {
112
        $rules = [
113 4
            'firstname' => 'required|max:50',
114
            'lastname'  => 'required|max:50',
115
            'email'     => 'required|email',
116
        ];
117
118 4
        if ($this->isEditing()) {
119 3
            $rules['email'] .= '|unique:users,email,'.$this->getModel()->id;
120 3
            $rules['password'] = 'confirmed';
121
        } else {
122 1
            $rules['email'] .= '|unique:users,email';
123
        }
124
125 4
        return $rules;
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function getRedirectUrl()
132
    {
133
        if ($this->isEditing()) {
134
            return 'administration/users/edit/'.$this->getModel()->id;
135
        }
136
137
        return 'administration/users/add/';
138
    }
139
}
140