Completed
Push — master ( 5e765d...c5065d )
by wen
03:03
created

ConfigFactory::getRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Sco\Admin\Config;
4
5
use AdminElement;
6
use JsonSerializable;
7
use Illuminate\Contracts\Support\Arrayable;
8
use Illuminate\Contracts\Support\Jsonable;
9
use Illuminate\Config\Repository as ConfigRepository;
10
use Illuminate\Foundation\Application;
11
use Sco\Admin\Column\Columns;
12
use Sco\Admin\Contracts\Config as ConfigContract;
13
use Sco\Attributes\HasAttributesTrait;
14
15
class ConfigFactory implements ConfigContract, Arrayable, Jsonable, JsonSerializable
16
{
17
    use HasAttributesTrait;
18
19
    protected $app;
20
    protected $name;
21
    protected $configRepository;
22
23
    protected $title;
24
    protected $permissions;
25
    protected $columns;
26
    protected $model;
27
    protected $elements;
28
29
    public function __construct(Application $app, $name)
30
    {
31
        $this->app  = $app;
32
        $this->name = $name;
33
34
        $this->configRepository = new ConfigRepository(
35
            $this->app['files']->getRequire($this->getConfigFile())
36
        );
37
    }
38
39
    private function getConfigFile()
40
    {
41
        return $this->app['path.config']
42
            . DIRECTORY_SEPARATOR . 'admin'
43
            . DIRECTORY_SEPARATOR . $this->name . '.php';
44
    }
45
46
    public function getConfigRepository()
47
    {
48
        return $this->configRepository;
49
    }
50
51
52
    public function getTitle()
53
    {
54
        if (!$this->title) {
55
            $this->title = $this->configRepository->get('title');
56
        }
57
58
        return $this->title;
59
    }
60
61
    /**
62
     * @return \Sco\Admin\Config\PermissionsConfig
63
     */
64
    public function getPermissions()
65
    {
66
        if (!$this->permissions) {
67
            $config = $this->configRepository->get('permissions');
68
69
            $this->permissions = new PermissionsConfig($config);
70
        }
71
72
        return $this->permissions;
73
    }
74
75
    public function getColumns()
76
    {
77
        if (!$this->columns) {
78
            $config = $this->configRepository->get('columns');
79
80
            $this->columns = collect($config)->mapWithKeys(function ($item, $key) {
81
                $columnClass = config('admin.column');
82
                return [$key => new $columnClass($key, $item)];
83
            });
84
        }
85
86
        return $this->columns;
87
    }
88
89
    protected function getElements()
90
    {
91
        if (!$this->elements) {
92
            $config = $this->configRepository->get('elements');
93
            $this->elements = collect($config)->mapWithKeys(function ($item, $key) {
94
                $type = isset($item['type']) ? $item['type'] : 'text';
95
                return [$key => AdminElement::{$type}($key, $item['title'])];
96
            });
97
        }
98
99
        return $this->elements;
100
    }
101
102
    /**
103
     * @return \Sco\Admin\Config\ModelConfig
104
     */
105
    public function getModel()
106
    {
107
        if (!$this->model) {
108
            $this->model = new ModelConfig($this->app, $this);
109
        }
110
        return $this->model;
111
    }
112
113
    public function getRules()
114
    {
115
        return $this->configRepository->get('rules');
116
    }
117
118
    public function getConfigs()
119
    {
120
        return [
121
            'primaryKey'  => $this->getModel()->getRepository()->getKeyName(),
122
            'title'       => $this->getTitle(),
123
            'permissions' => $this->getPermissions(),
124
            'columns'     => $this->getColumns()->values(),
125
            'elements'      => $this->getElements()->values(),
126
        ];
127
    }
128
129
    public function __toString()
130
    {
131
        return $this->toJson();
132
    }
133
}
134