AdminNewSingleProductController::page()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace Mongi\Mongicommerce\Http\Controllers\admin;
5
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\File;
8
use Mongi\Mongicommerce\Http\Controllers\Controller;
9
use Mongi\Mongicommerce\Models\Category;
10
use Mongi\Mongicommerce\Models\Product;
11
use Mongi\Mongicommerce\Models\ProductConfigurationField;
12
use Mongi\Mongicommerce\Models\ProductItem;
13
use Mongi\Mongicommerce\Models\ProductItemDetail;
14
15
class AdminNewSingleProductController extends Controller
16
{
17
    public function page(){
18
        $caregories = Category::all();
19
        return view('mongicommerce::admin.pages.products.new_single_product',['categories' => $caregories]);
20
    }
21
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);
0 ignored issues
show
Bug introduced by
It seems like $r->get('configuration_fields') 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

30
        $configuration_fields = json_decode(/** @scrutinizer ignore-type */ $r->get('configuration_fields'),true);
Loading history...
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);
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

46
        $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

46
        $base64_str = substr(/** @scrutinizer ignore-type */ $get_image, strpos($get_image, ",")+1);
Loading history...
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
        }
81
82
    }
83
}
84