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 | 454 |
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 |
||
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 | // The following methods are used in CrudController or your EntityCrudController to manipulate the variables above. |
||
61 | |||
62 | public function __construct() |
||
63 | { |
||
64 | $this->setRequest(); |
||
65 | |||
66 | if ($this->getCurrentOperation()) { |
||
67 | $this->setOperation($this->getCurrentOperation()); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Set the request instance for this CRUD. |
||
73 | * |
||
74 | * @param \Illuminate\Http\Request $request |
||
75 | */ |
||
76 | public function setRequest($request = null) |
||
77 | { |
||
78 | $this->request = $request ?? \Request::instance(); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * [getRequest description]. |
||
83 | * @return [type] [description] |
||
84 | */ |
||
85 | public function getRequest() |
||
86 | { |
||
87 | return $this->request; |
||
88 | } |
||
89 | |||
90 | // ------------------------------------------------------ |
||
91 | // BASICS - model, route, entity_name, entity_name_plural |
||
92 | // ------------------------------------------------------ |
||
93 | |||
94 | /** |
||
95 | * This function binds the CRUD to its corresponding Model (which extends Eloquent). |
||
96 | * All Create-Read-Update-Delete operations are done using that Eloquent Collection. |
||
97 | * |
||
98 | * @param string $model_namespace Full model namespace. Ex: App\Models\Article |
||
99 | * |
||
100 | * @throws \Exception in case the model does not exist |
||
101 | */ |
||
102 | public function setModel($model_namespace) |
||
103 | { |
||
104 | if (! class_exists($model_namespace)) { |
||
105 | throw new \Exception('The model does not exist.', 500); |
||
106 | } |
||
107 | |||
108 | if (! method_exists($model_namespace, 'hasCrudTrait')) { |
||
109 | throw new \Exception('Please use CrudTrait on the model.', 500); |
||
110 | } |
||
111 | |||
112 | $this->model = new $model_namespace(); |
||
113 | $this->query = $this->model->select('*'); |
||
114 | $this->entry = null; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Get the corresponding Eloquent Model for the CrudController, as defined with the setModel() function. |
||
119 | * |
||
120 | * @return string|\Illuminate\Database\Eloquent\Model |
||
121 | */ |
||
122 | public function getModel() |
||
123 | { |
||
124 | return $this->model; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Get the database connection, as specified in the .env file or overwritten by the property on the model. |
||
129 | * |
||
130 | * @return \Illuminate\Database\Schema\Builder |
||
131 | */ |
||
132 | private function getSchema() |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Check if the database connection driver is using mongodb. |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | private function driverIsMongoDb() |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Check if the database connection is any sql driver. |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | private function driverIsSql() |
||
153 | { |
||
154 | $driver = $this->getSchema()->getConnection()->getConfig()['driver']; |
||
155 | |||
156 | return in_array($driver, $this->getSqlDriverList()); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get SQL driver list. |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | private function getSqlDriverList() |
||
165 | { |
||
166 | return ['mysql', 'sqlsrv', 'sqlite', 'pgsql']; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Set the route for this CRUD. |
||
171 | * Ex: admin/article. |
||
172 | * |
||
173 | * @param string $route Route name. |
||
174 | */ |
||
175 | public function setRoute($route) |
||
176 | { |
||
177 | $this->route = $route; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Set the route for this CRUD using the route name. |
||
182 | * Ex: admin.article. |
||
183 | * |
||
184 | * @param string $route Route name. |
||
185 | * @param array $parameters Parameters. |
||
186 | * |
||
187 | * @throws \Exception |
||
188 | */ |
||
189 | public function setRouteName($route, $parameters = []) |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Get the current CrudController route. |
||
202 | * |
||
203 | * Can be defined in the CrudController with: |
||
204 | * - $this->crud->setRoute(config('backpack.base.route_prefix').'/article') |
||
205 | * - $this->crud->setRouteName(config('backpack.base.route_prefix').'.article') |
||
206 | * - $this->crud->route = config('backpack.base.route_prefix')."/article" |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | public function getRoute() |
||
211 | { |
||
212 | return $this->route; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Set the entity name in singular and plural. |
||
217 | * Used all over the CRUD interface (header, add button, reorder button, breadcrumbs). |
||
218 | * |
||
219 | * @param string $singular Entity name, in singular. Ex: article |
||
220 | * @param string $plural Entity name, in plural. Ex: articles |
||
221 | */ |
||
222 | public function setEntityNameStrings($singular, $plural) |
||
223 | { |
||
224 | $this->entity_name = $singular; |
||
225 | $this->entity_name_plural = $plural; |
||
226 | } |
||
227 | |||
228 | // ----------------------------------------------- |
||
229 | // ACTIONS - the current operation being processed |
||
230 | // ----------------------------------------------- |
||
231 | |||
232 | /** |
||
233 | * Get the action being performed by the controller, |
||
234 | * including middleware names, route name, method name, |
||
235 | * namespace, prefix, etc. |
||
236 | * |
||
237 | * @return string The EntityCrudController route action array. |
||
238 | */ |
||
239 | public function getAction() |
||
240 | { |
||
241 | return $this->getRequest()->route()->getAction(); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Get the full name of the controller method |
||
246 | * currently being called (including namespace). |
||
247 | * |
||
248 | * @return string The EntityCrudController full method name with namespace. |
||
249 | */ |
||
250 | public function getActionName() |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Get the name of the controller method |
||
257 | * currently being called. |
||
258 | * |
||
259 | * @return string The EntityCrudController method name. |
||
260 | */ |
||
261 | public function getActionMethod() |
||
262 | { |
||
263 | return $this->getRequest()->route()->getActionMethod(); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Check if the controller method being called |
||
268 | * matches a given string. |
||
269 | * |
||
270 | * @param string $methodName Name of the method (ex: index, create, update) |
||
271 | * |
||
272 | * @return bool Whether the condition is met or not. |
||
273 | */ |
||
274 | public function actionIs($methodName) |
||
275 | { |
||
276 | return $methodName === $this->getActionMethod(); |
||
277 | } |
||
278 | |||
279 | // ---------------------------------- |
||
280 | // Miscellaneous functions or methods |
||
281 | // ---------------------------------- |
||
282 | |||
283 | /** |
||
284 | * Return the first element in an array that has the given 'type' attribute. |
||
285 | * |
||
286 | * @param string $type |
||
287 | * @param array $array |
||
288 | * |
||
289 | * @return array |
||
290 | */ |
||
291 | public function getFirstOfItsTypeInArray($type, $array) |
||
292 | { |
||
293 | return Arr::first($array, function ($item) use ($type) { |
||
294 | return $item['type'] == $type; |
||
295 | }); |
||
296 | } |
||
297 | |||
298 | // ------------ |
||
299 | // TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED IN THIS FILE |
||
300 | // ------------ |
||
301 | // |
||
302 | // TODO: |
||
303 | // - figure out if they are really needed |
||
304 | // - comments inside the function to explain how they work |
||
305 | // - write docblock for them |
||
306 | // - place in the correct section above (CREATE, READ, UPDATE, DELETE, ACCESS, MANIPULATION) |
||
307 | |||
308 | public function sync($type, $fields, $attributes) |
||
309 | { |
||
310 | if (! empty($this->{$type})) { |
||
311 | $this->{$type} = array_map(function ($field) use ($fields, $attributes) { |
||
312 | if (in_array($field['name'], (array) $fields)) { |
||
313 | $field = array_merge($field, $attributes); |
||
314 | } |
||
315 | |||
316 | return $field; |
||
317 | }, $this->{$type}); |
||
318 | } |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Get the Eloquent Model name from the given relation definition string. |
||
323 | * |
||
324 | * @example For a given string 'company' and a relation between App/Models/User and App/Models/Company, defined by a |
||
325 | * company() method on the user model, the 'App/Models/Company' string will be returned. |
||
326 | * @example For a given string 'company.address' and a relation between App/Models/User, App/Models/Company and |
||
327 | * App/Models/Address defined by a company() method on the user model and an address() method on the |
||
328 | * company model, the 'App/Models/Address' string will be returned. |
||
329 | * |
||
330 | * @param string $relationString Relation string. A dot notation can be used to chain multiple relations. |
||
331 | * @param int $length Optionally specify the number of relations to omit from the start of the relation string. If |
||
332 | * the provided length is negative, then that many relations will be omitted from the end of the relation |
||
333 | * string. |
||
334 | * @param \Illuminate\Database\Eloquent\Model $model Optionally specify a different model than the one in the crud object. |
||
335 | * |
||
336 | * @return string Relation model name. |
||
337 | */ |
||
338 | public function getRelationModel($relationString, $length = null, $model = null) |
||
339 | { |
||
340 | $relationArray = explode('.', $relationString); |
||
341 | |||
342 | if (! isset($length)) { |
||
343 | $length = count($relationArray); |
||
344 | } |
||
345 | |||
346 | if (! isset($model)) { |
||
347 | $model = $this->model; |
||
348 | } |
||
349 | |||
350 | $result = array_reduce(array_splice($relationArray, 0, $length), function ($obj, $method) { |
||
351 | try { |
||
352 | $result = $obj->$method(); |
||
353 | |||
354 | return $result->getRelated(); |
||
355 | } catch (Exception $e) { |
||
356 | return $obj; |
||
357 | } |
||
358 | }, $model); |
||
359 | |||
360 | return get_class($result); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Get the given attribute from a model or models resulting from the specified relation string (eg: the list of streets from |
||
365 | * the many addresses of the company of a given user). |
||
366 | * |
||
367 | * @param \Illuminate\Database\Eloquent\Model $model Model (eg: user). |
||
368 | * @param string $relationString Model relation. Can be a string representing the name of a relation method in the given |
||
369 | * Model or one from a different Model through multiple relations. A dot notation can be used to specify |
||
370 | * multiple relations (eg: user.company.address). |
||
371 | * @param string $attribute The attribute from the relation model (eg: the street attribute from the address model). |
||
372 | * |
||
373 | * @return array An array containing a list of attributes from the resulting model. |
||
374 | */ |
||
375 | public function getRelatedEntriesAttributes($model, $relationString, $attribute) |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Parse translatable attributes from a model or models resulting from the specified relation string. |
||
417 | * |
||
418 | * @param \Illuminate\Database\Eloquent\Model $model Model (eg: user). |
||
419 | * @param string $attribute The attribute from the relation model (eg: the street attribute from the address model). |
||
420 | * @param string $value Attribute value translatable or not |
||
421 | * |
||
422 | * @return string A string containing the translated attributed based on app()->getLocale() |
||
423 | */ |
||
424 | public function parseTranslatableAttributes($model, $attribute, $value) |
||
425 | { |
||
426 | if (! method_exists($model, 'isTranslatableAttribute')) { |
||
427 | return $value; |
||
428 | } |
||
429 | |||
430 | if (! $model->isTranslatableAttribute($attribute)) { |
||
431 | return $value; |
||
432 | } |
||
433 | |||
434 | if (! is_array($value)) { |
||
435 | $decodedAttribute = json_decode($value, true); |
||
436 | } else { |
||
437 | $decodedAttribute = $value; |
||
438 | } |
||
439 | |||
440 | if (is_array($decodedAttribute) && ! empty($decodedAttribute)) { |
||
441 | if (isset($decodedAttribute[app()->getLocale()])) { |
||
442 | return $decodedAttribute[app()->getLocale()]; |
||
443 | } else { |
||
444 | return Arr::first($decodedAttribute); |
||
445 | } |
||
446 | } |
||
447 | |||
448 | return $value; |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * Traverse the tree of relations for the given model, defined by the given relation string, and return the ending |
||
453 | * associated model instance or instances. |
||
454 | * |
||
455 | * @param \Illuminate\Database\Eloquent\Model $model The CRUD model. |
||
456 | * @param string $relationString Relation string. A dot notation can be used to chain multiple relations. |
||
457 | * |
||
458 | * @return array An array of the associated model instances defined by the relation string. |
||
459 | */ |
||
460 | private function getRelatedEntries($model, $relationString) |
||
491 | } |
||
492 | } |
||
493 |