We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 61 |
| Total Lines | 407 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| 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 |
||
| 13 | trait Read |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Find and retrieve the id of the current entry. |
||
| 17 | * |
||
| 18 | * @return int|bool The id in the db or false. |
||
| 19 | */ |
||
| 20 | public function getCurrentEntryId() |
||
| 21 | { |
||
| 22 | if ($this->entry) { |
||
| 23 | return $this->entry->getKey(); |
||
| 24 | } |
||
| 25 | |||
| 26 | $params = Route::current()?->parameters() ?? []; |
||
| 27 | |||
| 28 | return // use the entity name to get the current entry |
||
| 29 | // this makes sure the ID is current even for nested resources |
||
| 30 | $this->getRequest()->input($this->entity_name) ?? |
||
|
|
|||
| 31 | // otherwise use the next to last parameter |
||
| 32 | array_values($params)[count($params) - 1] ?? |
||
| 33 | // otherwise return false |
||
| 34 | false; |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Find and retrieve the current entry. |
||
| 39 | * |
||
| 40 | * @return \Illuminate\Database\Eloquent\Model|bool The row in the db or false. |
||
| 41 | */ |
||
| 42 | public function getCurrentEntry() |
||
| 43 | { |
||
| 44 | $id = $this->getCurrentEntryId(); |
||
| 45 | |||
| 46 | if ($id === false) { |
||
| 47 | return false; |
||
| 48 | } |
||
| 49 | |||
| 50 | return $this->getEntry($id); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function getCurrentEntryWithLocale() |
||
| 54 | { |
||
| 55 | $entry = $this->getCurrentEntry(); |
||
| 56 | |||
| 57 | if (! $entry) { |
||
| 58 | return false; |
||
| 59 | } |
||
| 60 | |||
| 61 | return $this->setLocaleOnModel($entry); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Find and retrieve an entry in the database or fail. |
||
| 66 | * |
||
| 67 | * @param int The id of the row in the db to fetch. |
||
| 68 | * @return \Illuminate\Database\Eloquent\Model The row in the db. |
||
| 69 | */ |
||
| 70 | public function getEntry($id) |
||
| 71 | { |
||
| 72 | if (! $this->entry) { |
||
| 73 | if ($this->getOperationSetting('eagerLoadRelationships')) { |
||
| 74 | $this->eagerLoadRelationshipFields(); |
||
| 75 | } |
||
| 76 | $this->entry = $this->getModelWithCrudPanelQuery()->findOrFail($id); |
||
| 77 | $this->entry = $this->entry->withFakes(); |
||
| 78 | } |
||
| 79 | |||
| 80 | return $this->entry; |
||
| 81 | } |
||
| 82 | |||
| 83 | private function shouldUseFallbackLocale(): bool|string |
||
| 84 | { |
||
| 85 | $fallbackRequestValue = $this->getRequest()->get('_fallback_locale'); |
||
| 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 | $this->with($this->getRelationshipsFromCrudObjects('columns')); |
||
| 134 | } |
||
| 135 | |||
| 136 | public function eagerLoadRelationshipFields() |
||
| 139 | } |
||
| 140 | |||
| 141 | private function getRelationshipsFromCrudObjects(string $crudObjectType): array |
||
| 142 | { |
||
| 143 | $crudObjects = $this->{$crudObjectType}(); |
||
| 144 | |||
| 145 | $relationStrings = []; |
||
| 146 | |||
| 147 | foreach ($crudObjects as $crudObjectName => $attributes) { |
||
| 148 | $relationString = isset($attributes['entity']) && $attributes['entity'] !== false ? $attributes['entity'] : ''; |
||
| 149 | |||
| 150 | if (! $relationString) { |
||
| 151 | continue; |
||
| 152 | } |
||
| 153 | |||
| 154 | if (strpos($attributes['entity'], '.') === false) { |
||
| 155 | $relationStrings[] = $relationString; |
||
| 156 | } |
||
| 157 | |||
| 158 | $relationAttribute = $attributes['attribute'] ?? null; |
||
| 159 | |||
| 160 | if ($relationAttribute) { |
||
| 161 | $relationString = Str::endsWith($relationString, $relationAttribute) ? Str::beforeLast($relationString, '.') : $relationString; |
||
| 162 | |||
| 163 | $relationStrings[] = $relationString; |
||
| 164 | |||
| 165 | continue; |
||
| 166 | } |
||
| 167 | |||
| 168 | $parts = explode('.', $relationString); |
||
| 169 | $model = $this->model; |
||
| 170 | |||
| 171 | // Iterate over each relation part to find the valid relations without attributes |
||
| 172 | // We should eager load the relation but not the attribute |
||
| 173 | foreach ($parts as $i => $part) { |
||
| 174 | try { |
||
| 175 | $model = $model->$part()->getRelated(); |
||
| 176 | } catch (Exception $e) { |
||
| 177 | $relationString = implode('.', array_slice($parts, 0, $i)); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | $relationStrings[] = $relationString; |
||
| 182 | |||
| 183 | continue; |
||
| 184 | } |
||
| 185 | |||
| 186 | return array_unique($relationStrings); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get all entries from the database. |
||
| 191 | * |
||
| 192 | * @return array|\Illuminate\Database\Eloquent\Collection |
||
| 193 | */ |
||
| 194 | public function getEntries() |
||
| 195 | { |
||
| 196 | $this->autoEagerLoadRelationshipColumns(); |
||
| 197 | |||
| 198 | $entries = $this->query->get(); |
||
| 199 | |||
| 200 | // add the fake columns for each entry |
||
| 201 | foreach ($entries as $key => $entry) { |
||
| 202 | $entry->addFakes($this->getFakeColumnsAsArray()); |
||
| 203 | } |
||
| 204 | |||
| 205 | return $entries; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Enable the DETAILS ROW functionality:. |
||
| 210 | * |
||
| 211 | * In the table view, show a plus sign next to each entry. |
||
| 212 | * 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. |
||
| 213 | */ |
||
| 214 | public function enableDetailsRow() |
||
| 215 | { |
||
| 216 | if (! backpack_pro()) { |
||
| 217 | throw new BackpackProRequiredException('Details row'); |
||
| 218 | } |
||
| 219 | |||
| 220 | $this->setOperationSetting('detailsRow', true); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Disable the DETAILS ROW functionality:. |
||
| 225 | */ |
||
| 226 | public function disableDetailsRow() |
||
| 227 | { |
||
| 228 | $this->setOperationSetting('detailsRow', false); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Add two more columns at the beginning of the ListEntries table: |
||
| 233 | * - one shows the checkboxes needed for bulk actions |
||
| 234 | * - one is blank, in order for evenual detailsRow or expand buttons |
||
| 235 | * to be in a separate column. |
||
| 236 | */ |
||
| 237 | public function enableBulkActions() |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Remove the two columns needed for bulk actions. |
||
| 244 | */ |
||
| 245 | public function disableBulkActions() |
||
| 246 | { |
||
| 247 | $this->setOperationSetting('bulkActions', false); |
||
| 248 | |||
| 249 | $this->removeColumn('bulk_actions'); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Set the number of rows that should be show on the list view. |
||
| 254 | */ |
||
| 255 | public function setDefaultPageLength($value) |
||
| 256 | { |
||
| 257 | $this->abortIfInvalidPageLength($value); |
||
| 258 | |||
| 259 | $this->setOperationSetting('defaultPageLength', $value); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get the number of rows that should be show on the list view. |
||
| 264 | * |
||
| 265 | * @return int |
||
| 266 | */ |
||
| 267 | public function getDefaultPageLength() |
||
| 268 | { |
||
| 269 | return $this->getOperationSetting('defaultPageLength') ?? config('backpack.crud.operations.list.defaultPageLength') ?? 25; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * If a custom page length was specified as default, make sure it |
||
| 274 | * also show up in the page length menu. |
||
| 275 | */ |
||
| 276 | public function addCustomPageLengthToPageLengthMenu() |
||
| 277 | { |
||
| 278 | $values = $this->getOperationSetting('pageLengthMenu')[0]; |
||
| 279 | $labels = $this->getOperationSetting('pageLengthMenu')[1]; |
||
| 280 | |||
| 281 | if (array_search($this->getDefaultPageLength(), $values) === false) { |
||
| 282 | for ($i = 0; $i < count($values); $i++) { |
||
| 283 | if ($values[$i] > $this->getDefaultPageLength() || $values[$i] === -1) { |
||
| 284 | array_splice($values, $i, 0, $this->getDefaultPageLength()); |
||
| 285 | array_splice($labels, $i, 0, $this->getDefaultPageLength()); |
||
| 286 | break; |
||
| 287 | } |
||
| 288 | if ($i === count($values) - 1) { |
||
| 289 | $values[] = $this->getDefaultPageLength(); |
||
| 290 | $labels[] = $this->getDefaultPageLength(); |
||
| 291 | break; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | $this->setOperationSetting('pageLengthMenu', [$values, $labels]); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Specify array of available page lengths on the list view. |
||
| 301 | * |
||
| 302 | * @param array|int $menu |
||
| 303 | * |
||
| 304 | * https://backpackforlaravel.com/docs/4.1/crud-cheat-sheet#page-length |
||
| 305 | */ |
||
| 306 | public function setPageLengthMenu($menu) |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Builds the menu from the given array. It works out with two different types of arrays: |
||
| 334 | * [1, 2, 3] AND [1 => 'one', 2 => 'two', 3 => 'three']. |
||
| 335 | * |
||
| 336 | * @param array $menu |
||
| 337 | * @return array |
||
| 338 | */ |
||
| 339 | private function buildPageLengthMenuFromArray($menu) |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Get page length menu for the list view. |
||
| 361 | * |
||
| 362 | * @return array |
||
| 363 | */ |
||
| 364 | public function getPageLengthMenu() |
||
| 365 | { |
||
| 366 | // if we have a 2D array, update all the values in the right hand array to their translated values |
||
| 367 | if (isset($this->getOperationSetting('pageLengthMenu')[1]) && is_array($this->getOperationSetting('pageLengthMenu')[1])) { |
||
| 368 | $aux = $this->getOperationSetting('pageLengthMenu'); |
||
| 369 | foreach ($this->getOperationSetting('pageLengthMenu')[1] as $key => $val) { |
||
| 370 | $aux[1][$key] = trans($val); |
||
| 371 | } |
||
| 372 | $this->setOperationSetting('pageLengthMenu', $aux); |
||
| 373 | } |
||
| 374 | $this->addCustomPageLengthToPageLengthMenu(); |
||
| 375 | |||
| 376 | return $this->getOperationSetting('pageLengthMenu'); |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Checks if the provided PageLength segment is valid. |
||
| 381 | * |
||
| 382 | * @param array|int $value |
||
| 383 | * @return void |
||
| 384 | */ |
||
| 385 | private function abortIfInvalidPageLength($value) |
||
| 386 | { |
||
| 387 | if ($value === 0 || (is_array($value) && in_array(0, $value))) { |
||
| 388 | abort(500, 'You should not use 0 as a key in paginator. If you are looking for "ALL" option, use -1 instead.'); |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | /* |
||
| 393 | |-------------------------------------------------------------------------- |
||
| 394 | | EXPORT BUTTONS |
||
| 395 | |-------------------------------------------------------------------------- |
||
| 396 | */ |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Tell the list view to show the DataTables export buttons. |
||
| 400 | */ |
||
| 401 | public function enableExportButtons() |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Check if export buttons are enabled for the table view. |
||
| 414 | * |
||
| 415 | * @return bool |
||
| 416 | */ |
||
| 417 | public function exportButtons() |
||
| 422 |