AdminNewProductController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 29
c 1
b 0
f 0
dl 0
loc 45
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A page() 0 2 1
A createNewProduct() 0 39 2
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\Product;
11
use Mongi\Mongicommerce\Models\ProductItem;
12
use Mongi\Mongicommerce\Models\ProductItemDetail;
13
14
class AdminNewProductController extends Controller
15
{
16
    public function page(){
17
        return view('mongicommerce::admin.pages.products.new_product');
18
    }
19
20
    public function createNewProduct(Request $r){
21
22
        $r->validate([
23
            'product_name' => 'required',
24
            'product_description' => 'required',
25
            'category_id' => 'required',
26
            'image' => 'required',
27
        ]);
28
29
        $product_name = $r->get('product_name');
30
        $product_description = $r->get('product_description');
31
        $category_id = $r->get('category_id');
32
        $get_image = $r->get('image');
33
34
35
        $product = new Product();
36
        $product->name = $product_name;
37
        $product->description = $product_description;
38
        $product->category_id = $category_id;
39
        $product->save();
40
41
        $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

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

41
        $base64_str = substr(/** @scrutinizer ignore-type */ $get_image, strpos($get_image, ",")+1);
Loading history...
42
        $image = base64_decode($base64_str);
43
        $destinationPath = public_path().'/uploads/products_img/'.$product->id.'/';
44
        $destinationPathDB = url('/').'/uploads/products_img/'.$product->id.'/';
45
46
        if(!File::isDirectory($destinationPath)){
47
            File::makeDirectory($destinationPath, $mode = 0777, true, true);
48
        }
49
        $image_name = time().'.'.'jpeg';
50
        $path_file = $destinationPath.$image_name;
51
        $dbPath = $destinationPathDB.$image_name;
52
        file_put_contents($path_file, $image);
53
54
        $product->image = $dbPath;
55
        $product->save();
56
57
58
        return response()->json(['product_id' => $product->id]);
59
    }
60
}
61