We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 44 |
| Total Lines | 358 |
| Duplicated Lines | 0 % |
| Changes | 12 | ||
| Bugs | 6 | 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 |
||
| 10 | trait Read |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Find and retrieve the id of the current entry. |
||
| 14 | * |
||
| 15 | * @return int|bool The id in the db or false. |
||
| 16 | */ |
||
| 17 | public function getCurrentEntryId() |
||
| 18 | { |
||
| 19 | if ($this->entry) { |
||
| 20 | return $this->entry->getKey(); |
||
| 21 | } |
||
| 22 | |||
| 23 | $params = \Route::current()->parameters(); |
||
| 24 | |||
| 25 | return // use the entity name to get the current entry |
||
| 26 | // this makes sure the ID is corrent even for nested resources |
||
| 27 | $this->getRequest()->input($this->entity_name) ?? |
||
|
|
|||
| 28 | // otherwise use the next to last parameter |
||
| 29 | array_values($params)[count($params) - 1] ?? |
||
| 30 | // otherwise return false |
||
| 31 | false; |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Find and retrieve the current entry. |
||
| 36 | * |
||
| 37 | * @return \Illuminate\Database\Eloquent\Model|bool The row in the db or false. |
||
| 38 | */ |
||
| 39 | public function getCurrentEntry() |
||
| 40 | { |
||
| 41 | $id = $this->getCurrentEntryId(); |
||
| 42 | |||
| 43 | if (! $id) { |
||
| 44 | return false; |
||
| 45 | } |
||
| 46 | |||
| 47 | return $this->getEntry($id); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Find and retrieve an entry in the database or fail. |
||
| 52 | * |
||
| 53 | * @param int The id of the row in the db to fetch. |
||
| 54 | * |
||
| 55 | * @return \Illuminate\Database\Eloquent\Model The row in the db. |
||
| 56 | */ |
||
| 57 | public function getEntry($id) |
||
| 58 | { |
||
| 59 | if (! $this->entry) { |
||
| 60 | $this->entry = $this->model->findOrFail($id); |
||
| 61 | $this->entry = $this->entry->withFakes(); |
||
| 62 | } |
||
| 63 | |||
| 64 | return $this->entry; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Find and retrieve an entry in the database or fail. |
||
| 69 | * |
||
| 70 | * @param int The id of the row in the db to fetch. |
||
| 71 | * |
||
| 72 | * @return \Illuminate\Database\Eloquent\Model The row in the db. |
||
| 73 | */ |
||
| 74 | public function getEntryWithoutFakes($id) |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Make the query JOIN all relationships used in the columns, too, |
||
| 81 | * so there will be less database queries overall. |
||
| 82 | */ |
||
| 83 | public function autoEagerLoadRelationshipColumns() |
||
| 84 | { |
||
| 85 | $relationships = $this->getColumnsRelationships(); |
||
| 86 | |||
| 87 | foreach ($relationships as $relation) { |
||
| 88 | if (strpos($relation, '.') !== false) { |
||
| 89 | $relation_parts = explode('.', $relation); |
||
| 90 | $last_relation_part = $last_valid_relation_method = end($relation_parts); |
||
| 91 | |||
| 92 | array_reduce(array_splice($relation_parts, 0, count($relation_parts)), function ($obj, $method) use (&$last_valid_relation_method) { |
||
| 93 | try { |
||
| 94 | $result = $obj->$method(); |
||
| 95 | $last_valid_relation_method = $method; |
||
| 96 | |||
| 97 | return $result->getRelated(); |
||
| 98 | } catch (Exception $e) { |
||
| 99 | return; |
||
| 100 | } |
||
| 101 | }, $this->model); |
||
| 102 | |||
| 103 | // when this keys don't match means the last part of the relation string is the attribute in the relation and |
||
| 104 | // not a nested relation. In that case, we should eager load the relation but not the attribute |
||
| 105 | if ($last_valid_relation_method != $last_relation_part) { |
||
| 106 | // remove the last part of the relation string because it is the attribute in the relationship |
||
| 107 | $relation = substr($relation, 0, strrpos($relation, '.')); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | $this->with($relation); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get all entries from the database. |
||
| 116 | * |
||
| 117 | * @return array|\Illuminate\Database\Eloquent\Collection |
||
| 118 | */ |
||
| 119 | public function getEntries() |
||
| 120 | { |
||
| 121 | $this->autoEagerLoadRelationshipColumns(); |
||
| 122 | |||
| 123 | $entries = $this->query->get(); |
||
| 124 | |||
| 125 | // add the fake columns for each entry |
||
| 126 | foreach ($entries as $key => $entry) { |
||
| 127 | $entry->addFakes($this->getFakeColumnsAsArray()); |
||
| 128 | } |
||
| 129 | |||
| 130 | return $entries; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Enable the DETAILS ROW functionality:. |
||
| 135 | * |
||
| 136 | * In the table view, show a plus sign next to each entry. |
||
| 137 | * 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. |
||
| 138 | */ |
||
| 139 | public function enableDetailsRow() |
||
| 140 | { |
||
| 141 | $this->setOperationSetting('detailsRow', true); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Disable the DETAILS ROW functionality:. |
||
| 146 | */ |
||
| 147 | public function disableDetailsRow() |
||
| 148 | { |
||
| 149 | $this->setOperationSetting('detailsRow', false); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Add two more columns at the beginning of the ListEntrie table: |
||
| 154 | * - one shows the checkboxes needed for bulk actions |
||
| 155 | * - one is blank, in order for evenual detailsRow or expand buttons |
||
| 156 | * to be in a separate column. |
||
| 157 | */ |
||
| 158 | public function enableBulkActions() |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Remove the two columns needed for bulk actions. |
||
| 197 | */ |
||
| 198 | public function disableBulkActions() |
||
| 199 | { |
||
| 200 | $this->setOperationSetting('bulkActions', false); |
||
| 201 | |||
| 202 | $this->removeColumn('bulk_actions'); |
||
| 203 | $this->removeColumn('blank_first_column'); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Set the number of rows that should be show on the list view. |
||
| 208 | */ |
||
| 209 | public function setDefaultPageLength($value) |
||
| 210 | { |
||
| 211 | $this->abortIfInvalidPageLength($value); |
||
| 212 | |||
| 213 | $this->setOperationSetting('defaultPageLength', $value); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get the number of rows that should be show on the list view. |
||
| 218 | * |
||
| 219 | * @return int |
||
| 220 | */ |
||
| 221 | public function getDefaultPageLength() |
||
| 222 | { |
||
| 223 | return $this->getOperationSetting('defaultPageLength') ?? config('backpack.crud.operations.list.defaultPageLength') ?? 25; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * If a custom page length was specified as default, make sure it |
||
| 228 | * also show up in the page length menu. |
||
| 229 | */ |
||
| 230 | public function addCustomPageLengthToPageLengthMenu() |
||
| 231 | { |
||
| 232 | $values = $this->getOperationSetting('pageLengthMenu')[0]; |
||
| 233 | $labels = $this->getOperationSetting('pageLengthMenu')[1]; |
||
| 234 | |||
| 235 | if (array_search($this->getDefaultPageLength(), $values) === false) { |
||
| 236 | for ($i = 0; $i < count($values); $i++) { |
||
| 237 | if ($values[$i] > $this->getDefaultPageLength() || $values[$i] === -1) { |
||
| 238 | array_splice($values, $i, 0, $this->getDefaultPageLength()); |
||
| 239 | array_splice($labels, $i, 0, $this->getDefaultPageLength()); |
||
| 240 | break; |
||
| 241 | } |
||
| 242 | if ($i === count($values) - 1) { |
||
| 243 | $values[] = $this->getDefaultPageLength(); |
||
| 244 | $labels[] = $this->getDefaultPageLength(); |
||
| 245 | break; |
||
| 246 | } |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | $this->setOperationSetting('pageLengthMenu', [$values, $labels]); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Specify array of available page lengths on the list view. |
||
| 255 | * |
||
| 256 | * @param array|int $menu |
||
| 257 | * |
||
| 258 | * https://backpackforlaravel.com/docs/4.1/crud-cheat-sheet#page-length |
||
| 259 | */ |
||
| 260 | public function setPageLengthMenu($menu) |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Builds the menu from the given array. It works out with two different types of arrays: |
||
| 288 | * [1, 2, 3] AND [1 => 'one', 2 => 'two', 3 => 'three']. |
||
| 289 | * |
||
| 290 | * @param array $menu |
||
| 291 | * @return array |
||
| 292 | */ |
||
| 293 | private function buildPageLengthMenuFromArray($menu) |
||
| 294 | { |
||
| 295 | // check if the values of the array are strings, in case developer defined: |
||
| 296 | // setPageLengthMenu([0 => 'f', 100 => 'h', 300 => 't']) |
||
| 297 | if (count(array_filter(array_values($menu), 'is_string')) > 0) { |
||
| 298 | $values = array_keys($menu); |
||
| 299 | $labels = array_values($menu); |
||
| 300 | |||
| 301 | $this->abortIfInvalidPageLength($values); |
||
| 302 | |||
| 303 | return [$values, $labels]; |
||
| 304 | } else { |
||
| 305 | // developer defined length as setPageLengthMenu([50, 100, 300]) |
||
| 306 | // we will use the same values as labels |
||
| 307 | $this->abortIfInvalidPageLength($menu); |
||
| 308 | |||
| 309 | return [$menu, $menu]; |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get page length menu for the list view. |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | public function getPageLengthMenu() |
||
| 319 | { |
||
| 320 | // if we have a 2D array, update all the values in the right hand array to their translated values |
||
| 321 | if (isset($this->getOperationSetting('pageLengthMenu')[1]) && is_array($this->getOperationSetting('pageLengthMenu')[1])) { |
||
| 322 | $aux = $this->getOperationSetting('pageLengthMenu'); |
||
| 323 | foreach ($this->getOperationSetting('pageLengthMenu')[1] as $key => $val) { |
||
| 324 | $aux[1][$key] = trans($val); |
||
| 325 | } |
||
| 326 | $this->setOperationSetting('pageLengthMenu', $aux); |
||
| 327 | } |
||
| 328 | $this->addCustomPageLengthToPageLengthMenu(); |
||
| 329 | |||
| 330 | return $this->getOperationSetting('pageLengthMenu'); |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Checks if the provided PageLength segment is valid. |
||
| 335 | * |
||
| 336 | * @param array|int $value |
||
| 337 | * @return void |
||
| 338 | */ |
||
| 339 | private function abortIfInvalidPageLength($value) |
||
| 340 | { |
||
| 341 | if ($value === 0 || (is_array($value) && in_array(0, $value))) { |
||
| 342 | abort(500, 'You should not use 0 as a key in paginator. If you are looking for "ALL" option, use -1 instead.'); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | /* |
||
| 347 | |-------------------------------------------------------------------------- |
||
| 348 | | EXPORT BUTTONS |
||
| 349 | |-------------------------------------------------------------------------- |
||
| 350 | */ |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Tell the list view to show the DataTables export buttons. |
||
| 354 | */ |
||
| 355 | public function enableExportButtons() |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Check if export buttons are enabled for the table view. |
||
| 362 | * |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | public function exportButtons() |
||
| 368 | } |
||
| 369 | } |
||
| 370 |