Completed
Push — master ( 1e6f86...3701fc )
by wen
02:30
created

ConfigFactory   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 5
dl 0
loc 130
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A make() 0 10 1
A getConfigFile() 0 6 1
A getConfigRepository() 0 4 1
A getTitle() 0 8 2
A getPermissions() 0 10 2
A getColumns() 0 13 2
A getElements() 0 12 3
A getModel() 0 7 2
A getRules() 0 4 1
A getConfigs() 0 10 1
A __toString() 0 4 1
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\ConfigFactoryInterface;
13
use Sco\Attributes\HasAttributesTrait;
14
15
class ConfigFactory implements ConfigFactoryInterface, 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)
30
    {
31
        $this->app = $app;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function make($name)
38
    {
39
        $this->name = $name;
40
41
        $this->configRepository = new ConfigRepository(
42
            $this->app['files']->getRequire($this->getConfigFile())
43
        );
44
45
        return $this;
46
    }
47
48
    private function getConfigFile()
49
    {
50
        return $this->app['path.config']
51
            . DIRECTORY_SEPARATOR . 'admin'
52
            . DIRECTORY_SEPARATOR . $this->name . '.php';
53
    }
54
55
    public function getConfigRepository()
56
    {
57
        return $this->configRepository;
58
    }
59
60
    public function getTitle()
61
    {
62
        if (!$this->title) {
63
            $this->title = $this->configRepository->get('title');
64
        }
65
66
        return $this->title;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getPermissions()
73
    {
74
        if (!$this->permissions) {
75
            $config = $this->configRepository->get('permissions');
76
77
            $this->permissions = new PermissionsConfig($config);
78
        }
79
80
        return $this->permissions;
81
    }
82
83
    public function getColumns()
84
    {
85
        if (!$this->columns) {
86
            $config = $this->configRepository->get('columns');
87
88
            $this->columns = collect($config)->mapWithKeys(function ($item, $key) {
89
                $columnClass = config('admin.column');
90
                return [$key => new $columnClass($key, $item)];
91
            });
92
        }
93
94
        return $this->columns;
95
    }
96
97
    protected function getElements()
98
    {
99
        if (!$this->elements) {
100
            $config         = $this->configRepository->get('elements');
101
            $this->elements = collect($config)->mapWithKeys(function ($item, $key) {
102
                $type = isset($item['type']) ? $item['type'] : 'text';
103
                return [$key => AdminElement::{$type}($key, $item['title'])];
104
            });
105
        }
106
107
        return $this->elements;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function getModel()
114
    {
115
        if (!$this->model) {
116
            $this->model = new ModelConfig($this->app, $this);
117
        }
118
        return $this->model;
119
    }
120
121
    public function getRules()
122
    {
123
        return $this->configRepository->get('rules');
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function getConfigs()
130
    {
131
        return [
132
            'primaryKey'  => $this->getModel()->getRepository()->getKeyName(),
133
            'title'       => $this->getTitle(),
134
            'permissions' => $this->getPermissions(),
135
            'columns'     => $this->getColumns()->values(),
136
            'elements'    => $this->getElements()->values(),
137
        ];
138
    }
139
140
    public function __toString()
141
    {
142
        return $this->toJson();
143
    }
144
}
145