We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 41 | 
| Total Lines | 314 | 
| Duplicated Lines | 0 % | 
| Changes | 3 | ||
| Bugs | 1 | Features | 0 | 
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 | ||
| 8 | trait Read | ||
| 9 | { | ||
| 10 | /** | ||
| 11 | * Find and retrieve the id of the current entry. | ||
| 12 | * | ||
| 13 | * @return int|bool The id in the db or false. | ||
| 14 | */ | ||
| 15 | public function getCurrentEntryId() | ||
| 16 |     { | ||
| 17 |         if ($this->entry) { | ||
| 18 | return $this->entry->getKey(); | ||
| 19 | } | ||
| 20 | |||
| 21 | $params = \Route::current()->parameters(); | ||
| 22 | |||
| 23 | return // use the entity name to get the current entry | ||
| 24 | // this makes sure the ID is corrent even for nested resources | ||
| 25 | $this->getRequest()->input($this->entity_name) ?? | ||
|  | |||
| 26 | // otherwise use the next to last parameter | ||
| 27 | array_values($params)[count($params) - 1] ?? | ||
| 28 | // otherwise return false | ||
| 29 | false; | ||
| 30 | } | ||
| 31 | |||
| 32 | /** | ||
| 33 | * Find and retrieve the current entry. | ||
| 34 | * | ||
| 35 | * @return \Illuminate\Database\Eloquent\Model|bool The row in the db or false. | ||
| 36 | */ | ||
| 37 | public function getCurrentEntry() | ||
| 38 |     { | ||
| 39 | $id = $this->getCurrentEntryId(); | ||
| 40 | |||
| 41 |         if (! $id) { | ||
| 42 | return false; | ||
| 43 | } | ||
| 44 | |||
| 45 | return $this->getEntry($id); | ||
| 46 | } | ||
| 47 | |||
| 48 | /** | ||
| 49 | * Find and retrieve an entry in the database or fail. | ||
| 50 | * | ||
| 51 | * @param int The id of the row in the db to fetch. | ||
| 52 | * | ||
| 53 | * @return \Illuminate\Database\Eloquent\Model The row in the db. | ||
| 54 | */ | ||
| 55 | public function getEntry($id) | ||
| 56 |     { | ||
| 57 |         if (! $this->entry) { | ||
| 58 | $this->entry = $this->model->findOrFail($id); | ||
| 59 | $this->entry = $this->entry->withFakes(); | ||
| 60 | } | ||
| 61 | |||
| 62 | return $this->entry; | ||
| 63 | } | ||
| 64 | |||
| 65 | /** | ||
| 66 | * Find and retrieve an entry in the database or fail. | ||
| 67 | * | ||
| 68 | * @param int The id of the row in the db to fetch. | ||
| 69 | * | ||
| 70 | * @return \Illuminate\Database\Eloquent\Model The row in the db. | ||
| 71 | */ | ||
| 72 | public function getEntryWithoutFakes($id) | ||
| 75 | } | ||
| 76 | |||
| 77 | /** | ||
| 78 | * Make the query JOIN all relationships used in the columns, too, | ||
| 79 | * so there will be less database queries overall. | ||
| 80 | */ | ||
| 81 | public function autoEagerLoadRelationshipColumns() | ||
| 82 |     { | ||
| 83 | $relationships = $this->getColumnsRelationships(); | ||
| 84 | |||
| 85 |         if (count($relationships)) { | ||
| 86 | $this->with($relationships); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | /** | ||
| 91 | * Get all entries from the database. | ||
| 92 | * | ||
| 93 | * @return array|\Illuminate\Database\Eloquent\Collection | ||
| 94 | */ | ||
| 95 | public function getEntries() | ||
| 96 |     { | ||
| 97 | $this->autoEagerLoadRelationshipColumns(); | ||
| 98 | |||
| 99 | $entries = $this->query->get(); | ||
| 100 | |||
| 101 | // add the fake columns for each entry | ||
| 102 |         foreach ($entries as $key => $entry) { | ||
| 103 | $entry->addFakes($this->getFakeColumnsAsArray()); | ||
| 104 | } | ||
| 105 | |||
| 106 | return $entries; | ||
| 107 | } | ||
| 108 | |||
| 109 | /** | ||
| 110 | * Enable the DETAILS ROW functionality:. | ||
| 111 | * | ||
| 112 | * In the table view, show a plus sign next to each entry. | ||
| 113 | * 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. | ||
| 114 | */ | ||
| 115 | public function enableDetailsRow() | ||
| 116 |     { | ||
| 117 |         $this->setOperationSetting('detailsRow', true); | ||
| 118 | } | ||
| 119 | |||
| 120 | /** | ||
| 121 | * Disable the DETAILS ROW functionality:. | ||
| 122 | */ | ||
| 123 | public function disableDetailsRow() | ||
| 126 | } | ||
| 127 | |||
| 128 | /** | ||
| 129 | * Add two more columns at the beginning of the ListEntrie table: | ||
| 130 | * - one shows the checkboxes needed for bulk actions | ||
| 131 | * - one is blank, in order for evenual detailsRow or expand buttons | ||
| 132 | * to be in a separate column. | ||
| 133 | */ | ||
| 134 | public function enableBulkActions() | ||
| 135 |     { | ||
| 136 |         if ($this->getOperationSetting('bulkActions') == true) { | ||
| 137 | return; | ||
| 138 | } | ||
| 139 | |||
| 140 |         $this->setOperationSetting('bulkActions', true); | ||
| 141 | |||
| 142 | $this->addColumn([ | ||
| 143 | 'type' => 'checkbox', | ||
| 144 | 'name' => 'bulk_actions', | ||
| 145 | 'label' => ' <input type="checkbox" class="crud_bulk_actions_main_checkbox" style="width: 16px; height: 16px;" />', | ||
| 146 | 'priority' => 0, | ||
| 147 | 'searchLogic' => false, | ||
| 148 | 'orderable' => false, | ||
| 149 | 'visibleInTable' => true, | ||
| 150 | 'visibleInModal' => false, | ||
| 151 | 'visibleInExport' => false, | ||
| 152 | 'visibleInShow' => false, | ||
| 153 | 'hasActions' => true, | ||
| 154 | ])->makeFirstColumn(); | ||
| 155 | |||
| 156 | $this->addColumn([ | ||
| 157 | 'type' => 'custom_html', | ||
| 158 | 'name' => 'blank_first_column', | ||
| 159 | 'label' => ' ', | ||
| 160 | 'priority' => 0, | ||
| 161 | 'searchLogic' => false, | ||
| 162 | 'orderable' => false, | ||
| 163 | 'visibleInTabel' => true, | ||
| 164 | 'visibleInModal' => false, | ||
| 165 | 'visibleInExport' => false, | ||
| 166 | 'visibleInShow' => false, | ||
| 167 | 'hasActions' => true, | ||
| 168 | ])->makeFirstColumn(); | ||
| 169 | } | ||
| 170 | |||
| 171 | /** | ||
| 172 | * Remove the two columns needed for bulk actions. | ||
| 173 | */ | ||
| 174 | public function disableBulkActions() | ||
| 180 | } | ||
| 181 | |||
| 182 | /** | ||
| 183 | * Set the number of rows that should be show on the list view. | ||
| 184 | */ | ||
| 185 | public function setDefaultPageLength($value) | ||
| 186 |     { | ||
| 187 |         $this->setOperationSetting('defaultPageLength', $value); | ||
| 188 | } | ||
| 189 | |||
| 190 | /** | ||
| 191 | * Get the number of rows that should be show on the list view. | ||
| 192 | * | ||
| 193 | * @return int | ||
| 194 | */ | ||
| 195 | public function getDefaultPageLength() | ||
| 198 | } | ||
| 199 | |||
| 200 | /** | ||
| 201 | * If a custom page length was specified as default, make sure it | ||
| 202 | * also show up in the page length menu. | ||
| 203 | */ | ||
| 204 | public function addCustomPageLengthToPageLengthMenu() | ||
| 224 | } | ||
| 225 | |||
| 226 | /** | ||
| 227 | * Specify array of available page lengths on the list view. | ||
| 228 | * | ||
| 229 | * @param array $menu 1d array of page length values, | ||
| 230 | * or 2d array (first array: page length values, second array: page length labels) | ||
| 231 | * More at: https://datatables.net/reference/option/lengthMenu | ||
| 232 | */ | ||
| 233 | public function setPageLengthMenu($menu) | ||
| 256 | } | ||
| 257 | |||
| 258 | /** | ||
| 259 | * Builds the menu from the given array. It works out with two different types of arrays: | ||
| 260 | * [1, 2, 3] AND | ||
| 261 | * [1 => 'one', 2 => 'two', 3 => 'three'] | ||
| 262 | * | ||
| 263 | * @param array $menu | ||
| 264 | * @return array | ||
| 265 | */ | ||
| 266 |     private function buildPageLengthMenuFromArray($menu) { | ||
| 267 | // check if the values of the array are strings, in case developer defined: | ||
| 268 | // setPageLengthMenu([0 => 'f', 100 => 'h', 300 => 't']) | ||
| 269 |         if(count(array_filter(array_values($menu), 'is_string')) > 0) { | ||
| 270 | $values = array_keys($menu); | ||
| 271 | $labels = array_values($menu); | ||
| 272 | return [$values, $labels]; | ||
| 273 |         }else{ | ||
| 274 | // developer defined length as setPageLengthMenu([0, 100, 300]) | ||
| 275 | // we will use the same values as labels | ||
| 276 | return [$menu, $menu]; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | /** | ||
| 281 | * Get page length menu for the list view. | ||
| 282 | * | ||
| 283 | * @return array | ||
| 284 | */ | ||
| 285 | public function getPageLengthMenu() | ||
| 286 |     { | ||
| 287 | // if we have a 2D array, update all the values in the right hand array to their translated values | ||
| 288 |         if (isset($this->getOperationSetting('pageLengthMenu')[1]) && is_array($this->getOperationSetting('pageLengthMenu')[1])) { | ||
| 289 |             $aux = $this->getOperationSetting('pageLengthMenu'); | ||
| 290 |             foreach ($this->getOperationSetting('pageLengthMenu')[1] as $key => $val) { | ||
| 291 | $aux[1][$key] = trans($val); | ||
| 292 | } | ||
| 293 |             $this->setOperationSetting('pageLengthMenu', $aux); | ||
| 294 | } | ||
| 295 | $this->addCustomPageLengthToPageLengthMenu(); | ||
| 296 | |||
| 297 |         return $this->getOperationSetting('pageLengthMenu'); | ||
| 298 | } | ||
| 299 | |||
| 300 | /* | ||
| 301 | |-------------------------------------------------------------------------- | ||
| 302 | | EXPORT BUTTONS | ||
| 303 | |-------------------------------------------------------------------------- | ||
| 304 | */ | ||
| 305 | |||
| 306 | /** | ||
| 307 | * Tell the list view to show the DataTables export buttons. | ||
| 308 | */ | ||
| 309 | public function enableExportButtons() | ||
| 312 | } | ||
| 313 | |||
| 314 | /** | ||
| 315 | * Check if export buttons are enabled for the table view. | ||
| 316 | * | ||
| 317 | * @return bool | ||
| 318 | */ | ||
| 319 | public function exportButtons() | ||
| 324 |