Test Failed
Push — master ( befccc...a4b299 )
by Gianluca
19:49
created

Template   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 19
eloc 66
c 2
b 1
f 0
dl 0
loc 114
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A generateHtmlField() 0 11 2
A getProducts() 0 7 2
A recursiveChildren() 0 11 2
A getStructureCategories() 0 13 2
A getDetailsFields() 0 10 2
A getConfigurationFields() 0 9 2
A buttonCart() 0 2 1
A generateDetailHtml() 0 29 6
1
<?php
2
3
4
namespace Mongi\Mongicommerce\Libraries;
5
6
7
use Illuminate\Http\Request;
8
use Mongi\Mongicommerce\Models\Category;
9
use Mongi\Mongicommerce\Models\Detail;
10
use Mongi\Mongicommerce\Models\DetailValue;
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
17
class Template
18
{
19
    public static function getStructureCategories(){
20
21
        $categories = Category::with('children')->whereNull('parent_id')->get();
22
        $tree = [];
23
        foreach($categories as $category){
24
            $tree[] = [
25
                'id' => $category->id,
26
                'text' => $category->name,
27
                'state' => ['opened'=>true],
28
                'children' =>  self::recursiveChildren($category->children)
29
            ];
30
        }
31
        return $tree;
32
    }
33
34
    private static function recursiveChildren($childrens){
35
        $childs = [];
36
        foreach ($childrens as $children){
37
            $childs[] = [
38
                'id' => $children->id,
39
                'text' => $children->name,
40
                'state' => ['opened'=>true],
41
                'children' =>  self::recursiveChildren($children->children)
42
            ];
43
        }
44
        return $childs;
45
    }
46
47
48
    /**
49
     * @param Int $id
50
     * @return Product
51
     */
52
    public static function getProducts($id = null){
53
        if($id === null){
54
            $products = Product::all();
55
        }else{
56
            $products = Category::find($id)->products;
57
        }
58
        return $products;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $products returns the type Illuminate\Database\Eloquent\Collection which is incompatible with the documented return type Mongi\Mongicommerce\Models\Product.
Loading history...
59
    }
60
61
    public static function getDetailsFields(Product $product,$product_item_id){
62
        $element = '<div class="row">';
63
64
        foreach($product->details->groupBy('product_detail_id') as $key => $details){
65
66
            $element.= self::generateDetailHtml(Detail::find($key),$details->groupBy('product_detail_value_id'),$product->id,$product_item_id);
67
        }
68
        $element.= '<p class="show_error_product" style="color: red; display: none;">Prodotto non disponibile</p>';
69
        $element .= '</div>';
70
        return $element;
71
    }
72
73
    public static function generateHtmlField($type,$value,$label){
74
        if($type !== 'textarea'){
75
            $html = '';
76
            $html .= "<label>{$label}</label>";
77
            $html .= "<input class='form-control' type='{$type}' value='{$value}'>";
78
            return $html;
79
        }else{
80
            $html = '';
81
            $html .= "<label>{$label}</label>";
82
            $html .= "<textarea class='form-control'>{$value}</textarea>";
83
            return $html;
84
        }
85
    }
86
87
    public static function getConfigurationFields($product_item_id){
88
        $configurationFields = ProductConfigurationField::where('product_item_id',$product_item_id)->get();
89
        $element = '';
90
        foreach ($configurationFields as $field){
91
                $element .= '<div class="row">';
92
                $element .= self::generateHtmlField($field->field->type,$field->value,$field->field->name);
93
                $element .= '</div>';
94
            }
95
        return $element;
96
    }
97
98
    public static function buttonCart($product_item_id){
99
        return "<button onclick='addToCart(this)' data-product_item_id='{$product_item_id}' class='btn btn-primary mt-3'>Salva nel carrello</button>";
100
    }
101
102
    public static function generateDetailHtml($detail,$values,$product_id,$product_item_id){
103
104
        $type = $detail->type;
105
        $html = '';
106
        if($type === 'select'){
107
            $html = '<label>'.$detail->name.'</label>';
108
            $html .= '<select onchange="getVariationProduct()" data-product_id="'.$product_id.'" data-detail_id="'.$detail->id .'" class="form-control mongifield_into_product">';
109
            $selected = '';
110
111
            $details = ProductItemDetail::where('product_item_id',$product_item_id)->get();
112
            $filter = [];
113
            foreach ($details as $_detail){
114
                $filter[$_detail->product_detail_id] = $_detail->product_detail_value_id;
115
            }
116
            foreach ($values as $detail_value_id =>$value){
117
                if(isset($filter[$detail->id])){
118
                    if($filter[$detail->id] == $detail_value_id){
119
                        $selected = 'selected';
120
                    }else{
121
                        $selected = '';
122
                    }
123
                }
124
                $html .= '<option '.$selected.' value="'.$detail_value_id.'">'.DetailValue::find($detail_value_id)->value.'</option>';
125
            }
126
            $html .= '</select>';
127
        }
128
129
130
        return $html;
131
    }
132
}
133