Conditions | 10 |
Paths | 160 |
Total Lines | 199 |
Code Lines | 107 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
27 | public function setup() |
||
28 | { |
||
29 | |||
30 | /* |
||
31 | |-------------------------------------------------------------------------- |
||
32 | | BASIC CRUD INFORMATION |
||
33 | |-------------------------------------------------------------------------- |
||
34 | */ |
||
35 | |||
36 | $project_id = Route::current()->parameter('project_id') ?? Route::current()->parameter('project'); |
||
37 | |||
38 | $this->crud->setModel(Release::class); |
||
39 | $this->crud->setEntityNameStrings('Release', 'Releases'); |
||
40 | $this->crud->setRoute(config('backpack.base.route_prefix') . '/projects/' . $project_id . '/releases'); |
||
|
|||
41 | |||
42 | if ($project_id) { |
||
43 | $project = Project::findOrFail($project_id); |
||
44 | $this->crud->addClause('where', 'agent_id', $project_id); |
||
45 | Vocabulary::addGlobalScope('project_id', function (Builder $builder) use ($project_id) { |
||
46 | $builder->where('agent_id', $project_id); |
||
47 | }); |
||
48 | Elementset::addGlobalScope('project_id', function (Builder $builder) use ($project_id) { |
||
49 | $builder->where('agent_id', $project_id); |
||
50 | }); |
||
51 | } |
||
52 | |||
53 | /* |
||
54 | |-------------------------------------------------------------------------- |
||
55 | | BASIC CRUD INFORMATION |
||
56 | |-------------------------------------------------------------------------- |
||
57 | */ |
||
58 | |||
59 | $this->crud->setFromDb(); |
||
60 | |||
61 | // ------ CRUD FIELDS |
||
62 | // $this->crud->addField($options, 'update/create/both'); |
||
63 | // $this->crud->addFields($array_of_arrays, 'update/create/both'); |
||
64 | // $this->crud->removeField('name', 'update/create/both'); |
||
65 | $this->crud->removeFields(['user_id', 'agent_id', 'name', 'body', 'github_response', 'github_id', 'tag_name', 'target_commitish', 'is_draft', 'is_prerelease', 'published_at']); |
||
66 | $this->crud->addFields([ |
||
67 | [ |
||
68 | 'name' => 'user_id', |
||
69 | 'type' => 'hidden', |
||
70 | 'default' => request()->user() ? request()->user()->id : null, |
||
71 | ], |
||
72 | [ |
||
73 | 'name' => 'agent_id', |
||
74 | 'type' => 'hidden', |
||
75 | 'default' => $project_id, |
||
76 | ], |
||
77 | [ |
||
78 | 'name' => 'name', |
||
79 | 'label' => 'Release Title', |
||
80 | 'type' => 'text', |
||
81 | 'attributes' => [ |
||
82 | 'placeholder' => 'The descriptive title of this release', |
||
83 | ], |
||
84 | ], |
||
85 | [ |
||
86 | 'name' => 'body', |
||
87 | 'label' => 'Release Notes', |
||
88 | 'type' => 'textarea', |
||
89 | 'attributes' => [ |
||
90 | 'placeholder' => 'Add some description of the changes in this release', |
||
91 | 'rows' => 10, |
||
92 | ], |
||
93 | ], |
||
94 | [ |
||
95 | 'name' => 'tag_name', |
||
96 | 'label' => 'Tag Name', |
||
97 | 'type' => 'text', |
||
98 | 'attributes' => [ |
||
99 | 'placeholder' => 'The name of the GIT tag to assign to this release', |
||
100 | ], |
||
101 | ], |
||
102 | [ |
||
103 | 'name' => 'is_prerelease', |
||
104 | 'label' => 'Pre-Release', |
||
105 | 'type' => 'checkbox', |
||
106 | 'hint' => 'Notify consuming systems that this release is not final', |
||
107 | |||
108 | ], |
||
109 | [ |
||
110 | 'label' => 'Value Vocabularies to publish...', |
||
111 | 'type' => 'select_multiple', |
||
112 | 'name' => 'vocabularies', // the method that defines the relationship in your Model |
||
113 | 'entity' => 'vocabularies', // the method that defines the relationship in your Model |
||
114 | 'attribute' => 'name', // foreign key attribute that is shown to user |
||
115 | 'model' => Vocabulary::class, // foreign key model |
||
116 | 'pivot' => true, // on create&update, do you need to add/delete pivot table entries? |
||
117 | 'pivotFields' => [], // an array of pivot table fields |
||
118 | 'morph' => true, |
||
119 | 'allows_null' => false, |
||
120 | 'hint' => 'Use shift- or ctrl-click (Windows) or cmd-click (mac) to select.', |
||
121 | ], |
||
122 | [ |
||
123 | 'label' => 'Element Sets to publish...', |
||
124 | 'type' => 'select_multiple', |
||
125 | 'name' => 'elementsets', // the method that defines the relationship in your Model |
||
126 | 'entity' => 'elementsets', // the method that defines the relationship in your Model |
||
127 | 'attribute' => 'name', // foreign key attribute that is shown to user |
||
128 | 'model' => Elementset::class, // foreign key model |
||
129 | 'pivot' => true, // on create&update, do you need to add/delete pivot table entries? |
||
130 | 'pivotFields' => [], // an array of pivot table fields |
||
131 | 'morph' => true, |
||
132 | 'allows_null' => false, |
||
133 | 'hint' => 'Use shift- or ctrl-click (Windows) or cmd-click (mac) to select.', |
||
134 | ], |
||
135 | ]); |
||
136 | |||
137 | // ------ CRUD COLUMNS |
||
138 | // $this->crud->addColumn(); // add a single column, at the end of the stack |
||
139 | // $this->crud->addColumns(); // add multiple columns, at the end of the stack |
||
140 | //$this->crud->removeColumn('github_response'); // remove a column from the stack |
||
141 | $this->crud->removeColumns(['user_id', 'agent_id', 'github_response', 'github_id', 'target_commitish', 'is_draft', 'deleted_at']); // remove an array of columns from the stack |
||
142 | // $this->crud->setColumnDetails('column_name', ['attribute' => 'value']); // adjusts the properties of the passed in column (by name) |
||
143 | $this->crud->addColumns([ |
||
144 | [ |
||
145 | // n-n relationship (with pivot table) |
||
146 | 'label' => 'Vocabularies', // Table column heading |
||
147 | 'type' => 'select_multiple', |
||
148 | 'name' => 'vocabularies', // the method that defines the relationship in your Model |
||
149 | 'entity' => 'vocabularies', // the method that defines the relationship in your Model |
||
150 | 'attribute' => 'name', // foreign key attribute that is shown to user |
||
151 | 'model' => Vocabulary::class, // foreign key model |
||
152 | |||
153 | ], |
||
154 | [ |
||
155 | // n-n relationship (with pivot table) |
||
156 | 'label' => 'Element Sets', // Table column heading |
||
157 | 'type' => 'select_multiple', |
||
158 | 'name' => 'elementsets', // the method that defines the relationship in your Model |
||
159 | 'entity' => 'elementsets', // the method that defines the relationship in your Model |
||
160 | 'attribute' => 'name', // foreign key attribute that is shown to user |
||
161 | 'model' => Elementset::class, // foreign key model |
||
162 | |||
163 | ], |
||
164 | [ |
||
165 | 'label' => 'Released by', // Table column heading |
||
166 | 'type' => 'select', |
||
167 | 'name' => 'user', // the method that defines the relationship in your Model |
||
168 | 'entity' => 'user', // the method that defines the relationship in your Model |
||
169 | 'attribute' => 'name', // foreign key attribute that is shown to user |
||
170 | 'model' => User::class, // foreign key model |
||
171 | |||
172 | ], |
||
173 | ]); |
||
174 | |||
175 | $this->crud->setColumnDetails('is_prerelease', |
||
176 | [ |
||
177 | 'type' => 'boolean', |
||
178 | 'label' => 'Pre-release?', |
||
179 | |||
180 | ]); |
||
181 | $this->crud->setColumnDetails('body', |
||
182 | [ |
||
183 | 'type' => 'markdown', |
||
184 | ]); |
||
185 | |||
186 | $this->crud->setColumnsDetails(['body', 'tag_name'], |
||
187 | [ |
||
188 | 'list' => false, |
||
189 | ]); |
||
190 | |||
191 | // $this->crud->setColumnsDetails(['column_1', 'column_2'], ['attribute' => 'value']); |
||
192 | |||
193 | // ------ CRUD BUTTONS |
||
194 | // possible positions: 'beginning' and 'end'; defaults to 'beginning' for the 'line' stack, 'end' for the others; |
||
195 | $this->crud->addButton('line', 'publish', 'view', 'backpack::crud.buttons.publish', 'end'); // add a button; possible types are: view, model_function |
||
196 | // $this->crud->addButtonFromModelFunction($stack, $name, $model_function_name, $position); // add a button whose HTML is returned by a method in the CRUD model |
||
197 | // $this->crud->addButtonFromView($stack, $name, $view, $position); // add a button whose HTML is in a view placed at resources\views\vendor\backpack\crud\buttons |
||
198 | // $this->crud->removeButton($name); |
||
199 | // $this->crud->removeButtonFromStack($name, $stack); |
||
200 | // $this->crud->removeAllButtons(); |
||
201 | // $this->crud->removeAllButtonsFromStack('line'); |
||
202 | |||
203 | $this->crud->denyAccess(['create', 'update', 'delete', 'import', 'publish']); |
||
204 | if ($project->is_private) { |
||
205 | if (Gate::allows('index', $project)) { |
||
206 | $this->crud->allowAccess(['index']); |
||
207 | } |
||
208 | if (Gate::allows('view', $project)) { |
||
209 | $this->crud->allowAccess(['show']); |
||
210 | } |
||
211 | } else { |
||
212 | $this->crud->allowAccess(['index']); |
||
213 | $this->crud->allowAccess(['show']); |
||
214 | } |
||
215 | if (Gate::allows('create', $project)) { |
||
216 | $this->crud->allowAccess(['create']); |
||
217 | } |
||
218 | if (Gate::allows('update', $project)) { |
||
219 | $this->crud->allowAccess(['update']); |
||
220 | } |
||
221 | if (Gate::allows('delete', $project)) { |
||
222 | $this->crud->allowAccess(['delete']); |
||
223 | } |
||
224 | if (Gate::allows('publish', $project)) { |
||
225 | $this->crud->allowAccess(['publish']); |
||
226 | } |
||
321 |