Conditions | 36 |
Paths | 1 |
Total Lines | 216 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php namespace App\Modules\Core\BaseClasses; |
||
188 | public function save(array $data) |
||
189 | { |
||
190 | \Session::put('locale', 'all'); |
||
191 | $model = false; |
||
192 | $modelClass = $this->model; |
||
193 | $relations = []; |
||
194 | |||
195 | \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
||
196 | /** |
||
197 | * If the id is present in the data then select the model for updating, |
||
198 | * else create new model. |
||
199 | * @var array |
||
200 | */ |
||
201 | $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
||
202 | if (! $model) { |
||
203 | \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Construct the model object with the given data, |
||
208 | * and if there is a relation add it to relations array, |
||
209 | * then save the model. |
||
210 | */ |
||
211 | foreach ($data as $key => $value) { |
||
212 | /** |
||
213 | * If the attribute is a relation. |
||
214 | */ |
||
215 | $relation = camel_case($key); |
||
216 | if (method_exists($model, $relation) && \Core::$relation()) { |
||
217 | /** |
||
218 | * Check if the relation is a collection. |
||
219 | */ |
||
220 | if (class_basename($model->$relation) == 'Collection') { |
||
221 | /** |
||
222 | * If the relation has no value then marke the relation data |
||
223 | * related to the model to be deleted. |
||
224 | */ |
||
225 | if (! $value || ! count($value)) { |
||
226 | $relations[$relation] = 'delete'; |
||
227 | } |
||
228 | } |
||
229 | if (is_array($value)) { |
||
230 | /** |
||
231 | * Loop through the relation data. |
||
232 | */ |
||
233 | foreach ($value as $attr => $val) { |
||
234 | /** |
||
235 | * Get the relation model. |
||
236 | */ |
||
237 | $relationBaseModel = \Core::$relation()->model; |
||
238 | |||
239 | /** |
||
240 | * Check if the relation is a collection. |
||
241 | */ |
||
242 | if (class_basename($model->$relation) == 'Collection') { |
||
243 | /** |
||
244 | * If the id is present in the data then select the relation model for updating, |
||
245 | * else create new model. |
||
246 | */ |
||
247 | $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
||
248 | |||
249 | /** |
||
250 | * If model doesn't exists. |
||
251 | */ |
||
252 | if (! $relationModel) { |
||
253 | \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Loop through the relation attributes. |
||
258 | */ |
||
259 | foreach ($val as $attr => $val) { |
||
260 | /** |
||
261 | * Prevent the sub relations or attributes not in the fillable. |
||
262 | */ |
||
263 | if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
||
264 | $relationModel->$attr = $val; |
||
265 | } |
||
266 | } |
||
267 | |||
268 | $relations[$relation][] = $relationModel; |
||
269 | } else { |
||
270 | /** |
||
271 | * Prevent the sub relations. |
||
272 | */ |
||
273 | if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
||
274 | /** |
||
275 | * If the id is present in the data then select the relation model for updating, |
||
276 | * else create new model. |
||
277 | */ |
||
278 | $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
||
279 | |||
280 | /** |
||
281 | * If model doesn't exists. |
||
282 | */ |
||
283 | if (! $relationModel) { |
||
284 | \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
||
285 | } |
||
286 | |||
287 | foreach ($value as $relationAttribute => $relationValue) { |
||
288 | /** |
||
289 | * Prevent attributes not in the fillable. |
||
290 | */ |
||
291 | if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
||
292 | $relationModel->$relationAttribute = $relationValue; |
||
293 | } |
||
294 | } |
||
295 | |||
296 | $relations[$relation] = $relationModel; |
||
297 | } |
||
298 | } |
||
299 | } |
||
300 | } |
||
301 | } elseif (array_search($key, $model->getFillable(), true) !== false) { |
||
302 | /** |
||
303 | * If the attribute isn't a relation and prevent attributes not in the fillable. |
||
304 | */ |
||
305 | $model->$key = $value; |
||
306 | } |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Loop through the relations array. |
||
311 | */ |
||
312 | foreach ($relations as $key => $value) { |
||
313 | /** |
||
314 | * If the relation is marked for delete then delete it. |
||
315 | */ |
||
316 | if ($value == 'delete' && $model->$key()->count()) { |
||
317 | $model->$key()->delete(); |
||
318 | } elseif (gettype($value) == 'array') { |
||
319 | /** |
||
320 | * Save the model. |
||
321 | */ |
||
322 | $model->save(); |
||
323 | $ids = []; |
||
324 | |||
325 | /** |
||
326 | * Loop through the relations. |
||
327 | */ |
||
328 | foreach ($value as $val) { |
||
329 | switch (class_basename($model->$key())) { |
||
330 | /** |
||
331 | * If the relation is one to many then update it's foreign key with |
||
332 | * the model id and save it then add its id to ids array to delete all |
||
333 | * relations who's id isn't in the ids array. |
||
334 | */ |
||
335 | case 'HasMany': |
||
336 | $foreignKeyName = $model->$key()->getForeignKeyName(); |
||
337 | $val->$foreignKeyName = $model->id; |
||
338 | $val->save(); |
||
339 | $ids[] = $val->id; |
||
340 | break; |
||
341 | |||
342 | /** |
||
343 | * If the relation is many to many then add it's id to the ids array to |
||
344 | * attache these ids to the model. |
||
345 | */ |
||
346 | case 'BelongsToMany': |
||
347 | $val->save(); |
||
348 | $ids[] = $val->id; |
||
349 | break; |
||
350 | } |
||
351 | } |
||
352 | switch (class_basename($model->$key())) { |
||
353 | /** |
||
354 | * If the relation is one to many then delete all |
||
355 | * relations who's id isn't in the ids array. |
||
356 | */ |
||
357 | case 'HasMany': |
||
358 | $model->$key()->whereNotIn('id', $ids)->delete(); |
||
359 | break; |
||
360 | |||
361 | /** |
||
362 | * If the relation is many to many then |
||
363 | * detach the previous data and attach |
||
364 | * the ids array to the model. |
||
365 | */ |
||
366 | case 'BelongsToMany': |
||
367 | $model->$key()->detach(); |
||
368 | $model->$key()->attach($ids); |
||
369 | break; |
||
370 | } |
||
371 | } else { |
||
372 | switch (class_basename($model->$key())) { |
||
373 | /** |
||
374 | * If the relation is one to one. |
||
375 | */ |
||
376 | case 'HasOne': |
||
377 | /** |
||
378 | * Save the model. |
||
379 | */ |
||
380 | $model->save(); |
||
381 | $foreignKeyName = $model->$key()->getForeignKeyName(); |
||
382 | $value->$foreignKeyName = $model->id; |
||
383 | $value->save(); |
||
384 | break; |
||
385 | case 'BelongsTo': |
||
386 | /** |
||
387 | * Save the model. |
||
388 | */ |
||
389 | $value->save(); |
||
390 | $model->$key()->associate($value); |
||
391 | break; |
||
392 | } |
||
393 | } |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * Save the model. |
||
398 | */ |
||
399 | $model->save(); |
||
400 | }); |
||
401 | |||
402 | return $model; |
||
403 | } |
||
404 | |||
625 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.