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