createNewVariation()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 64
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 51
c 2
b 0
f 0
dl 0
loc 64
rs 9.069
cc 4
nc 8
nop 1

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
4
namespace Mongi\Mongicommerce\Http\Controllers\admin;
5
6
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\File;
9
use Mongi\Mongicommerce\Http\Controllers\Controller;
10
use Mongi\Mongicommerce\Models\ConfigurationField;
11
use Mongi\Mongicommerce\Models\Product;
12
use Mongi\Mongicommerce\Models\ProductConfigurationField;
13
use Mongi\Mongicommerce\Models\ProductItem;
14
use Mongi\Mongicommerce\Models\ProductItemDetail;
15
16
class AdminNewProductVariationController extends Controller
17
{
18
    public function page($id_product){
19
        $product = Product::find($id_product);
20
        $items = $product->items;
21
22
        return view('mongicommerce::admin.pages.products.new_product_variation',['product' => $product,'items' => $items]);
23
    }
24
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);
0 ignored issues
show
Bug introduced by
It seems like $r->get('details') can also be of type null; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        $details = json_decode(/** @scrutinizer ignore-type */ $r->get('details'),true);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $get_image can also be of type null; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        $base64_str = substr($get_image, strpos(/** @scrutinizer ignore-type */ $get_image, ",")+1);
Loading history...
Bug introduced by
It seems like $get_image can also be of type null; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        $base64_str = substr(/** @scrutinizer ignore-type */ $get_image, strpos($get_image, ",")+1);
Loading history...
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
        }
90
91
92
        //ceare la tabella ProductConfigurationField
93
        //id, product_item_id, configuration_field_id, configuration_field_value
94
95
    }
96
}
97