AdminCategoryController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
eloc 37
dl 0
loc 61
rs 10
c 1
b 0
f 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A page() 0 2 1
A setNewCategory() 0 13 1
A getCategories() 0 3 1
A recursiveChildren() 0 11 2
A getCategoryTree() 0 7 2
A getStructureCategories() 0 13 2
1
<?php
2
3
4
namespace Mongi\Mongicommerce\Http\Controllers\admin;
5
6
7
use Illuminate\Http\Request;
8
use Mongi\Mongicommerce\Http\Controllers\Controller;
9
use Mongi\Mongicommerce\Models\Category;
10
11
class AdminCategoryController extends Controller
12
{
13
    public function page(){
14
        return view('mongicommerce::admin.pages.category.new_category');
15
    }
16
17
    public function setNewCategory(Request $r){
18
        $r->validate([
19
            'name' => 'required',
20
            'description' => 'max:255'
21
        ]);
22
        $name = $r->get('name');
23
        $description = $r->get('description');
24
        $parent_id = $r->get('parent_id');
25
        $category = new Category();
26
        $category->name = $name;
27
        $category->description = $description;
28
        $category->parent_id = $parent_id;
29
        $category->save();
30
    }
31
32
    public function getCategories(){
33
        $data = $this->getCategoryTree();
34
        return Response()->json($data);
35
    }
36
37
    function getCategoryTree($parent_id = null, $spacing = '', $tree_array = array()) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
38
        $categories = Category::select('id', 'name', 'parent_id')->where('parent_id' ,'=', $parent_id)->orderBy('parent_id')->get();
39
        foreach ($categories as $item){
40
            $tree_array[] = ['id' => $item->id, 'name' =>$spacing . $item->name] ;
41
            $tree_array = $this->getCategoryTree($item->id, $spacing . '- ', $tree_array);
42
        }
43
        return $tree_array;
44
    }
45
46
    public function getStructureCategories(){
47
48
        $categories = Category::with('children')->whereNull('parent_id')->get();
49
        $tree = [];
50
        foreach($categories as $category){
51
                $tree[] = [
52
                    'id' => $category->id,
53
                    'text' => $category->name,
54
                    'state' => ['opened'=>true],
55
                    'children' =>  $this->recursiveChildren($category->children)
56
                ];
57
        }
58
        return response()->json($tree);
59
    }
60
61
    private function recursiveChildren($childrens){
62
        $childs = [];
63
        foreach ($childrens as $children){
64
            $childs[] = [
65
                'id' => $children->id,
66
                'text' => $children->name,
67
                'state' => ['opened'=>true],
68
                'children' => $this->recursiveChildren($children->children)
69
            ];
70
        }
71
        return $childs;
72
    }
73
74
75
}
76