ShopShowVariationProductController::getData()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 40
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 40
rs 9.1608
cc 5
nc 9
nop 1
1
<?php
2
3
4
namespace Mongi\Mongicommerce\Http\Controllers\shop;
5
6
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\DB;
9
use Mongi\Mongicommerce\Http\Controllers\Controller;
10
use Mongi\Mongicommerce\Models\ProductItemDetail;
11
12
class ShopShowVariationProductController extends Controller
13
{
14
    public function getData(Request $r)
15
    {
16
        $r->validate([
17
            'product_id' => 'required'
18
        ]);
19
20
        $informations = $r->get('informations');
21
        $product_id = $r->get('product_id');
22
        $flash_data = $r->get('flash_data');
23
        session()->flash('filters', $flash_data);
24
25
        $g = '';
26
        foreach ($informations as $key => $information) {
27
            $information = (object)$information;
28
            $product_detail_id = $information->product_detail_id;
29
            $product_detail_value_id = $information->product_detail_value_id;
30
            $condition = '';
31
32
            if(count($informations) == $key +1){
33
                $condition = '';
34
            }else{
35
                $condition = 'or';
36
            }
37
            $g .= ('(a.product_detail_id ='.$product_detail_id.' and a.product_detail_value_id ='.$product_detail_value_id.') '.$condition.'');
38
        }
39
        $q = DB::table('product_item_details AS p')->select(DB::raw('product_item_id, count(*) as num_details'))->groupByRaw('product_item_id having num_details = ( select count(*) from product_item_details a where a.product_item_id = p.product_item_id and ('.$g.'))');
40
41
        $result = $q->first();
42
43
        if($result){
44
            $filters = $result->num_details >= count($informations);
45
            if($filters){
46
                $product_item_id = $result->product_item_id;
47
48
                return response()->json(route('shop.single.product',[$product_id,$product_item_id]));
49
            }else{
50
                return response()->json(false);
51
            }
52
        }else{
53
            return response()->json(false);
54
        }
55
56
57
    }
58
}
59