We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 54 |
Total Lines | 373 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 2 | Features | 2 |
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 |
||
12 | trait Read |
||
13 | { |
||
14 | /** |
||
15 | * Find and retrieve the id of the current entry. |
||
16 | * |
||
17 | * @return int|bool The id in the db or false. |
||
18 | */ |
||
19 | public function getCurrentEntryId() |
||
20 | { |
||
21 | if ($this->entry) { |
||
22 | return $this->entry->getKey(); |
||
23 | } |
||
24 | |||
25 | $params = Route::current()?->parameters() ?? []; |
||
26 | |||
27 | return // use the entity name to get the current entry |
||
28 | // this makes sure the ID is corrent even for nested resources |
||
29 | $this->getRequest()->input($this->entity_name) ?? |
||
|
|||
30 | // otherwise use the next to last parameter |
||
31 | array_values($params)[count($params) - 1] ?? |
||
32 | // otherwise return false |
||
33 | false; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Find and retrieve the current entry. |
||
38 | * |
||
39 | * @return \Illuminate\Database\Eloquent\Model|bool The row in the db or false. |
||
40 | */ |
||
41 | public function getCurrentEntry() |
||
50 | } |
||
51 | |||
52 | public function getCurrentEntryWithLocale() |
||
53 | { |
||
54 | $entry = $this->getCurrentEntry(); |
||
55 | |||
56 | if (! $entry) { |
||
57 | return false; |
||
58 | } |
||
59 | |||
60 | return $this->setLocaleOnModel($entry); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Find and retrieve an entry in the database or fail. |
||
65 | * |
||
66 | * @param int The id of the row in the db to fetch. |
||
67 | * @return \Illuminate\Database\Eloquent\Model The row in the db. |
||
68 | */ |
||
69 | public function getEntry($id) |
||
77 | } |
||
78 | |||
79 | private function shouldUseFallbackLocale() |
||
80 | { |
||
81 | if ($this->getOperationSetting('useFallbackLocale') !== null) { |
||
82 | return $this->getOperationSetting('useFallbackLocale'); |
||
83 | } |
||
84 | |||
85 | $fallbackRequestValue = $this->getRequest()->get('_use_fallback'); |
||
86 | |||
87 | return $fallbackRequestValue === 'true' ? true : (in_array($fallbackRequestValue, array_keys(config('backpack.crud.locales'))) ? $fallbackRequestValue : false); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Find and retrieve an entry in the database or fail. |
||
92 | * When found, make sure we set the Locale on it. |
||
93 | * |
||
94 | * @param int The id of the row in the db to fetch. |
||
95 | * @return \Illuminate\Database\Eloquent\Model The row in the db. |
||
96 | */ |
||
97 | public function getEntryWithLocale($id) |
||
98 | { |
||
99 | if (! $this->entry) { |
||
100 | $this->entry = $this->getEntry($id); |
||
101 | } |
||
102 | |||
103 | return $this->setLocaleOnModel($this->entry); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Return a Model builder instance with the current crud query applied. |
||
108 | * |
||
109 | * @return \Illuminate\Database\Eloquent\Builder |
||
110 | */ |
||
111 | public function getModelWithCrudPanelQuery() |
||
112 | { |
||
113 | return $this->model->setQuery($this->query->getQuery()); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Find and retrieve an entry in the database or fail. |
||
118 | * |
||
119 | * @param int The id of the row in the db to fetch. |
||
120 | * @return \Illuminate\Database\Eloquent\Model The row in the db. |
||
121 | */ |
||
122 | public function getEntryWithoutFakes($id) |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Make the query JOIN all relationships used in the columns, too, |
||
129 | * so there will be less database queries overall. |
||
130 | */ |
||
131 | public function autoEagerLoadRelationshipColumns() |
||
132 | { |
||
133 | $relationships = $this->getColumnsRelationships(); |
||
134 | |||
135 | foreach ($relationships as $relation) { |
||
136 | if (strpos($relation, '.') !== false) { |
||
137 | $parts = explode('.', $relation); |
||
138 | $model = $this->model; |
||
139 | |||
140 | // Iterate over each relation part to find the valid relations without attributes |
||
141 | // We should eager load the relation but not the attribute |
||
142 | foreach ($parts as $i => $part) { |
||
143 | try { |
||
144 | $model = $model->$part()->getRelated(); |
||
145 | } catch (Exception $e) { |
||
146 | $relation = implode('.', array_slice($parts, 0, $i)); |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | $this->with($relation); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Get all entries from the database. |
||
156 | * |
||
157 | * @return array|\Illuminate\Database\Eloquent\Collection |
||
158 | */ |
||
159 | public function getEntries() |
||
160 | { |
||
161 | $this->autoEagerLoadRelationshipColumns(); |
||
162 | |||
163 | $entries = $this->query->get(); |
||
164 | |||
165 | // add the fake columns for each entry |
||
166 | foreach ($entries as $key => $entry) { |
||
167 | $entry->addFakes($this->getFakeColumnsAsArray()); |
||
168 | } |
||
169 | |||
170 | return $entries; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Enable the DETAILS ROW functionality:. |
||
175 | * |
||
176 | * In the table view, show a plus sign next to each entry. |
||
177 | * 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. |
||
178 | */ |
||
179 | public function enableDetailsRow() |
||
180 | { |
||
181 | if (! backpack_pro()) { |
||
182 | throw new BackpackProRequiredException('Details row'); |
||
183 | } |
||
184 | |||
185 | $this->setOperationSetting('detailsRow', true); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Disable the DETAILS ROW functionality:. |
||
190 | */ |
||
191 | public function disableDetailsRow() |
||
192 | { |
||
193 | $this->setOperationSetting('detailsRow', false); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Add two more columns at the beginning of the ListEntrie table: |
||
198 | * - one shows the checkboxes needed for bulk actions |
||
199 | * - one is blank, in order for evenual detailsRow or expand buttons |
||
200 | * to be in a separate column. |
||
201 | */ |
||
202 | public function enableBulkActions() |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Remove the two columns needed for bulk actions. |
||
209 | */ |
||
210 | public function disableBulkActions() |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Set the number of rows that should be show on the list view. |
||
219 | */ |
||
220 | public function setDefaultPageLength($value) |
||
221 | { |
||
222 | $this->abortIfInvalidPageLength($value); |
||
223 | |||
224 | $this->setOperationSetting('defaultPageLength', $value); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Get the number of rows that should be show on the list view. |
||
229 | * |
||
230 | * @return int |
||
231 | */ |
||
232 | public function getDefaultPageLength() |
||
233 | { |
||
234 | return $this->getOperationSetting('defaultPageLength') ?? config('backpack.crud.operations.list.defaultPageLength') ?? 25; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * If a custom page length was specified as default, make sure it |
||
239 | * also show up in the page length menu. |
||
240 | */ |
||
241 | public function addCustomPageLengthToPageLengthMenu() |
||
242 | { |
||
243 | $values = $this->getOperationSetting('pageLengthMenu')[0]; |
||
244 | $labels = $this->getOperationSetting('pageLengthMenu')[1]; |
||
245 | |||
246 | if (array_search($this->getDefaultPageLength(), $values) === false) { |
||
247 | for ($i = 0; $i < count($values); $i++) { |
||
248 | if ($values[$i] > $this->getDefaultPageLength() || $values[$i] === -1) { |
||
249 | array_splice($values, $i, 0, $this->getDefaultPageLength()); |
||
250 | array_splice($labels, $i, 0, $this->getDefaultPageLength()); |
||
251 | break; |
||
252 | } |
||
253 | if ($i === count($values) - 1) { |
||
254 | $values[] = $this->getDefaultPageLength(); |
||
255 | $labels[] = $this->getDefaultPageLength(); |
||
256 | break; |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | |||
261 | $this->setOperationSetting('pageLengthMenu', [$values, $labels]); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Specify array of available page lengths on the list view. |
||
266 | * |
||
267 | * @param array|int $menu |
||
268 | * |
||
269 | * https://backpackforlaravel.com/docs/4.1/crud-cheat-sheet#page-length |
||
270 | */ |
||
271 | public function setPageLengthMenu($menu) |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Builds the menu from the given array. It works out with two different types of arrays: |
||
299 | * [1, 2, 3] AND [1 => 'one', 2 => 'two', 3 => 'three']. |
||
300 | * |
||
301 | * @param array $menu |
||
302 | * @return array |
||
303 | */ |
||
304 | private function buildPageLengthMenuFromArray($menu) |
||
305 | { |
||
306 | // check if the values of the array are strings, in case developer defined: |
||
307 | // setPageLengthMenu([0 => 'f', 100 => 'h', 300 => 't']) |
||
308 | if (count(array_filter(array_values($menu), 'is_string')) > 0) { |
||
309 | $values = array_keys($menu); |
||
310 | $labels = array_values($menu); |
||
311 | |||
312 | $this->abortIfInvalidPageLength($values); |
||
313 | |||
314 | return [$values, $labels]; |
||
315 | } else { |
||
316 | // developer defined length as setPageLengthMenu([50, 100, 300]) |
||
317 | // we will use the same values as labels |
||
318 | $this->abortIfInvalidPageLength($menu); |
||
319 | |||
320 | return [$menu, $menu]; |
||
321 | } |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Get page length menu for the list view. |
||
326 | * |
||
327 | * @return array |
||
328 | */ |
||
329 | public function getPageLengthMenu() |
||
330 | { |
||
331 | // if we have a 2D array, update all the values in the right hand array to their translated values |
||
332 | if (isset($this->getOperationSetting('pageLengthMenu')[1]) && is_array($this->getOperationSetting('pageLengthMenu')[1])) { |
||
333 | $aux = $this->getOperationSetting('pageLengthMenu'); |
||
334 | foreach ($this->getOperationSetting('pageLengthMenu')[1] as $key => $val) { |
||
335 | $aux[1][$key] = trans($val); |
||
336 | } |
||
337 | $this->setOperationSetting('pageLengthMenu', $aux); |
||
338 | } |
||
339 | $this->addCustomPageLengthToPageLengthMenu(); |
||
340 | |||
341 | return $this->getOperationSetting('pageLengthMenu'); |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Checks if the provided PageLength segment is valid. |
||
346 | * |
||
347 | * @param array|int $value |
||
348 | * @return void |
||
349 | */ |
||
350 | private function abortIfInvalidPageLength($value) |
||
351 | { |
||
352 | if ($value === 0 || (is_array($value) && in_array(0, $value))) { |
||
353 | abort(500, 'You should not use 0 as a key in paginator. If you are looking for "ALL" option, use -1 instead.'); |
||
354 | } |
||
355 | } |
||
356 | |||
357 | /* |
||
358 | |-------------------------------------------------------------------------- |
||
359 | | EXPORT BUTTONS |
||
360 | |-------------------------------------------------------------------------- |
||
361 | */ |
||
362 | |||
363 | /** |
||
364 | * Tell the list view to show the DataTables export buttons. |
||
365 | */ |
||
366 | public function enableExportButtons() |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Check if export buttons are enabled for the table view. |
||
379 | * |
||
380 | * @return bool |
||
381 | */ |
||
382 | public function exportButtons() |
||
383 | { |
||
385 | } |
||
386 | } |
||
387 |