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