We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 53 |
| Total Lines | 434 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 3 | 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 |
||
| 36 | class CrudPanel |
||
| 37 | { |
||
| 38 | // load all the default CrudPanel features |
||
| 39 | use Create, Read, Search, Update, Delete, Errors, Reorder, Access, Columns, Fields, Query, Buttons, AutoSet, FakeFields, FakeColumns, AutoFocus, Filters, Tabs, Views, Validation, HeadingsAndTitles, Operations, SaveActions, Settings, Relationships; |
||
| 40 | // allow developers to add their own closures to this object |
||
| 41 | use Macroable; |
||
| 42 | |||
| 43 | // -------------- |
||
| 44 | // CRUD variables |
||
| 45 | // -------------- |
||
| 46 | // These variables are passed to the CRUD views, inside the $crud variable. |
||
| 47 | // All variables are public, so they can be modified from your EntityCrudController. |
||
| 48 | // All functions and methods are also public, so they can be used in your EntityCrudController to modify these variables. |
||
| 49 | |||
| 50 | public $model = "\App\Models\Entity"; // what's the namespace for your entity's model |
||
| 51 | public $route; // what route have you defined for your entity? used for links. |
||
| 52 | public $entity_name = 'entry'; // what name will show up on the buttons, in singural (ex: Add entity) |
||
| 53 | public $entity_name_plural = 'entries'; // what name will show up on the buttons, in plural (ex: Delete 5 entities) |
||
| 54 | |||
| 55 | public $entry; |
||
| 56 | |||
| 57 | protected $request; |
||
| 58 | |||
| 59 | // The following methods are used in CrudController or your EntityCrudController to manipulate the variables above. |
||
| 60 | |||
| 61 | public function __construct() |
||
| 62 | { |
||
| 63 | $this->setRequest(); |
||
| 64 | |||
| 65 | if ($this->getCurrentOperation()) { |
||
| 66 | $this->setOperation($this->getCurrentOperation()); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Set the request instance for this CRUD. |
||
| 72 | * |
||
| 73 | * @param \Illuminate\Http\Request $request |
||
| 74 | */ |
||
| 75 | public function setRequest($request = null) |
||
| 76 | { |
||
| 77 | $this->request = $request ?? \Request::instance(); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * [getRequest description]. |
||
| 82 | * @return [type] [description] |
||
| 83 | */ |
||
| 84 | public function getRequest() |
||
| 85 | { |
||
| 86 | return $this->request; |
||
| 87 | } |
||
| 88 | |||
| 89 | // ------------------------------------------------------ |
||
| 90 | // BASICS - model, route, entity_name, entity_name_plural |
||
| 91 | // ------------------------------------------------------ |
||
| 92 | |||
| 93 | /** |
||
| 94 | * This function binds the CRUD to its corresponding Model (which extends Eloquent). |
||
| 95 | * All Create-Read-Update-Delete operations are done using that Eloquent Collection. |
||
| 96 | * |
||
| 97 | * @param string $model_namespace Full model namespace. Ex: App\Models\Article |
||
| 98 | * |
||
| 99 | * @throws \Exception in case the model does not exist |
||
| 100 | */ |
||
| 101 | public function setModel($model_namespace) |
||
| 102 | { |
||
| 103 | if (! class_exists($model_namespace)) { |
||
| 104 | throw new \Exception('The model does not exist.', 500); |
||
| 105 | } |
||
| 106 | |||
| 107 | if (! method_exists($model_namespace, 'hasCrudTrait')) { |
||
| 108 | throw new \Exception('Please use CrudTrait on the model.', 500); |
||
| 109 | } |
||
| 110 | |||
| 111 | $this->model = new $model_namespace(); |
||
| 112 | $this->query = $this->model->select('*'); |
||
| 113 | $this->entry = null; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the corresponding Eloquent Model for the CrudController, as defined with the setModel() function. |
||
| 118 | * |
||
| 119 | * @return string|\Illuminate\Database\Eloquent\Model |
||
| 120 | */ |
||
| 121 | public function getModel() |
||
| 122 | { |
||
| 123 | return $this->model; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get the database connection, as specified in the .env file or overwritten by the property on the model. |
||
| 128 | * |
||
| 129 | * @return \Illuminate\Database\Schema\Builder |
||
| 130 | */ |
||
| 131 | private function getSchema() |
||
| 132 | { |
||
| 133 | return $this->getModel()->getConnection()->getSchemaBuilder(); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Check if the database connection driver is using mongodb. |
||
| 138 | * |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | private function driverIsMongoDb() |
||
| 142 | { |
||
| 143 | return $this->getSchema()->getConnection()->getConfig()['driver'] === 'mongodb'; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Set the route for this CRUD. |
||
| 148 | * Ex: admin/article. |
||
| 149 | * |
||
| 150 | * @param string $route Route name. |
||
| 151 | */ |
||
| 152 | public function setRoute($route) |
||
| 153 | { |
||
| 154 | $this->route = $route; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Set the route for this CRUD using the route name. |
||
| 159 | * Ex: admin.article. |
||
| 160 | * |
||
| 161 | * @param string $route Route name. |
||
| 162 | * @param array $parameters Parameters. |
||
| 163 | * |
||
| 164 | * @throws \Exception |
||
| 165 | */ |
||
| 166 | public function setRouteName($route, $parameters = []) |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the current CrudController route. |
||
| 180 | * |
||
| 181 | * Can be defined in the CrudController with: |
||
| 182 | * - $this->crud->setRoute(config('backpack.base.route_prefix').'/article') |
||
| 183 | * - $this->crud->setRouteName(config('backpack.base.route_prefix').'.article') |
||
| 184 | * - $this->crud->route = config('backpack.base.route_prefix')."/article" |
||
| 185 | * |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public function getRoute() |
||
| 189 | { |
||
| 190 | return $this->route; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Set the entity name in singular and plural. |
||
| 195 | * Used all over the CRUD interface (header, add button, reorder button, breadcrumbs). |
||
| 196 | * |
||
| 197 | * @param string $singular Entity name, in singular. Ex: article |
||
| 198 | * @param string $plural Entity name, in plural. Ex: articles |
||
| 199 | */ |
||
| 200 | public function setEntityNameStrings($singular, $plural) |
||
| 201 | { |
||
| 202 | $this->entity_name = $singular; |
||
| 203 | $this->entity_name_plural = $plural; |
||
| 204 | } |
||
| 205 | |||
| 206 | // ----------------------------------------------- |
||
| 207 | // ACTIONS - the current operation being processed |
||
| 208 | // ----------------------------------------------- |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Get the action being performed by the controller, |
||
| 212 | * including middleware names, route name, method name, |
||
| 213 | * namespace, prefix, etc. |
||
| 214 | * |
||
| 215 | * @return string The EntityCrudController route action array. |
||
| 216 | */ |
||
| 217 | public function getAction() |
||
| 218 | { |
||
| 219 | return $this->getRequest()->route()->getAction(); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get the full name of the controller method |
||
| 224 | * currently being called (including namespace). |
||
| 225 | * |
||
| 226 | * @return string The EntityCrudController full method name with namespace. |
||
| 227 | */ |
||
| 228 | public function getActionName() |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Get the name of the controller method |
||
| 235 | * currently being called. |
||
| 236 | * |
||
| 237 | * @return string The EntityCrudController method name. |
||
| 238 | */ |
||
| 239 | public function getActionMethod() |
||
| 240 | { |
||
| 241 | return $this->getRequest()->route()->getActionMethod(); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Check if the controller method being called |
||
| 246 | * matches a given string. |
||
| 247 | * |
||
| 248 | * @param string $methodName Name of the method (ex: index, create, update) |
||
| 249 | * |
||
| 250 | * @return bool Whether the condition is met or not. |
||
| 251 | */ |
||
| 252 | public function actionIs($methodName) |
||
| 253 | { |
||
| 254 | return $methodName === $this->getActionMethod(); |
||
| 255 | } |
||
| 256 | |||
| 257 | // ---------------------------------- |
||
| 258 | // Miscellaneous functions or methods |
||
| 259 | // ---------------------------------- |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Return the first element in an array that has the given 'type' attribute. |
||
| 263 | * |
||
| 264 | * @param string $type |
||
| 265 | * @param array $array |
||
| 266 | * |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | public function getFirstOfItsTypeInArray($type, $array) |
||
| 273 | }); |
||
| 274 | } |
||
| 275 | |||
| 276 | // ------------ |
||
| 277 | // TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED IN THIS FILE |
||
| 278 | // ------------ |
||
| 279 | // |
||
| 280 | // TODO: |
||
| 281 | // - figure out if they are really needed |
||
| 282 | // - comments inside the function to explain how they work |
||
| 283 | // - write docblock for them |
||
| 284 | // - place in the correct section above (CREATE, READ, UPDATE, DELETE, ACCESS, MANIPULATION) |
||
| 285 | |||
| 286 | public function sync($type, $fields, $attributes) |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get the Eloquent Model name from the given relation definition string. |
||
| 301 | * |
||
| 302 | * @example For a given string 'company' and a relation between App/Models/User and App/Models/Company, defined by a |
||
| 303 | * company() method on the user model, the 'App/Models/Company' string will be returned. |
||
| 304 | * @example For a given string 'company.address' and a relation between App/Models/User, App/Models/Company and |
||
| 305 | * App/Models/Address defined by a company() method on the user model and an address() method on the |
||
| 306 | * company model, the 'App/Models/Address' string will be returned. |
||
| 307 | * |
||
| 308 | * @param string $relationString Relation string. A dot notation can be used to chain multiple relations. |
||
| 309 | * @param int $length Optionally specify the number of relations to omit from the start of the relation string. If |
||
| 310 | * the provided length is negative, then that many relations will be omitted from the end of the relation |
||
| 311 | * string. |
||
| 312 | * @param \Illuminate\Database\Eloquent\Model $model Optionally specify a different model than the one in the crud object. |
||
| 313 | * |
||
| 314 | * @return string Relation model name. |
||
| 315 | */ |
||
| 316 | public function getRelationModel($relationString, $length = null, $model = null) |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Get the given attribute from a model or models resulting from the specified relation string (eg: the list of streets from |
||
| 343 | * the many addresses of the company of a given user). |
||
| 344 | * |
||
| 345 | * @param \Illuminate\Database\Eloquent\Model $model Model (eg: user). |
||
| 346 | * @param string $relationString Model relation. Can be a string representing the name of a relation method in the given |
||
| 347 | * Model or one from a different Model through multiple relations. A dot notation can be used to specify |
||
| 348 | * multiple relations (eg: user.company.address). |
||
| 349 | * @param string $attribute The attribute from the relation model (eg: the street attribute from the address model). |
||
| 350 | * |
||
| 351 | * @return array An array containing a list of attributes from the resulting model. |
||
| 352 | */ |
||
| 353 | public function getRelatedEntriesAttributes($model, $relationString, $attribute) |
||
| 354 | { |
||
| 355 | $endModels = $this->getRelatedEntries($model, $relationString); |
||
| 356 | $attributes = []; |
||
| 357 | foreach ($endModels as $model => $entries) { |
||
| 358 | $model_instance = new $model(); |
||
| 359 | $modelKey = $model_instance->getKeyName(); |
||
| 360 | |||
| 361 | if (is_array($entries)) { |
||
| 362 | //if attribute does not exist in main array we have more than one entry OR the attribute |
||
| 363 | //is an acessor that is not in $appends property of model. |
||
| 364 | if (! isset($entries[$attribute])) { |
||
| 365 | //we first check if we don't have the attribute because it's and acessor that is not in appends. |
||
| 366 | if ($model_instance->hasGetMutator($attribute) && isset($entries[$modelKey])) { |
||
| 367 | $entry_in_database = $model_instance->find($entries[$modelKey]); |
||
| 368 | $attributes[$entry_in_database->{$modelKey}] = $this->parseTranslatableAttributes($model_instance, $attribute, $entry_in_database->{$attribute}); |
||
| 369 | } else { |
||
| 370 | //we have multiple entries |
||
| 371 | //for each entry we check if $attribute exists in array or try to check if it's an acessor. |
||
| 372 | foreach ($entries as $entry) { |
||
| 373 | if (isset($entry[$attribute])) { |
||
| 374 | $attributes[$entry[$modelKey]] = $this->parseTranslatableAttributes($model_instance, $attribute, $entry[$attribute]); |
||
| 375 | } else { |
||
| 376 | if ($model_instance->hasGetMutator($attribute)) { |
||
| 377 | $entry_in_database = $model_instance->find($entry[$modelKey]); |
||
| 378 | $attributes[$entry_in_database->{$modelKey}] = $this->parseTranslatableAttributes($model_instance, $attribute, $entry_in_database->{$attribute}); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | } |
||
| 382 | } |
||
| 383 | } else { |
||
| 384 | //if we have the attribute we just return it, does not matter if it is direct attribute or an acessor added in $appends. |
||
| 385 | $attributes[$entries[$modelKey]] = $this->parseTranslatableAttributes($model_instance, $attribute, $entries[$attribute]); |
||
| 386 | } |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | return $attributes; |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Parse translatable attributes from a model or models resulting from the specified relation string. |
||
| 395 | * |
||
| 396 | * @param \Illuminate\Database\Eloquent\Model $model Model (eg: user). |
||
| 397 | * @param string $attribute The attribute from the relation model (eg: the street attribute from the address model). |
||
| 398 | * @param string $value Attribute value translatable or not |
||
| 399 | * |
||
| 400 | * @return string A string containing the translated attributed based on app()->getLocale() |
||
| 401 | */ |
||
| 402 | public function parseTranslatableAttributes($model, $attribute, $value) |
||
| 403 | { |
||
| 404 | if (! method_exists($model, 'isTranslatableAttribute')) { |
||
| 405 | return $value; |
||
| 406 | } |
||
| 407 | |||
| 408 | if (! $model->isTranslatableAttribute($attribute)) { |
||
| 409 | return $value; |
||
| 410 | } |
||
| 411 | |||
| 412 | if (! is_array($value)) { |
||
| 413 | $decodedAttribute = json_decode($value, true); |
||
| 414 | } else { |
||
| 415 | $decodedAttribute = $value; |
||
| 416 | } |
||
| 417 | |||
| 418 | if (is_array($decodedAttribute) && ! empty($decodedAttribute)) { |
||
| 419 | if (isset($decodedAttribute[app()->getLocale()])) { |
||
| 420 | return $decodedAttribute[app()->getLocale()]; |
||
| 421 | } else { |
||
| 422 | return Arr::first($decodedAttribute); |
||
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | return $value; |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Traverse the tree of relations for the given model, defined by the given relation string, and return the ending |
||
| 431 | * associated model instance or instances. |
||
| 432 | * |
||
| 433 | * @param \Illuminate\Database\Eloquent\Model $model The CRUD model. |
||
| 434 | * @param string $relationString Relation string. A dot notation can be used to chain multiple relations. |
||
| 435 | * |
||
| 436 | * @return array An array of the associated model instances defined by the relation string. |
||
| 437 | */ |
||
| 438 | private function getRelatedEntries($model, $relationString) |
||
| 470 | } |
||
| 471 | } |
||
| 472 |