We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 50 |
Total Lines | 358 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 1 |
Complex classes like Read often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Read, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | trait Read |
||
12 | { |
||
13 | /** |
||
14 | * Find and retrieve the id of the current entry. |
||
15 | * |
||
16 | * @return int|bool The id in the db or false. |
||
17 | */ |
||
18 | public function getCurrentEntryId() |
||
19 | { |
||
20 | if ($this->entry) { |
||
21 | return $this->entry->getKey(); |
||
22 | } |
||
23 | |||
24 | $params = \Route::current()->parameters(); |
||
25 | |||
26 | return // use the entity name to get the current entry |
||
27 | // this makes sure the ID is corrent even for nested resources |
||
28 | $this->getRequest()->input($this->entity_name) ?? |
||
|
|||
29 | // otherwise use the next to last parameter |
||
30 | array_values($params)[count($params) - 1] ?? |
||
31 | // otherwise return false |
||
32 | false; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Find and retrieve the current entry. |
||
37 | * |
||
38 | * @return \Illuminate\Database\Eloquent\Model|bool The row in the db or false. |
||
39 | */ |
||
40 | public function getCurrentEntry() |
||
41 | { |
||
42 | $id = $this->getCurrentEntryId(); |
||
43 | |||
44 | if ($id === false) { |
||
45 | return false; |
||
46 | } |
||
47 | |||
48 | return $this->getEntry($id); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Find and retrieve an entry in the database or fail. |
||
53 | * |
||
54 | * @param int The id of the row in the db to fetch. |
||
55 | * @return \Illuminate\Database\Eloquent\Model The row in the db. |
||
56 | */ |
||
57 | public function getEntry($id) |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Find and retrieve an entry in the database or fail. |
||
69 | * When found, make sure we set the Locale on it. |
||
70 | * |
||
71 | * @param int The id of the row in the db to fetch. |
||
72 | * @return \Illuminate\Database\Eloquent\Model The row in the db. |
||
73 | */ |
||
74 | public function getEntryWithLocale($id) |
||
75 | { |
||
76 | if (! $this->entry) { |
||
77 | $this->entry = $this->getEntry($id); |
||
78 | } |
||
79 | |||
80 | if ($this->entry->translationEnabled()) { |
||
81 | $locale = request('_locale', \App::getLocale()); |
||
82 | if (in_array($locale, array_keys($this->entry->getAvailableLocales()))) { |
||
83 | $this->entry->setLocale($locale); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | return $this->entry; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Return a Model builder instance with the current crud query applied. |
||
92 | * |
||
93 | * @return \Illuminate\Database\Eloquent\Builder |
||
94 | */ |
||
95 | public function getModelWithCrudPanelQuery() |
||
96 | { |
||
97 | return $this->model->setQuery($this->query->getQuery()); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Find and retrieve an entry in the database or fail. |
||
102 | * |
||
103 | * @param int The id of the row in the db to fetch. |
||
104 | * @return \Illuminate\Database\Eloquent\Model The row in the db. |
||
105 | */ |
||
106 | public function getEntryWithoutFakes($id) |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Make the query JOIN all relationships used in the columns, too, |
||
113 | * so there will be less database queries overall. |
||
114 | */ |
||
115 | public function autoEagerLoadRelationshipColumns() |
||
116 | { |
||
117 | $relationships = $this->getColumnsRelationships(); |
||
118 | |||
119 | foreach ($relationships as $relation) { |
||
120 | if (strpos($relation, '.') !== false) { |
||
121 | $parts = explode('.', $relation); |
||
122 | $model = $this->model; |
||
123 | |||
124 | // Iterate over each relation part to find the valid relations without attributes |
||
125 | // We should eager load the relation but not the attribute |
||
126 | foreach ($parts as $i => $part) { |
||
127 | try { |
||
128 | $model = $model->$part()->getRelated(); |
||
129 | } catch (Exception $e) { |
||
130 | $relation = join('.', array_slice($parts, 0, $i)); |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | $this->with($relation); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Get all entries from the database. |
||
140 | * |
||
141 | * @return array|\Illuminate\Database\Eloquent\Collection |
||
142 | */ |
||
143 | public function getEntries() |
||
144 | { |
||
145 | $this->autoEagerLoadRelationshipColumns(); |
||
146 | |||
147 | $entries = $this->query->get(); |
||
148 | |||
149 | // add the fake columns for each entry |
||
150 | foreach ($entries as $key => $entry) { |
||
151 | $entry->addFakes($this->getFakeColumnsAsArray()); |
||
152 | } |
||
153 | |||
154 | return $entries; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Enable the DETAILS ROW functionality:. |
||
159 | * |
||
160 | * In the table view, show a plus sign next to each entry. |
||
161 | * When clicking that plus sign, an AJAX call will bring whatever content you want from the EntityCrudController::showDetailsRow($id) and show it to the user. |
||
162 | */ |
||
163 | public function enableDetailsRow() |
||
164 | { |
||
165 | if (! backpack_pro()) { |
||
166 | throw new BackpackProRequiredException('Details row'); |
||
167 | } |
||
168 | |||
169 | $this->setOperationSetting('detailsRow', true); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Disable the DETAILS ROW functionality:. |
||
174 | */ |
||
175 | public function disableDetailsRow() |
||
176 | { |
||
177 | $this->setOperationSetting('detailsRow', false); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Add two more columns at the beginning of the ListEntrie table: |
||
182 | * - one shows the checkboxes needed for bulk actions |
||
183 | * - one is blank, in order for evenual detailsRow or expand buttons |
||
184 | * to be in a separate column. |
||
185 | */ |
||
186 | public function enableBulkActions() |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Remove the two columns needed for bulk actions. |
||
193 | */ |
||
194 | public function disableBulkActions() |
||
195 | { |
||
196 | $this->setOperationSetting('bulkActions', false); |
||
197 | |||
198 | $this->removeColumn('bulk_actions'); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Set the number of rows that should be show on the list view. |
||
203 | */ |
||
204 | public function setDefaultPageLength($value) |
||
205 | { |
||
206 | $this->abortIfInvalidPageLength($value); |
||
207 | |||
208 | $this->setOperationSetting('defaultPageLength', $value); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Get the number of rows that should be show on the list view. |
||
213 | * |
||
214 | * @return int |
||
215 | */ |
||
216 | public function getDefaultPageLength() |
||
217 | { |
||
218 | return $this->getOperationSetting('defaultPageLength') ?? config('backpack.crud.operations.list.defaultPageLength') ?? 25; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * If a custom page length was specified as default, make sure it |
||
223 | * also show up in the page length menu. |
||
224 | */ |
||
225 | public function addCustomPageLengthToPageLengthMenu() |
||
226 | { |
||
227 | $values = $this->getOperationSetting('pageLengthMenu')[0]; |
||
228 | $labels = $this->getOperationSetting('pageLengthMenu')[1]; |
||
229 | |||
230 | if (array_search($this->getDefaultPageLength(), $values) === false) { |
||
231 | for ($i = 0; $i < count($values); $i++) { |
||
232 | if ($values[$i] > $this->getDefaultPageLength() || $values[$i] === -1) { |
||
233 | array_splice($values, $i, 0, $this->getDefaultPageLength()); |
||
234 | array_splice($labels, $i, 0, $this->getDefaultPageLength()); |
||
235 | break; |
||
236 | } |
||
237 | if ($i === count($values) - 1) { |
||
238 | $values[] = $this->getDefaultPageLength(); |
||
239 | $labels[] = $this->getDefaultPageLength(); |
||
240 | break; |
||
241 | } |
||
242 | } |
||
243 | } |
||
244 | |||
245 | $this->setOperationSetting('pageLengthMenu', [$values, $labels]); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Specify array of available page lengths on the list view. |
||
250 | * |
||
251 | * @param array|int $menu |
||
252 | * |
||
253 | * https://backpackforlaravel.com/docs/4.1/crud-cheat-sheet#page-length |
||
254 | */ |
||
255 | public function setPageLengthMenu($menu) |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Builds the menu from the given array. It works out with two different types of arrays: |
||
283 | * [1, 2, 3] AND [1 => 'one', 2 => 'two', 3 => 'three']. |
||
284 | * |
||
285 | * @param array $menu |
||
286 | * @return array |
||
287 | */ |
||
288 | private function buildPageLengthMenuFromArray($menu) |
||
289 | { |
||
290 | // check if the values of the array are strings, in case developer defined: |
||
291 | // setPageLengthMenu([0 => 'f', 100 => 'h', 300 => 't']) |
||
292 | if (count(array_filter(array_values($menu), 'is_string')) > 0) { |
||
293 | $values = array_keys($menu); |
||
294 | $labels = array_values($menu); |
||
295 | |||
296 | $this->abortIfInvalidPageLength($values); |
||
297 | |||
298 | return [$values, $labels]; |
||
299 | } else { |
||
300 | // developer defined length as setPageLengthMenu([50, 100, 300]) |
||
301 | // we will use the same values as labels |
||
302 | $this->abortIfInvalidPageLength($menu); |
||
303 | |||
304 | return [$menu, $menu]; |
||
305 | } |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Get page length menu for the list view. |
||
310 | * |
||
311 | * @return array |
||
312 | */ |
||
313 | public function getPageLengthMenu() |
||
314 | { |
||
315 | // if we have a 2D array, update all the values in the right hand array to their translated values |
||
316 | if (isset($this->getOperationSetting('pageLengthMenu')[1]) && is_array($this->getOperationSetting('pageLengthMenu')[1])) { |
||
317 | $aux = $this->getOperationSetting('pageLengthMenu'); |
||
318 | foreach ($this->getOperationSetting('pageLengthMenu')[1] as $key => $val) { |
||
319 | $aux[1][$key] = trans($val); |
||
320 | } |
||
321 | $this->setOperationSetting('pageLengthMenu', $aux); |
||
322 | } |
||
323 | $this->addCustomPageLengthToPageLengthMenu(); |
||
324 | |||
325 | return $this->getOperationSetting('pageLengthMenu'); |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Checks if the provided PageLength segment is valid. |
||
330 | * |
||
331 | * @param array|int $value |
||
332 | * @return void |
||
333 | */ |
||
334 | private function abortIfInvalidPageLength($value) |
||
335 | { |
||
336 | if ($value === 0 || (is_array($value) && in_array(0, $value))) { |
||
337 | abort(500, 'You should not use 0 as a key in paginator. If you are looking for "ALL" option, use -1 instead.'); |
||
338 | } |
||
339 | } |
||
340 | |||
341 | /* |
||
342 | |-------------------------------------------------------------------------- |
||
343 | | EXPORT BUTTONS |
||
344 | |-------------------------------------------------------------------------- |
||
345 | */ |
||
346 | |||
347 | /** |
||
348 | * Tell the list view to show the DataTables export buttons. |
||
349 | */ |
||
350 | public function enableExportButtons() |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Check if export buttons are enabled for the table view. |
||
363 | * |
||
364 | * @return bool |
||
365 | */ |
||
366 | public function exportButtons() |
||
367 | { |
||
368 | return $this->getOperationSetting('exportButtons') ?? false; |
||
369 | } |
||
370 | } |
||
371 |