Conditions | 4 |
Paths | 8 |
Total Lines | 64 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
25 | public function createNewVariation(Request $r){ |
||
26 | $r->validate([ |
||
27 | 'category_id' => 'required', |
||
28 | 'quantity' => 'required', |
||
29 | 'price' => 'required', |
||
30 | 'image' => 'required', |
||
31 | 'details' => 'required|min:4', |
||
32 | ]); |
||
33 | $product_id = $r->get('product_id'); |
||
34 | |||
35 | $details = json_decode($r->get('details'),true); |
||
|
|||
36 | $configuration_fields = json_decode($r->get('configuration_fields'),true); |
||
37 | $quantity = $r->get('quantity'); |
||
38 | $price = $r->get('price'); |
||
39 | $product_name = $r->get('product_name'); |
||
40 | $product_description = $r->get('product_description'); |
||
41 | $get_image = $r->get('image'); |
||
42 | $category_id = $r->get('category_id'); |
||
43 | |||
44 | $product = Product::find($product_id); |
||
45 | |||
46 | $product_item = new ProductItem(); |
||
47 | $product_item->product_id = $product->id; |
||
48 | $product_item->category_id = $category_id; |
||
49 | $product_item->name = $product_name; |
||
50 | $product_item->description = $product_description; |
||
51 | $product_item->image = null; |
||
52 | $product_item->price = $price; |
||
53 | $product_item->quantity = $quantity; |
||
54 | $product_item->save(); |
||
55 | |||
56 | $base64_str = substr($get_image, strpos($get_image, ",")+1); |
||
57 | $image = base64_decode($base64_str); |
||
58 | $destinationPath = public_path().'/uploads/products_img/'.$product_item->product_id.'/'.$product_item->id.'/'; |
||
59 | $destinationPathDB = url('/').'/uploads/products_img/'.$product_item->product_id.'/'.$product_item->id.'/'; |
||
60 | |||
61 | if(!File::isDirectory($destinationPath)){ |
||
62 | File::makeDirectory($destinationPath, $mode = 0777, true, true); |
||
63 | } |
||
64 | |||
65 | $image_name = time().'.'.'jpeg'; |
||
66 | $path_file = $destinationPath.$image_name; |
||
67 | $dbPath = $destinationPathDB.$image_name; |
||
68 | file_put_contents($path_file, $image); |
||
69 | |||
70 | $product_item->image = $dbPath; |
||
71 | $product_item->save(); |
||
72 | |||
73 | foreach($details as $detail){ |
||
74 | $detail_obj = (object) $detail; |
||
75 | $product_detail = new ProductItemDetail(); |
||
76 | $product_detail->product_item_id = $product_item->id; |
||
77 | $product_detail->product_detail_id = $detail_obj->detail_id; |
||
78 | $product_detail->product_detail_value_id = $detail_obj->detail_value; |
||
79 | $product_detail->save(); |
||
80 | } |
||
81 | |||
82 | foreach ($configuration_fields as $conf_field){ |
||
83 | $conf_fields_obj = (object) $conf_field; |
||
84 | $configuration_field = new ProductConfigurationField(); |
||
85 | $configuration_field->product_item_id = $product_item->id; |
||
86 | $configuration_field->config_field_id = $conf_fields_obj->configuration_field_id; |
||
87 | $configuration_field->value = $conf_fields_obj->configuration_field_value; |
||
88 | $configuration_field->save(); |
||
89 | } |
||
97 |