CategoryRepository::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * @author  Eddy <[email protected]>
4
 */
5
6
namespace App\Repository\Admin;
7
8
use App\Model\Admin\Category;
9
use App\Repository\Searchable;
10
11
class CategoryRepository
12
{
13
    use Searchable;
14
15
    public static function listSingle($perPage, $condition = [])
16
    {
17
        $data = Category::query()
18
            ->where(function ($query) use ($condition) {
19
                Searchable::buildQuery($query, $condition);
20
            })
21
            ->orderBy('id', 'desc')
22
            ->paginate($perPage);
23
        $data->transform(function ($item) {
24
            xssFilter($item);
25
            $item->editUrl = route('admin::category.edit', ['id' => $item->id]);
26
            $item->parentName = $item->pid == 0 ? '顶级菜单' : $item->parent->name;
27
            $item->entityName = $item->entity ? $item->entity->name : '';
28
            unset($item->entity);
29
            return $item;
30
        });
31
32
        return [
33
            'code' => 0,
34
            'msg' => '',
35
            'count' => $data->total(),
36
            'data' => $data->items(),
37
        ];
38
    }
39
40
    public static function list($perPage, $condition = [])
41
    {
42
        $list = [];
43
        $data = Category::query()
44
            ->where(function ($query) use ($condition) {
45
                Searchable::buildQuery($query, $condition);
46
            })
47
            ->orderBy('id', 'desc')
48
            ->paginate($perPage);
49
        $data->each(function ($item, $key) use (&$list) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

49
        $data->each(function ($item, /** @scrutinizer ignore-unused */ $key) use (&$list) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
            xssFilter($item);
51
            $item->editUrl = route('admin::category.edit', ['id' => $item->id]);
52
            $item->parentName = $item->pid == 0 ? '顶级菜单' : $item->parent->name;
53
            $item->entityName = $item->entity ? $item->entity->name : '';
54
            unset($item->entity);
55
56
            array_push($list, $item);
57
58
            $item->children->each(function ($v, $k) use (&$list) {
0 ignored issues
show
Unused Code introduced by
The parameter $k is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

58
            $item->children->each(function ($v, /** @scrutinizer ignore-unused */ $k) use (&$list) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
                $v->editUrl = route('admin::category.edit', ['id' => $v->id]);
60
                //$v->parentName = $v->pid == 0 ? '顶级菜单' : $v->parent->name;
61
                //$v->entityName = $v->entity ? $v->entity->name : '';
62
                $v->name = '|--------' . $v->name;
63
                array_push($list, $v);
64
            });
65
            unset($item->children);
66
        });
67
68
        return [
69
            'code' => 0,
70
            'msg' => '',
71
            'count' => count($list),
72
            'data' => $list,
73
        ];
74
    }
75
76
    public static function add($data)
77
    {
78
        return Category::query()->create($data);
79
    }
80
81
    public static function update($id, $data)
82
    {
83
        return Category::query()->where('id', $id)->update($data);
84
    }
85
86
    public static function find($id)
87
    {
88
        return Category::query()->find($id);
89
    }
90
91
    public static function tree($entity_id = null, $pid = 0, $all = null, $level = 0, $path = [])
92
    {
93
        if (is_null($all)) {
94
            if (is_null($entity_id)) {
95
                $all = Category::select('id', 'pid', 'name', 'order')->get();
96
            } else {
97
                $all = Category::select('id', 'pid', 'name', 'order')->where('model_id', $entity_id)->get();
98
            }
99
        }
100
        return $all->where('pid', $pid)
101
            ->map(function (Category $model) use ($all, $level, $path, $entity_id) {
102
                $data = [
103
                    'id' => $model->id,
104
                    'name' => $model->name,
105
                    'level' => $level,
106
                    'pid' => $model->pid,
107
                    'path' => $path,
108
                    'order' => $model->order,
109
                ];
110
111
                $child = $all->where('pid', $model->id);
112
                if ($child->isEmpty()) {
113
                    return $data;
114
                }
115
116
                array_push($path, $model->id);
117
                $data['children'] = self::tree($entity_id, $model->id, $all, $level + 1, $path);
118
                return $data;
119
            })->sortBy('order');
120
    }
121
}
122