We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 44 |
Total Lines | 372 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 2 | 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, ViewsAndRestoresRevisions, 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) |
||
287 | { |
||
288 | if (! empty($this->{$type})) { |
||
289 | $this->{$type} = array_map(function ($field) use ($fields, $attributes) { |
||
290 | if (in_array($field['name'], (array) $fields)) { |
||
291 | $field = array_merge($field, $attributes); |
||
292 | } |
||
293 | |||
294 | return $field; |
||
295 | }, $this->{$type}); |
||
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) |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Get the given attribute from a model or models resulting from the specified relation string (eg: the list of streets from |
||
337 | * the many addresses of the company of a given user). |
||
338 | * |
||
339 | * @param \Illuminate\Database\Eloquent\Model $model Model (eg: user). |
||
340 | * @param string $relationString Model relation. Can be a string representing the name of a relation method in the given |
||
341 | * Model or one from a different Model through multiple relations. A dot notation can be used to specify |
||
342 | * multiple relations (eg: user.company.address). |
||
343 | * @param string $attribute The attribute from the relation model (eg: the street attribute from the address model). |
||
344 | * |
||
345 | * @return array An array containing a list of attributes from the resulting model. |
||
346 | */ |
||
347 | public function getRelatedEntriesAttributes($model, $relationString, $attribute) |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Traverse the tree of relations for the given model, defined by the given relation string, and return the ending |
||
369 | * associated model instance or instances. |
||
370 | * |
||
371 | * @param \Illuminate\Database\Eloquent\Model $model The CRUD model. |
||
372 | * @param string $relationString Relation string. A dot notation can be used to chain multiple relations. |
||
373 | * |
||
374 | * @return array An array of the associated model instances defined by the relation string. |
||
375 | */ |
||
376 | private function getRelatedEntries($model, $relationString) |
||
410 |