Completed
Push — master ( b4a558...04db93 )
by wen
03:09
created

ConfigFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace Sco\Admin\Config;
4
5
use JsonSerializable;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Contracts\Support\Jsonable;
8
use Illuminate\Config\Repository as ConfigRepository;
9
use Illuminate\Foundation\Application;
10
use Sco\Admin\Column\Columns;
11
use Sco\Admin\Contracts\Config as ConfigContract;
12
use Sco\Attributes\HasOriginalAndAttributesTrait;
13
14
class ConfigFactory implements ConfigContract, Arrayable, Jsonable, JsonSerializable
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: jsonSerialize, toArray, toJson
Loading history...
15
{
16
    use HasOriginalAndAttributesTrait;
17
18
    protected $app;
19
    protected $name;
20
    protected $config;
21
22
    protected $title;
23
    protected $permissions;
24
    protected $columns;
25
    protected $model;
26
27
    public function __construct(Application $app, $name)
28
    {
29
        $this->app  = $app;
30
        $this->name = $name;
31
32
        $this->config = new ConfigRepository(
33
            $this->app['files']->getRequire($this->getConfigFile())
34
        );
35
    }
36
37
    private function getConfigFile()
38
    {
39
        return $this->app['path.config']
40
            . DIRECTORY_SEPARATOR . 'admin'
41
            . DIRECTORY_SEPARATOR . $this->name . '.php';
42
    }
43
44
    public function getTitle()
45
    {
46
        if (!$this->title) {
47
            $this->title = $this->config->get('title');
48
        }
49
50
        return $this->title;
51
    }
52
53
    /**
54
     * @return \Sco\Admin\Config\PermissionsConfig
55
     */
56
    public function getPermissions()
57
    {
58
        if (!$this->permissions) {
59
            $config = $this->config->get('permissions');
60
61
            $this->permissions = new PermissionsConfig($config);
62
        }
63
64
        return $this->permissions;
65
    }
66
67
    public function getColumns()
68
    {
69
        if (!$this->columns) {
70
            $config = $this->config->get('columns');
71
72
            $this->columns = new Columns($config);
73
        }
74
75
        return $this->columns;
76
    }
77
78
    protected function getFields()
79
    {
80
        $fields = $this->getAttribute('fields', collect());
81
        if ($fields->isEmpty()) {
82
            $options = $this->getOriginal('fields');
83
84
            foreach ($options as $option) {
85
                $fields->push($option);
86
            }
87
            $this->setAttribute('columns', $fields);
88
        }
89
90
        return $fields;
91
    }
92
93
    /**
94
     * @return \Sco\Admin\Config\ModelConfig
95
     */
96
    public function getModel()
97
    {
98
        if (!$this->model) {
99
            $class       = $this->config->get('model');
100
            $this->model = new ModelConfig($this, new $class());
101
        }
102
        return $this->model;
103
    }
104
105
    public function getConfigs()
106
    {
107
        $this->setAttribute([
108
            'title'       => $this->getTitle(),
109
            'permissions' => $this->getPermissions(),
110
            'columns'     => $this->getColumns(),
111
        ]);
112
113
        return $this->getAttributes();
114
    }
115
116
117
    public function __toString()
118
    {
119
        return $this->toJson();
120
    }
121
}
122