DynamicUnit   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 1
Metric Value
wmc 11
eloc 13
c 4
b 2
f 1
dl 0
loc 121
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A findListByCode() 0 3 1
A createUnit() 0 3 1
A convertUnit() 0 4 1
A getAll() 0 3 1
A findAttributeByCode() 0 3 1
A updateUnit() 0 3 1
A deleteAttribute() 0 3 1
A createAttribute() 0 3 1
A findByCode() 0 3 1
A deleteUnit() 0 3 1
A updateAttribute() 0 3 1
1
<?php
2
namespace SimpleCMS\DynamicUnit\Packages;
3
4
use Illuminate\Http\UploadedFile;
5
use Illuminate\Support\Collection;
6
use SimpleCMS\DynamicUnit\Models\DynamicAttribute;
7
use SimpleCMS\DynamicUnit\Models\DynamicUnit as DynamicUnitModel;
8
9
class DynamicUnit
10
{
11
    /**
12
     * 获取所有参数
13
     * @return Collection
14
     */
15
    public function getAll(): Collection
16
    {
17
        return $this->convertUnit(DynamicUnitModel::all());
18
    }
19
20
    private function convertUnit(Collection $collection)
21
    {
22
        return $collection->map(function (DynamicUnitModel $item) {
23
            return new DynamicModel($item->id, $item->name, $item->code, $item->items->map(fn(DynamicAttribute $dynamicAttribute) => new AttributeModel($dynamicAttribute->id, $dynamicAttribute->name, $dynamicAttribute->code, $dynamicAttribute->thumbnail)));
24
        });
25
    }
26
27
    /**
28
     * 按标识查询
29
     * @param string $code
30
     * @return mixed
31
     */
32
    public function findByCode(string $code)
33
    {
34
        return $this->convertUnit(DynamicUnitModel::where('code', $code)->get())->first();
35
    }
36
37
    /**
38
     * 获取属性选项
39
     * @param string $code
40
     * @return mixed
41
     */
42
    public function findListByCode(string $code)
43
    {
44
        return $this->findByCode($code)->items;
45
    }
46
47
    /**
48
     * 获取单一属性
49
     * @param string $code
50
     * @param string $attribute_code
51
     * @return mixed
52
     */
53
    public function findAttributeByCode(string $code, string $attribute_code)
54
    {
55
        return $this->findListByCode($code)->where('code', $attribute_code)->first();
56
    }
57
58
    /**
59
     * 创建动态单元
60
     *
61
     * @author Dennis Lui <[email protected]>
62
     * @param  array<string,int|string|array<string,int|string|UploadedFile>>   $array
63
     * @return bool
64
     */
65
    public function createUnit(array $array): bool
66
    {
67
        return DynamicQuery::create($array, 'unit');
68
    }
69
70
    /**
71
     * 更新动态单元
72
     *
73
     * @author Dennis Lui <[email protected]>
74
     * @param  int $id
75
     * @param  array<string,int|string|array<string,int|string|UploadedFile>>   $array
76
     * @return bool
77
     */
78
    public function updateUnit(int $id, array $array): bool
79
    {
80
        return DynamicQuery::update($id, $array, 'unit');
81
    }
82
83
    /**
84
     * 删除动态单元
85
     *
86
     * @author Dennis Lui <[email protected]>
87
     * @param  int $id
88
     * @return bool
89
     */
90
    public function deleteUnit(int $id): bool
91
    {
92
        return DynamicQuery::delete($id, 'unit');
93
    }
94
95
    /**
96
     * 创建动态单元值
97
     *
98
     * @author Dennis Lui <[email protected]>
99
     * @param  array<string,int|string|UploadedFile>   $array
100
     * @return bool
101
     */
102
    public function createAttribute(array $array): bool
103
    {
104
        return DynamicQuery::create($array, 'attribute');
105
    }
106
107
    /**
108
     * 更新动态单元值
109
     *
110
     * @author Dennis Lui <[email protected]>
111
     * @param  int $id
112
     * @param  array<string,int|string|UploadedFile>   $array
113
     * @return bool
114
     */
115
    public function updateAttribute(int $id, array $array): bool
116
    {
117
        return DynamicQuery::update($id, $array, 'attribute');
118
    }
119
120
    /**
121
     * 删除动态单元值
122
     *
123
     * @author Dennis Lui <[email protected]>
124
     * @param  int $id
125
     * @return bool
126
     */
127
    public function deleteAttribute(int $id): bool
128
    {
129
        return DynamicQuery::delete($id, 'attribute');
130
    }
131
}