Completed
Push — master ( b86bbe...5e765d )
by wen
02:51
created

ConfigFactory   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getModel() 0 7 2
A __construct() 0 9 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 getConfigs() 0 10 1
A __toString() 0 4 1
1
<?php
2
3
namespace Sco\Admin\Config;
4
5
use AdminField;
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 $config;
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->config = 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->config;
49
    }
50
51
52
    public function getTitle()
53
    {
54
        if (!$this->title) {
55
            $this->title = $this->config->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->config->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->config->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->config->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 getConfigs()
114
    {
115
        return [
116
            'primaryKey'  => $this->getModel()->getRepository()->getKeyName(),
117
            'title'       => $this->getTitle(),
118
            'permissions' => $this->getPermissions(),
119
            'columns'     => $this->getColumns()->values(),
120
            'elements'      => $this->getElements()->values(),
121
        ];
122
    }
123
124
    public function __toString()
125
    {
126
        return $this->toJson();
127
    }
128
}
129