ConfigRepository   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A groupNames() 0 3 1
A all() 0 11 2
A list() 0 20 1
A add() 0 3 1
A find() 0 3 1
A update() 0 3 1
1
<?php
2
/**
3
 * @author  Eddy <[email protected]>
4
 */
5
6
namespace App\Repository\Admin;
7
8
use App\Model\Admin\Config;
9
use App\Repository\Searchable;
10
use Illuminate\Support\Facades\Cache;
11
12
class ConfigRepository
13
{
14
    use Searchable;
15
16
    public static function list($perPage, $condition = [])
17
    {
18
        $data = Config::query()
19
            ->where(function ($query) use ($condition) {
20
                Searchable::buildQuery($query, $condition);
21
            })
22
            ->orderBy('id', 'desc')
23
            ->paginate($perPage);
24
        $data->transform(function ($item) {
25
            xssFilter($item);
26
            $item->editUrl = route('admin::config.edit', ['id' => $item->id]);
27
            $item->type = Config::$types[$item->type];
28
            return $item;
29
        });
30
31
        return [
32
            'code' => 0,
33
            'msg' => '',
34
            'count' => $data->total(),
35
            'data' => $data->items(),
36
        ];
37
    }
38
39
    public static function add($data)
40
    {
41
        return Config::query()->create($data);
42
    }
43
44
    public static function update($id, $data)
45
    {
46
        return Config::query()->where('id', $id)->update($data);
47
    }
48
49
    public static function find($id)
50
    {
51
        return Config::query()->find($id);
52
    }
53
54
    public static function all()
55
    {
56
        return Cache::rememberForever(config('light.cache_key.config'), function () {
57
            $value = Config::query()->select(['key', 'value'])->get();
58
            if ($value->isEmpty()) {
59
                return [];
60
            }
61
62
            return $value->mapWithKeys(function ($item) {
63
                return [$item->key => $item->value];
64
            })->all();
65
        });
66
    }
67
68
    public static function groupNames()
69
    {
70
        return Config::query()->select('group')->distinct()->pluck('group')->toArray();
71
    }
72
}
73