1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Config; |
4
|
|
|
|
5
|
|
|
use Sco\Attributes\HasOriginalAndAttributesTrait; |
6
|
|
|
|
7
|
|
|
abstract class Config |
8
|
|
|
{ |
9
|
|
|
use HasOriginalAndAttributesTrait; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
protected $defaultPermissions = [ |
13
|
|
|
'view' => true, |
14
|
|
|
'create' => true, |
15
|
|
|
'update' => true, |
16
|
|
|
'delete' => true, |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
public function __construct(array $attributes) |
20
|
|
|
{ |
21
|
|
|
$this->setOriginal($attributes); |
22
|
|
|
$this->getOptions(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function getTitle() |
26
|
|
|
{ |
27
|
|
|
$title = $this->getAttribute('title'); |
28
|
|
|
if ($title) { |
29
|
|
|
return $title; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$title = $this->getOriginal('title'); |
33
|
|
|
$this->setAttribute('title', $title); |
34
|
|
|
return $title; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function getPermissions() |
38
|
|
|
{ |
39
|
|
|
$permissions = $this->getAttribute('permissions', collect()); |
40
|
|
|
if ($permissions->isEmpty()) { |
41
|
|
|
$option = $this->getOriginal('permissions'); |
42
|
|
|
if (is_array($option)) { |
43
|
|
|
$option = array_merge($this->defaultPermissions, $option); |
44
|
|
|
foreach ($option as $key => $item) { |
45
|
|
|
$val = $item instanceof \Closure ? $item() : $item; |
46
|
|
|
$permissions->put($key, $val ? true : false); |
47
|
|
|
} |
48
|
|
|
} else { |
49
|
|
|
$val = $option instanceof \Closure ? $option() : $option; |
50
|
|
|
foreach ($this->defaultPermissions as $key => $item) { |
51
|
|
|
$permissions->put($key, $val ? true : false); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
$this->setAttribute('permissions', $permissions); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $permissions; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
protected function getColumns() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$columns = $this->getAttribute('columns', collect()); |
63
|
|
|
if ($columns->isEmpty()) { |
64
|
|
|
$options = $this->getOriginal('columns'); |
65
|
|
|
foreach ($options as $option) { |
66
|
|
|
$columns->push(app('admin.column')->make($option)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->setAttribute('columns', $columns); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $columns; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
protected function getFields() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$fields = $this->getAttribute('fields', collect()); |
78
|
|
|
if ($fields->isEmpty()) { |
79
|
|
|
$options = $this->getOriginal('fields'); |
80
|
|
|
|
81
|
|
|
foreach ($options as $option) { |
82
|
|
|
$fields->push($option); |
83
|
|
|
} |
84
|
|
|
$this->setAttribute('columns', $fields); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $fields; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getOption($key, $default = null) |
91
|
|
|
{ |
92
|
|
|
return $this->getAttribute($key, $default); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getOptions() |
96
|
|
|
{ |
97
|
|
|
$this->getTitle(); |
98
|
|
|
$this->getPermissions(); |
99
|
|
|
$this->getColumns(); |
100
|
|
|
|
101
|
|
|
return $this->getAttributes(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function __toString() |
105
|
|
|
{ |
106
|
|
|
return $this->toJson(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.