Completed
Push — develop ( f06541...810a8c )
by Mohamed
11:09 queued 05:53
created

Project::fields()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 70
Code Lines 50

Duplication

Lines 13
Ratio 18.57 %

Code Coverage

Tests 44
CRAP Score 4.0169

Importance

Changes 7
Bugs 2 Features 1
Metric Value
c 7
b 2
f 1
dl 13
loc 70
ccs 44
cts 49
cp 0.898
rs 8.7445
cc 4
eloc 50
nc 4
nop 0
crap 4.0169

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 2
                ],
39 2
            ];
40
        }
41
42
        return [
43 1
            'submit' => 'create_project',
44 1
        ];
45
    }
46
47
    /**
48
     * @return array
49
     */
50 3
    public function fields()
51
    {
52
        $fields = [
53
            'name' => [
54 3
                'type'  => 'text',
55 3
                'label' => 'name',
56 3
            ],
57
            'private' => [
58 3
                'type'    => 'select',
59 3
                'label'   => 'visibility',
60 3
                'options' => [ProjectModel::PRIVATE_YES => trans('tinyissue.private'), ProjectModel::PRIVATE_NO => trans('tinyissue.public')],
61 3
            ],
62
            'default_assignee' => [
63 3
                'type' => 'hidden',
64 3
                'id'   => 'default_assignee-id',
65 3
            ],
66 3
        ];
67
68
        // On create project can assign users
69
        // On edit project can change status or default assignee
70 3
        if (!$this->isEditing()) {
71 1
            $fields['user'] = [
72 1
                'type'        => 'selectUser',
73 1
                'label'       => 'assign_users',
74 1
                'id'          => 'add-user-project',
75 1
                'placeholder' => trans('tinyissue.assign_a_user'),
76
            ];
77 1
        } else {
78 2
            $fields['status'] = [
79 2
                'type'    => 'select',
80 2
                'label'   => 'status',
81 2
                'options' => [ProjectModel::STATUS_OPEN => trans('tinyissue.open'), ProjectModel::STATUS_ARCHIVED => trans('tinyissue.archived')],
82
            ];
83 2
            $fields['default_assignee'] = [
84 2
                'type'    => 'select',
85 2
                'label'   => 'default_assignee',
86 2
                'options' => [0 => ''] + $this->getModel()->users()->get()->lists('fullname', 'id')->all(),
87
            ];
88
        }
89
90 3
        $fields['kanban_board'] = [
91 3
            'type' => 'legend',
92
        ];
93
94 3
        $statusTags = (new TagModel())->getStatusTags()->tags()->get()->implode('fullname', ', ');
95 3 View Code Duplication
        if ($this->isEditing()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
            $selectTags = $this->getModel()->kanbanTags()->get()->filter(function (TagModel $tag) {
97
                return !($tag->name == TagModel::STATUS_OPEN || $tag->name == TagModel::STATUS_CLOSED);
98 2
            })->map(function (TagModel $tag) {
99
                return [
100
                    'value'   => $tag->id,
101
                    'label'   => ($tag->fullname),
102
                    'bgcolor' => $tag->bgcolor,
103
                ];
104 2
            })->toJson();
105 2
        } else {
106 1
            $selectTags = (new TagModel())->tagsToJson(\Request::input('tags'));
107
        }
108 3
        $fields['columns'] = [
109 3
            'type'        => 'text',
110 3
            'label'       => 'columns',
111 3
            'placeholder' => trans('tinyissue.tags'),
112 3
            'multiple'    => true,
113 3
            'class'       => 'tagit',
114 3
            'help'        => trans('tinyissue.columns_help', ['status' => $statusTags]),
115 3
            'data_tokens' => htmlentities($selectTags, ENT_QUOTES),
116
        ];
117
118 3
        return $fields;
119
    }
120
121
    /**
122
     * @return array
123
     */
124 3
    public function rules()
125
    {
126
        $rules = [
127 3
            'name' => 'required|max:250',
128 3
            'user' => 'array|min:1',
129 3
        ];
130
131 3
        return $rules;
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getRedirectUrl()
138
    {
139
        if ($this->isEditing()) {
140
            return $this->getModel()->to('edit');
141
        }
142
143
        return 'projects/new';
144
    }
145
}
146