We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 60 |
| Total Lines | 503 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like CrudPanel 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 CrudPanel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class CrudPanel |
||
| 43 | { |
||
| 44 | // load all the default CrudPanel features |
||
| 45 | use Create, Read, Search, Update, Delete, Input, Errors, Reorder, Access, Columns, Fields, Query, Buttons, AutoSet, FakeFields, FakeColumns, AutoFocus, Filters, Tabs, Views, Validation, HeadingsAndTitles, Operations, SaveActions, Settings, Relationships, HasViewNamespaces, MorphRelationships; |
||
| 46 | |||
| 47 | // allow developers to add their own closures to this object |
||
| 48 | use Macroable; |
||
| 49 | |||
| 50 | // -------------- |
||
| 51 | // CRUD variables |
||
| 52 | // -------------- |
||
| 53 | // These variables are passed to the CRUD views, inside the $crud variable. |
||
| 54 | // All variables are public, so they can be modified from your EntityCrudController. |
||
| 55 | // All functions and methods are also public, so they can be used in your EntityCrudController to modify these variables. |
||
| 56 | |||
| 57 | public $model = "\App\Models\Entity"; // what's the namespace for your entity's model |
||
| 58 | |||
| 59 | public $route; // what route have you defined for your entity? used for links. |
||
| 60 | |||
| 61 | public $entity_name = 'entry'; // what name will show up on the buttons, in singural (ex: Add entity) |
||
| 62 | |||
| 63 | public $entity_name_plural = 'entries'; // what name will show up on the buttons, in plural (ex: Delete 5 entities) |
||
| 64 | |||
| 65 | public $entry; |
||
| 66 | |||
| 67 | protected $request; |
||
| 68 | |||
| 69 | public $initialized = false; |
||
| 70 | |||
| 71 | // The following methods are used in CrudController or your EntityCrudController to manipulate the variables above. |
||
| 72 | |||
| 73 | public function __construct() |
||
| 74 | { |
||
| 75 | } |
||
| 76 | |||
| 77 | public function isInitialized() |
||
| 78 | { |
||
| 79 | return $this->initialized; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Set the request instance for this CRUD. |
||
| 84 | * |
||
| 85 | * @param Request $request |
||
| 86 | */ |
||
| 87 | public function setRequest($request = null): self |
||
| 88 | { |
||
| 89 | $this->request = $request ?? \Request::instance(); |
||
| 90 | |||
| 91 | return $this; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get the request instance for this CRUD. |
||
| 96 | * |
||
| 97 | * @return Request |
||
| 98 | */ |
||
| 99 | public function getRequest() |
||
| 100 | { |
||
| 101 | |||
| 102 | return $this->request; |
||
| 103 | } |
||
| 104 | |||
| 105 | // ------------------------------------------------------ |
||
| 106 | // BASICS - model, route, entity_name, entity_name_plural |
||
| 107 | // ------------------------------------------------------ |
||
| 108 | |||
| 109 | /** |
||
| 110 | * This function binds the CRUD to its corresponding Model (which extends Eloquent). |
||
| 111 | * All Create-Read-Update-Delete operations are done using that Eloquent Collection. |
||
| 112 | * |
||
| 113 | * @param string $model_namespace Full model namespace. Ex: App\Models\Article |
||
| 114 | * |
||
| 115 | * @throws Exception in case the model does not exist |
||
| 116 | */ |
||
| 117 | public function setModel($model_namespace) |
||
| 118 | { |
||
| 119 | if (! class_exists($model_namespace)) { |
||
| 120 | throw new Exception('The model does not exist.', 500); |
||
| 121 | } |
||
| 122 | |||
| 123 | if (! method_exists($model_namespace, 'hasCrudTrait')) { |
||
| 124 | throw new Exception('Please use CrudTrait on the model.', 500); |
||
| 125 | } |
||
| 126 | |||
| 127 | $this->model = new $model_namespace(); |
||
| 128 | $this->query = clone $this->totalQuery = $this->model->select('*'); |
||
| 129 | $this->entry = null; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get the corresponding Eloquent Model for the CrudController, as defined with the setModel() function. |
||
| 134 | * |
||
| 135 | * @return string|\Illuminate\Database\Eloquent\Model |
||
| 136 | */ |
||
| 137 | public function getModel() |
||
| 138 | { |
||
| 139 | return $this->model; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get the database connection, as specified in the .env file or overwritten by the property on the model. |
||
| 144 | * |
||
| 145 | * @return \Illuminate\Database\Schema\Builder |
||
| 146 | */ |
||
| 147 | private function getSchema() |
||
| 148 | { |
||
| 149 | return $this->getModel()->getConnection()->getSchemaBuilder(); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Check if the database connection driver is using mongodb. |
||
| 154 | * |
||
| 155 | * DEPRECATION NOTICE: This method is no longer used and will be removed in future versions of Backpack |
||
| 156 | * |
||
| 157 | * @deprecated |
||
| 158 | * |
||
| 159 | * @codeCoverageIgnore |
||
| 160 | * |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | private function driverIsMongoDb() |
||
| 164 | { |
||
| 165 | return $this->getSchema()->getConnection()->getConfig()['driver'] === 'mongodb'; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Check if the database connection is any sql driver. |
||
| 170 | * |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | private function driverIsSql() |
||
| 174 | { |
||
| 175 | $driver = $this->getSchema()->getConnection()->getConfig('driver'); |
||
| 176 | |||
| 177 | return in_array($driver, $this->getSqlDriverList()); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get SQL driver list. |
||
| 182 | * |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | public function getSqlDriverList() |
||
| 186 | { |
||
| 187 | return ['mysql', 'sqlsrv', 'sqlite', 'pgsql', 'mariadb']; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Set the route for this CRUD. |
||
| 192 | * Ex: admin/article. |
||
| 193 | * |
||
| 194 | * @param string $route Route name. |
||
| 195 | */ |
||
| 196 | public function setRoute($route) |
||
| 197 | { |
||
| 198 | $this->route = ltrim($route, '/'); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Set the route for this CRUD using the route name. |
||
| 203 | * Ex: admin.article. |
||
| 204 | * |
||
| 205 | * @param string $route Route name. |
||
| 206 | * @param array $parameters Parameters. |
||
| 207 | * |
||
| 208 | * @throws Exception |
||
| 209 | */ |
||
| 210 | public function setRouteName($route, $parameters = []) |
||
| 211 | { |
||
| 212 | $route = ltrim($route, '.'); |
||
| 213 | |||
| 214 | $complete_route = $route.'.index'; |
||
| 215 | |||
| 216 | if (! \Route::has($complete_route)) { |
||
| 217 | throw new Exception('There are no routes for this route name.', 404); |
||
| 218 | } |
||
| 219 | |||
| 220 | $this->route = route($complete_route, $parameters); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get the current CrudController route. |
||
| 225 | * |
||
| 226 | * Can be defined in the CrudController with: |
||
| 227 | * - $this->crud->setRoute(config('backpack.base.route_prefix').'/article') |
||
| 228 | * - $this->crud->setRouteName(config('backpack.base.route_prefix').'.article') |
||
| 229 | * - $this->crud->route = config('backpack.base.route_prefix')."/article" |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function getRoute() |
||
| 234 | { |
||
| 235 | return $this->route; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Set the entity name in singular and plural. |
||
| 240 | * Used all over the CRUD interface (header, add button, reorder button, breadcrumbs). |
||
| 241 | * |
||
| 242 | * @param string $singular Entity name, in singular. Ex: article |
||
| 243 | * @param string $plural Entity name, in plural. Ex: articles |
||
| 244 | */ |
||
| 245 | public function setEntityNameStrings($singular, $plural) |
||
| 246 | { |
||
| 247 | $this->entity_name = $singular; |
||
| 248 | $this->entity_name_plural = $plural; |
||
| 249 | } |
||
| 250 | |||
| 251 | // ----------------------------------------------- |
||
| 252 | // ACTIONS - the current operation being processed |
||
| 253 | // ----------------------------------------------- |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get the action being performed by the controller, |
||
| 257 | * including middleware names, route name, method name, |
||
| 258 | * namespace, prefix, etc. |
||
| 259 | * |
||
| 260 | * @return string The EntityCrudController route action array. |
||
| 261 | */ |
||
| 262 | public function getAction() |
||
| 263 | { |
||
| 264 | return $this->getRequest()->route()->getAction(); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get the full name of the controller method |
||
| 269 | * currently being called (including namespace). |
||
| 270 | * |
||
| 271 | * @return string The EntityCrudController full method name with namespace. |
||
| 272 | */ |
||
| 273 | public function getActionName() |
||
| 274 | { |
||
| 275 | return $this->getRequest()->route()->getActionName(); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get the name of the controller method |
||
| 280 | * currently being called. |
||
| 281 | * |
||
| 282 | * @return string The EntityCrudController method name. |
||
| 283 | */ |
||
| 284 | public function getActionMethod() |
||
| 285 | { |
||
| 286 | return $this->getRequest()->route()->getActionMethod(); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Check if the controller method being called |
||
| 291 | * matches a given string. |
||
| 292 | * |
||
| 293 | * @param string $methodName Name of the method (ex: index, create, update) |
||
| 294 | * @return bool Whether the condition is met or not. |
||
| 295 | */ |
||
| 296 | public function actionIs($methodName) |
||
| 297 | { |
||
| 298 | return $methodName === $this->getActionMethod(); |
||
| 299 | } |
||
| 300 | |||
| 301 | // ---------------------------------- |
||
| 302 | // Miscellaneous functions or methods |
||
| 303 | // ---------------------------------- |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Return the first element in an array that has the given 'type' attribute. |
||
| 307 | * |
||
| 308 | * @param string $type |
||
| 309 | * @param array $array |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | public function getFirstOfItsTypeInArray($type, $array) |
||
| 313 | { |
||
| 314 | return Arr::first($array, function ($item) use ($type) { |
||
| 315 | return $item['type'] == $type; |
||
| 316 | }); |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED IN THIS FILE. |
||
| 321 | * |
||
| 322 | * TODO: |
||
| 323 | * - figure out if they are really needed |
||
| 324 | * - comments inside the function to explain how they work |
||
| 325 | * - write docblock for them |
||
| 326 | * - place in the correct section above (CREATE, READ, UPDATE, DELETE, ACCESS, MANIPULATION) |
||
| 327 | * |
||
| 328 | * @deprecated |
||
| 329 | * |
||
| 330 | * @codeCoverageIgnore |
||
| 331 | */ |
||
| 332 | public function sync($type, $fields, $attributes) |
||
| 333 | { |
||
| 334 | if (! empty($this->{$type})) { |
||
| 335 | $this->{$type} = array_map(function ($field) use ($fields, $attributes) { |
||
| 336 | if (in_array($field['name'], (array) $fields)) { |
||
| 337 | $field = array_merge($field, $attributes); |
||
| 338 | } |
||
| 339 | |||
| 340 | return $field; |
||
| 341 | }, $this->{$type}); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Get the Eloquent Model name from the given relation definition string. |
||
| 347 | * |
||
| 348 | * @example For a given string 'company' and a relation between App/Models/User and App/Models/Company, defined by a |
||
| 349 | * company() method on the user model, the 'App/Models/Company' string will be returned. |
||
| 350 | * @example For a given string 'company.address' and a relation between App/Models/User, App/Models/Company and |
||
| 351 | * App/Models/Address defined by a company() method on the user model and an address() method on the |
||
| 352 | * company model, the 'App/Models/Address' string will be returned. |
||
| 353 | * |
||
| 354 | * @param string $relationString Relation string. A dot notation can be used to chain multiple relations. |
||
| 355 | * @param int $length Optionally specify the number of relations to omit from the start of the relation string. If |
||
| 356 | * the provided length is negative, then that many relations will be omitted from the end of the relation |
||
| 357 | * string. |
||
| 358 | * @param Model $model Optionally specify a different model than the one in the crud object. |
||
| 359 | * @return string Relation model name. |
||
| 360 | */ |
||
| 361 | public function getRelationModel($relationString, $length = null, $model = null) |
||
| 362 | { |
||
| 363 | $relationArray = explode('.', $relationString); |
||
| 364 | |||
| 365 | if (! isset($length)) { |
||
| 366 | $length = count($relationArray); |
||
| 367 | } |
||
| 368 | |||
| 369 | if (! isset($model)) { |
||
| 370 | $model = $this->model; |
||
| 371 | } |
||
| 372 | |||
| 373 | $result = array_reduce(array_splice($relationArray, 0, $length), function ($obj, $method) { |
||
| 374 | try { |
||
| 375 | $result = $obj->$method(); |
||
| 376 | if (! $result instanceof Relation) { |
||
| 377 | throw new Exception('Not a relation'); |
||
| 378 | } |
||
| 379 | |||
| 380 | return $result->getRelated(); |
||
| 381 | } catch (Exception $e) { |
||
| 382 | return $obj; |
||
| 383 | } |
||
| 384 | }, $model); |
||
| 385 | |||
| 386 | return get_class($result); |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Get the given attribute from a model or models resulting from the specified relation string (eg: the list of streets from |
||
| 391 | * the many addresses of the company of a given user). |
||
| 392 | * |
||
| 393 | * @param Model $model Model (eg: user). |
||
| 394 | * @param string $relationString Model relation. Can be a string representing the name of a relation method in the given |
||
| 395 | * Model or one from a different Model through multiple relations. A dot notation can be used to specify |
||
| 396 | * multiple relations (eg: user.company.address). |
||
| 397 | * @param string $attribute The attribute from the relation model (eg: the street attribute from the address model). |
||
| 398 | * @return array An array containing a list of attributes from the resulting model. |
||
| 399 | */ |
||
| 400 | public function getRelatedEntriesAttributes($model, $relationString, $attribute) |
||
| 401 | { |
||
| 402 | $endModels = $this->getRelatedEntries($model, $relationString); |
||
| 403 | $attributes = []; |
||
| 404 | foreach ($endModels as $model => $entries) { |
||
| 405 | /** @var Model $model_instance */ |
||
| 406 | $model_instance = new $model(); |
||
| 407 | $modelKey = $model_instance->getKeyName(); |
||
| 408 | |||
| 409 | if (is_array($entries)) { |
||
| 410 | //if attribute does not exist in main array we have more than one entry OR the attribute |
||
| 411 | //is an accessor that is not in $appends property of model. |
||
| 412 | if (! isset($entries[$attribute])) { |
||
| 413 | //we first check if we don't have the attribute because it's an accessor that is not in appends. |
||
| 414 | if ($model_instance->hasGetMutator($attribute) && isset($entries[$modelKey])) { |
||
| 415 | $entry_in_database = $model_instance->find($entries[$modelKey]); |
||
| 416 | $attributes[$entry_in_database->{$modelKey}] = $this->parseTranslatableAttributes($model_instance, $attribute, $entry_in_database->{$attribute}); |
||
| 417 | } else { |
||
| 418 | //we have multiple entries |
||
| 419 | //for each entry we check if $attribute exists in array or try to check if it's an accessor. |
||
| 420 | foreach ($entries as $entry) { |
||
| 421 | if (isset($entry[$attribute])) { |
||
| 422 | $attributes[$entry[$modelKey]] = $this->parseTranslatableAttributes($model_instance, $attribute, $entry[$attribute]); |
||
| 423 | } else { |
||
| 424 | if ($model_instance->hasGetMutator($attribute)) { |
||
| 425 | $entry_in_database = $model_instance->find($entry[$modelKey]); |
||
| 426 | $attributes[$entry_in_database->{$modelKey}] = $this->parseTranslatableAttributes($model_instance, $attribute, $entry_in_database->{$attribute}); |
||
| 427 | } |
||
| 428 | } |
||
| 429 | } |
||
| 430 | } |
||
| 431 | } else { |
||
| 432 | //if we have the attribute we just return it, does not matter if it is direct attribute or an accessor added in $appends. |
||
| 433 | $attributes[$entries[$modelKey]] = $this->parseTranslatableAttributes($model_instance, $attribute, $entries[$attribute]); |
||
| 434 | } |
||
| 435 | } |
||
| 436 | } |
||
| 437 | |||
| 438 | return $attributes; |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Parse translatable attributes from a model or models resulting from the specified relation string. |
||
| 443 | * |
||
| 444 | * @param Model $model Model (eg: user). |
||
| 445 | * @param string $attribute The attribute from the relation model (eg: the street attribute from the address model). |
||
| 446 | * @param string $value Attribute value translatable or not |
||
| 447 | * @return string A string containing the translated attributed based on app()->getLocale() |
||
| 448 | */ |
||
| 449 | public function parseTranslatableAttributes($model, $attribute, $value) |
||
| 450 | { |
||
| 451 | if (! method_exists($model, 'isTranslatableAttribute')) { |
||
| 452 | return $value; |
||
| 453 | } |
||
| 454 | |||
| 455 | if (! $model->isTranslatableAttribute($attribute)) { |
||
| 456 | return $value; |
||
| 457 | } |
||
| 458 | |||
| 459 | if (! is_array($value)) { |
||
| 460 | $decodedAttribute = json_decode($value, true) ?? ($value !== null ? [$value] : []); |
||
| 461 | } else { |
||
| 462 | $decodedAttribute = $value; |
||
| 463 | } |
||
| 464 | |||
| 465 | if (is_array($decodedAttribute) && ! empty($decodedAttribute)) { |
||
| 466 | if (isset($decodedAttribute[app()->getLocale()])) { |
||
| 467 | return $decodedAttribute[app()->getLocale()]; |
||
| 468 | } else { |
||
| 469 | return Arr::first($decodedAttribute); |
||
| 470 | } |
||
| 471 | } |
||
| 472 | |||
| 473 | return $value; |
||
| 474 | } |
||
| 475 | |||
| 476 | public function setLocaleOnModel(Model $model) |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Traverse the tree of relations for the given model, defined by the given relation string, and return the ending |
||
| 493 | * associated model instance or instances. |
||
| 494 | * |
||
| 495 | * @param Model $model The CRUD model. |
||
| 496 | * @param string $relationString Relation string. A dot notation can be used to chain multiple relations. |
||
| 497 | * @return array An array of the associated model instances defined by the relation string. |
||
| 498 | */ |
||
| 499 | private function getRelatedEntries($model, $relationString) |
||
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Allow to add an attribute to multiple fields/columns/filters/buttons at same time. |
||
| 534 | * |
||
| 535 | * Using the fluent syntax allow the developer to add attributes to multiple fields at the same time. Eg: |
||
| 536 | * |
||
| 537 | * - CRUD::group(CRUD::field('price')->type('number'), CRUD::field('title')->type('text'))->tab('both_on_same_tab'); |
||
| 538 | * |
||
| 539 | * @param mixed fluent syntax objects. |
||
| 540 | * @return CrudObjectGroup |
||
| 541 | */ |
||
| 542 | public function group(...$objects) |
||
| 545 | } |
||
| 546 | } |
||
| 547 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths