We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 54 |
Total Lines | 474 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
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 |
||
37 | class CrudPanel |
||
38 | { |
||
39 | // load all the default CrudPanel features |
||
40 | 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; |
||
41 | // allow developers to add their own closures to this object |
||
42 | use Macroable; |
||
43 | |||
44 | // -------------- |
||
45 | // CRUD variables |
||
46 | // -------------- |
||
47 | // These variables are passed to the CRUD views, inside the $crud variable. |
||
48 | // All variables are public, so they can be modified from your EntityCrudController. |
||
49 | // All functions and methods are also public, so they can be used in your EntityCrudController to modify these variables. |
||
50 | |||
51 | public $model = "\App\Models\Entity"; // what's the namespace for your entity's model |
||
52 | public $route; // what route have you defined for your entity? used for links. |
||
53 | public $entity_name = 'entry'; // what name will show up on the buttons, in singural (ex: Add entity) |
||
54 | public $entity_name_plural = 'entries'; // what name will show up on the buttons, in plural (ex: Delete 5 entities) |
||
55 | |||
56 | public $entry; |
||
57 | |||
58 | protected $request; |
||
59 | |||
60 | private $enableApproachFormRequest; |
||
61 | // The following methods are used in CrudController or your EntityCrudController to manipulate the variables above. |
||
62 | |||
63 | public function __construct() |
||
64 | { |
||
65 | $this->setRequest(); |
||
66 | |||
67 | if ($this->getCurrentOperation()) { |
||
68 | $this->setOperation($this->getCurrentOperation()); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Set the request instance for this CRUD. |
||
74 | * |
||
75 | * @param \Illuminate\Http\Request $request |
||
76 | */ |
||
77 | public function setRequest($request = null) |
||
78 | { |
||
79 | $this->request = $request ?? \Request::instance(); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * [getRequest description]. |
||
84 | * @return [type] [description] |
||
85 | */ |
||
86 | public function getRequest() |
||
87 | { |
||
88 | return $this->request; |
||
89 | } |
||
90 | |||
91 | // ------------------------------------------------------ |
||
92 | // BASICS - model, route, entity_name, entity_name_plural |
||
93 | // ------------------------------------------------------ |
||
94 | |||
95 | /** |
||
96 | * This function binds the CRUD to its corresponding Model (which extends Eloquent). |
||
97 | * All Create-Read-Update-Delete operations are done using that Eloquent Collection. |
||
98 | * |
||
99 | * @param string $model_namespace Full model namespace. Ex: App\Models\Article |
||
100 | * |
||
101 | * @throws \Exception in case the model does not exist |
||
102 | */ |
||
103 | public function setModel($model_namespace) |
||
104 | { |
||
105 | if (! class_exists($model_namespace)) { |
||
106 | throw new \Exception('The model does not exist.', 500); |
||
107 | } |
||
108 | |||
109 | if (! method_exists($model_namespace, 'hasCrudTrait')) { |
||
110 | throw new \Exception('Please use CrudTrait on the model.', 500); |
||
111 | } |
||
112 | |||
113 | $this->model = new $model_namespace(); |
||
114 | $this->query = $this->model->select('*'); |
||
115 | $this->entry = null; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Get the corresponding Eloquent Model for the CrudController, as defined with the setModel() function. |
||
120 | * |
||
121 | * @return string|\Illuminate\Database\Eloquent\Model |
||
122 | */ |
||
123 | public function getModel() |
||
124 | { |
||
125 | return $this->model; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Get the database connection, as specified in the .env file or overwritten by the property on the model. |
||
130 | * |
||
131 | * @return \Illuminate\Database\Schema\Builder |
||
132 | */ |
||
133 | private function getSchema() |
||
134 | { |
||
135 | return $this->getModel()->getConnection()->getSchemaBuilder(); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Check if the database connection driver is using mongodb. |
||
140 | * |
||
141 | * DEPRECATION NOTICE: This method is no longer used and will be removed in future versions of Backpack |
||
142 | * |
||
143 | * @deprecated |
||
144 | * @return bool |
||
145 | */ |
||
146 | private function driverIsMongoDb() |
||
147 | { |
||
148 | return $this->getSchema()->getConnection()->getConfig()['driver'] === 'mongodb'; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Check if the database connection is any sql driver. |
||
153 | * |
||
154 | * @return bool |
||
155 | */ |
||
156 | private function driverIsSql() |
||
157 | { |
||
158 | $driver = $this->getSchema()->getConnection()->getConfig('driver'); |
||
159 | |||
160 | return in_array($driver, $this->getSqlDriverList()); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Get SQL driver list. |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | public function getSqlDriverList() |
||
169 | { |
||
170 | return ['mysql', 'sqlsrv', 'sqlite', 'pgsql']; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Set the route for this CRUD. |
||
175 | * Ex: admin/article. |
||
176 | * |
||
177 | * @param string $route Route name. |
||
178 | */ |
||
179 | public function setRoute($route) |
||
180 | { |
||
181 | $this->route = $route; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Set the route for this CRUD using the route name. |
||
186 | * Ex: admin.article. |
||
187 | * |
||
188 | * @param string $route Route name. |
||
189 | * @param array $parameters Parameters. |
||
190 | * |
||
191 | * @throws \Exception |
||
192 | */ |
||
193 | public function setRouteName($route, $parameters = []) |
||
194 | { |
||
195 | $complete_route = $route.'.index'; |
||
196 | |||
197 | if (! \Route::has($complete_route)) { |
||
198 | throw new \Exception('There are no routes for this route name.', 404); |
||
199 | } |
||
200 | |||
201 | $this->route = route($complete_route, $parameters); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Get the current CrudController route. |
||
206 | * |
||
207 | * Can be defined in the CrudController with: |
||
208 | * - $this->crud->setRoute(config('backpack.base.route_prefix').'/article') |
||
209 | * - $this->crud->setRouteName(config('backpack.base.route_prefix').'.article') |
||
210 | * - $this->crud->route = config('backpack.base.route_prefix')."/article" |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | public function getRoute() |
||
215 | { |
||
216 | return $this->route; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Set the entity name in singular and plural. |
||
221 | * Used all over the CRUD interface (header, add button, reorder button, breadcrumbs). |
||
222 | * |
||
223 | * @param string $singular Entity name, in singular. Ex: article |
||
224 | * @param string $plural Entity name, in plural. Ex: articles |
||
225 | */ |
||
226 | public function setEntityNameStrings($singular, $plural) |
||
227 | { |
||
228 | $this->entity_name = $singular; |
||
229 | $this->entity_name_plural = $plural; |
||
230 | } |
||
231 | |||
232 | // ----------------------------------------------- |
||
233 | // ACTIONS - the current operation being processed |
||
234 | // ----------------------------------------------- |
||
235 | |||
236 | /** |
||
237 | * Get the action being performed by the controller, |
||
238 | * including middleware names, route name, method name, |
||
239 | * namespace, prefix, etc. |
||
240 | * |
||
241 | * @return string The EntityCrudController route action array. |
||
242 | */ |
||
243 | public function getAction() |
||
244 | { |
||
245 | return $this->getRequest()->route()->getAction(); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Get the full name of the controller method |
||
250 | * currently being called (including namespace). |
||
251 | * |
||
252 | * @return string The EntityCrudController full method name with namespace. |
||
253 | */ |
||
254 | public function getActionName() |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Get the name of the controller method |
||
261 | * currently being called. |
||
262 | * |
||
263 | * @return string The EntityCrudController method name. |
||
264 | */ |
||
265 | public function getActionMethod() |
||
266 | { |
||
267 | return $this->getRequest()->route()->getActionMethod(); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Check if the controller method being called |
||
272 | * matches a given string. |
||
273 | * |
||
274 | * @param string $methodName Name of the method (ex: index, create, update) |
||
275 | * |
||
276 | * @return bool Whether the condition is met or not. |
||
277 | */ |
||
278 | public function actionIs($methodName) |
||
279 | { |
||
280 | return $methodName === $this->getActionMethod(); |
||
281 | } |
||
282 | |||
283 | // ---------------------------------- |
||
284 | // Miscellaneous functions or methods |
||
285 | // ---------------------------------- |
||
286 | |||
287 | /** |
||
288 | * Return the first element in an array that has the given 'type' attribute. |
||
289 | * |
||
290 | * @param string $type |
||
291 | * @param array $array |
||
292 | * |
||
293 | * @return array |
||
294 | */ |
||
295 | public function getFirstOfItsTypeInArray($type, $array) |
||
299 | }); |
||
300 | } |
||
301 | |||
302 | // ------------ |
||
303 | // TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED IN THIS FILE |
||
304 | // ------------ |
||
305 | // |
||
306 | // TODO: |
||
307 | // - figure out if they are really needed |
||
308 | // - comments inside the function to explain how they work |
||
309 | // - write docblock for them |
||
310 | // - place in the correct section above (CREATE, READ, UPDATE, DELETE, ACCESS, MANIPULATION) |
||
311 | |||
312 | public function sync($type, $fields, $attributes) |
||
313 | { |
||
314 | if (! empty($this->{$type})) { |
||
315 | $this->{$type} = array_map(function ($field) use ($fields, $attributes) { |
||
316 | if (in_array($field['name'], (array) $fields)) { |
||
317 | $field = array_merge($field, $attributes); |
||
318 | } |
||
319 | |||
320 | return $field; |
||
321 | }, $this->{$type}); |
||
322 | } |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Get the Eloquent Model name from the given relation definition string. |
||
327 | * |
||
328 | * @example For a given string 'company' and a relation between App/Models/User and App/Models/Company, defined by a |
||
329 | * company() method on the user model, the 'App/Models/Company' string will be returned. |
||
330 | * @example For a given string 'company.address' and a relation between App/Models/User, App/Models/Company and |
||
331 | * App/Models/Address defined by a company() method on the user model and an address() method on the |
||
332 | * company model, the 'App/Models/Address' string will be returned. |
||
333 | * |
||
334 | * @param string $relationString Relation string. A dot notation can be used to chain multiple relations. |
||
335 | * @param int $length Optionally specify the number of relations to omit from the start of the relation string. If |
||
336 | * the provided length is negative, then that many relations will be omitted from the end of the relation |
||
337 | * string. |
||
338 | * @param \Illuminate\Database\Eloquent\Model $model Optionally specify a different model than the one in the crud object. |
||
339 | * |
||
340 | * @return string Relation model name. |
||
341 | */ |
||
342 | public function getRelationModel($relationString, $length = null, $model = null) |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Get the given attribute from a model or models resulting from the specified relation string (eg: the list of streets from |
||
369 | * the many addresses of the company of a given user). |
||
370 | * |
||
371 | * @param \Illuminate\Database\Eloquent\Model $model Model (eg: user). |
||
372 | * @param string $relationString Model relation. Can be a string representing the name of a relation method in the given |
||
373 | * Model or one from a different Model through multiple relations. A dot notation can be used to specify |
||
374 | * multiple relations (eg: user.company.address). |
||
375 | * @param string $attribute The attribute from the relation model (eg: the street attribute from the address model). |
||
376 | * |
||
377 | * @return array An array containing a list of attributes from the resulting model. |
||
378 | */ |
||
379 | public function getRelatedEntriesAttributes($model, $relationString, $attribute) |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Parse translatable attributes from a model or models resulting from the specified relation string. |
||
421 | * |
||
422 | * @param \Illuminate\Database\Eloquent\Model $model Model (eg: user). |
||
423 | * @param string $attribute The attribute from the relation model (eg: the street attribute from the address model). |
||
424 | * @param string $value Attribute value translatable or not |
||
425 | * |
||
426 | * @return string A string containing the translated attributed based on app()->getLocale() |
||
427 | */ |
||
428 | public function parseTranslatableAttributes($model, $attribute, $value) |
||
429 | { |
||
430 | if (! method_exists($model, 'isTranslatableAttribute')) { |
||
431 | return $value; |
||
432 | } |
||
433 | |||
434 | if (! $model->isTranslatableAttribute($attribute)) { |
||
435 | return $value; |
||
436 | } |
||
437 | |||
438 | if (! is_array($value)) { |
||
439 | $decodedAttribute = json_decode($value, true); |
||
440 | } else { |
||
441 | $decodedAttribute = $value; |
||
442 | } |
||
443 | |||
444 | if (is_array($decodedAttribute) && ! empty($decodedAttribute)) { |
||
445 | if (isset($decodedAttribute[app()->getLocale()])) { |
||
446 | return $decodedAttribute[app()->getLocale()]; |
||
447 | } else { |
||
448 | return Arr::first($decodedAttribute); |
||
449 | } |
||
450 | } |
||
451 | |||
452 | return $value; |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Traverse the tree of relations for the given model, defined by the given relation string, and return the ending |
||
457 | * associated model instance or instances. |
||
458 | * |
||
459 | * @param \Illuminate\Database\Eloquent\Model $model The CRUD model. |
||
460 | * @param string $relationString Relation string. A dot notation can be used to chain multiple relations. |
||
461 | * |
||
462 | * @return array An array of the associated model instances defined by the relation string. |
||
463 | */ |
||
464 | private function getRelatedEntries($model, $relationString) |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * Enables use of the FormRequest Laravel class, completely. |
||
499 | */ |
||
500 | public function enableApproachFormRequest() |
||
501 | { |
||
502 | $this->enableApproachFormRequest = true; |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * @return bool |
||
507 | */ |
||
508 | public function isEnableApproachFormRequest() |
||
511 | } |
||
512 | } |
||
513 |