Test Failed
Push — master ( 83b4ef...37cc60 )
by Gianluca
19:45 queued 19:45
created

Template   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 28
c 1
b 0
f 0
dl 0
loc 48
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getStructureCategories() 0 13 2
A recursiveChildren() 0 11 2
A getProducts() 0 13 3
1
<?php
2
3
4
namespace Mongi\Mongicommerce\Libraries;
5
6
7
use Mongi\Mongicommerce\Models\Category;
8
use Mongi\Mongicommerce\Models\Product;
9
10
11
class Template
12
{
13
    public static function getStructureCategories(){
14
15
        $categories = Category::with('children')->whereNull('parent_id')->get();
16
        $tree = [];
17
        foreach($categories as $category){
18
            $tree[] = [
19
                'id' => $category->id,
20
                'text' => $category->name,
21
                'state' => ['opened'=>true],
22
                'children' =>  self::recursiveChildren($category->children)
23
            ];
24
        }
25
        return $tree;
26
    }
27
28
    private static function recursiveChildren($childrens){
29
        $childs = [];
30
        foreach ($childrens as $children){
31
            $childs[] = [
32
                'id' => $children->id,
33
                'text' => $children->name,
34
                'state' => ['opened'=>true],
35
                'children' =>  self::recursiveChildren($children->children)
36
            ];
37
        }
38
        return $childs;
39
    }
40
41
42
    /**
43
     * @param Int $id
44
     * @return Product
45
     */
46
    public static function getProducts($id = null){
47
        if($id === null){
48
            $products = Product::all();
49
        }else{
50
            if(Category::find($id)){
51
                $belong_to_this_category = Category::find($id)->children->pluck('id')->toArray();
0 ignored issues
show
Bug introduced by
The property children does not exist on Mongi\Mongicommerce\Models\Category. Did you mean children_rec?
Loading history...
52
                $result = array_merge($belong_to_this_category, [0 =>$id]);
53
                $products = Product::whereIn('category_id',$result)->get();
54
            }else{
55
                $products = [];
56
            }
57
        }
58
        return $products;
59
    }
60
}
61