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