Completed
Push — master ( 195b94...7a36f1 )
by Mohamed
04:50
created

Project::fields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 52
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 52
ccs 23
cts 23
cp 1
rs 9.4929
c 2
b 0
f 0
cc 2
eloc 34
nc 2
nop 0
crap 2

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
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\Project as ProjectModel;
15
use Tinyissue\Model\Tag as TagModel;
16
17
/**
18
 * Project is a class to defines fields & rules for add/edit project form.
19
 *
20
 * @author Mohamed Alsharaf <[email protected]>
21
 */
22
class Project extends FormAbstract
23
{
24
    /**
25
     * @return array
26
     */
27 3
    public function actions()
28
    {
29 3
        if ($this->isEditing()) {
30
            return [
31 2
                'submit' => 'update',
32
                'delete' => [
33 2
                    'type'         => 'danger_submit',
34 2
                    'label'        => trans('tinyissue.delete_something', ['name' => $this->getModel()->name]),
35 2
                    'class'        => 'delete-project',
36 2
                    'name'         => 'delete-project',
37 2
                    'data-message' => trans('tinyissue.delete_project_confirm'),
38
                ],
39
            ];
40
        }
41
42
        return [
43 1
            'submit' => 'create_project',
44
        ];
45
    }
46
47
    /**
48
     * @return array
49
     */
50 3
    public function fields()
51
    {
52
        $fields = [
53
            'name' => [
54
                'type'  => 'text',
55
                'label' => 'name',
56 3
            ],
57
            'private' => [
58 3
                'type'    => 'select',
59 3
                'label'   => 'visibility',
60 3
                'options' => [
61
                    ProjectModel::INTERNAL_YES => trans('tinyissue.internal'),
62
                    ProjectModel::PRIVATE_YES  => trans('tinyissue.private'),
63
                    ProjectModel::PRIVATE_NO   => trans('tinyissue.public'),
64
                ],
65
            ],
66
            'default_assignee' => [
67
                'type' => 'hidden',
68
                'id'   => 'default_assignee-id',
69
            ],
70 3
        ];
71 1
72 1
        // On create project can assign users
73 1
        // On edit project can change status or default assignee
74 1
        if (!$this->isEditing()) {
75 1
            $fields['user'] = [
76
                'type'        => 'selectUser',
77
                'label'       => 'assign_users',
78 2
                'id'          => 'add-user-project',
79 2
                'placeholder' => trans('tinyissue.assign_a_user'),
80 2
            ];
81 2
        } else {
82
            $fields['status'] = [
83 2
                'type'    => 'select',
84 2
                'label'   => 'status',
85 2
                'options' => [ProjectModel::STATUS_OPEN => trans('tinyissue.open'), ProjectModel::STATUS_ARCHIVED => trans('tinyissue.archived')],
86 2
            ];
87
            $fields['default_assignee'] = [
88
                'type'    => 'select',
89
                'label'   => 'default_assignee',
90 3
                'options' => [0 => ''] + $this->getModel()->usersCanFixIssue()->get()->lists('fullname', 'id')->all(),
91
            ];
92
        }
93
94 3
        $fields['kanban_board'] = [
95 3
            'type' => 'legend',
96
        ];
97
98 2
        $fields += $this->getKanbanColumnsField();
99
100
        return $fields;
101
    }
102
103
    /**
104 2
     * Return Kanban columns field.
105
     *
106 1
     * @return array
107
     */
108 3
    protected function getKanbanColumnsField()
109 3
    {
110 3
        $fields = [];
111 3
112
        // All of the status tags
113 3
        $statusTags = (new TagModel())->getStatusTags()->get();
114 3
115 3
        // Get selected status tags on editing a project
116
        $selectTags = [];
117
        if ($this->isEditing()) {
118 3
            $selectTags = $this->getModel()->kanbanTags()->get()->lists('id');
119
        }
120
121
        // An array for checkboxes
122
        $options = [];
123
        foreach ($selectTags as $tagId) {
124 3
            $tag = $statusTags->find($tagId);
125
            if ($tag) {
126
                $options[ucwords($tag->name)] = $this->getKanbanColumnField($tag, true);
127 3
            }
128
        }
129
130
        foreach ($statusTags as $tag) {
131 3
            if (!isset($options[ucwords($tag->name)])) {
132
                $options[ucwords($tag->name)] = $this->getKanbanColumnField($tag);
133
            }
134
        }
135
136
        // The checkbox button element
137
        $fields['columns[]'] = [
138
            'label'      => 'columns',
139
            'type'       => 'checkboxButton',
140
            'checkboxes' => $options,
141
            'grouped'    => true,
142
        ];
143
144
        return $fields;
145
    }
146
147
    /**
148
     * Returns an array structure for a checkbox button in the kanban field.
149
     *
150
     * @param TagModel $tag
151
     * @param bool     $checked
152
     *
153
     * @return array
154
     */
155
    protected function getKanbanColumnField(TagModel $tag, $checked = false)
156
    {
157
        return [
158
            'value'     => $tag->id,
159
            'data-tags' => $tag->id,
160
            'color'     => $tag->bgcolor,
161
            'checked'   => $checked,
162
        ];
163
    }
164
165
    /**
166
     * @return array
167
     */
168
    public function rules()
169
    {
170
        $rules = [
171
            'name' => 'required|max:250',
172
            'user' => 'array|min:1',
173
        ];
174
175
        return $rules;
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    public function getRedirectUrl()
182
    {
183
        if ($this->isEditing()) {
184
            return $this->getModel()->to('edit');
185
        }
186
187
        return 'projects/new';
188
    }
189
}
190