Passed
Push — master ( db0af9...5e9f02 )
by Ferry
03:57
created

ColumnSingleton   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 75
dl 0
loc 191
rs 8.96
c 0
b 0
f 0
wmc 43

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getJoin() 0 3 1
A getColumns() 0 3 1
A newColumns() 0 2 1
A addJoin() 0 3 1
A getEditColumns() 0 11 3
A getIndexColumns() 0 11 3
A getAddEditColumns() 0 11 4
A getAddColumns() 0 11 3
B valueAssignment() 0 22 7
A getAssignmentData() 0 15 4
A getColumnNameOnly() 0 8 2
A getColumn() 0 2 1
A setColumn() 0 3 1
A setColumnArray() 0 3 1
A getFilterableColumns() 0 11 3
A removeColumn() 0 11 4
A getDetailColumns() 0 11 3

How to fix   Complexity   

Complex Class

Complex classes like ColumnSingleton often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ColumnSingleton, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: User
5
 * Date: 1/26/2019
6
 * Time: 6:03 PM
7
 */
8
9
namespace crocodicstudio\crudbooster\controllers\scaffolding\singletons;
10
11
12
use crocodicstudio\crudbooster\models\ColumnModel;
13
14
class ColumnSingleton
15
{
16
17
    private $columns;
18
    private $joins;
19
20
    public function newColumns() {
21
        $this->columns = [];
22
    }
23
24
    public function addJoin($data)
25
    {
26
        $this->joins[md5(serialize($data))] = $data;
27
    }
28
29
    public function getJoin()
30
    {
31
        return $this->joins;
32
    }
33
34
    /**
35
     * @return mixed
36
     */
37
    public function getColumns()
38
    {
39
        return $this->columns;
40
    }
41
42
    public function valueAssignment($data_row = null) {
43
        foreach ($this->getColumns() as $index=>$column) {
44
45
            if (! $column->getName()) {
46
                continue;
47
            }
48
49
            /** @var ColumnModel $column */
50
            if($data_row) {
51
                $value = (isset($data_row->{$column->getField()}))?$data_row->{ $column->getField() }:null;
52
            }else{
53
                $value = request($column->getName());
54
55
                if(!$value && $column->getDefaultValue()) {
56
                    $value = $column->getDefaultValue();
57
                }
58
59
                $value = getTypeHook($column->getType())->assignment($value, $column);
60
            }
61
62
            $column->setValue($value);
63
            $this->setColumn($index, $column);
64
        }
65
    }
66
67
    public function getIndexColumns()
68
    {
69
        $data = $this->columns;
70
        $newData = [];
71
        foreach($data as $i=>$item) {
72
            /** @var ColumnModel $item */
73
            if($item->getShowIndex()) {
74
                $newData[] = $item;
75
            }
76
        }
77
        return $newData;
78
    }
79
80
    public function getAddEditColumns()
81
    {
82
        $data = $this->columns;
83
        $newData = [];
84
        foreach($data as $i=>$item) {
85
            /** @var ColumnModel $item */
86
            if($item->getShowAdd() || $item->getShowEdit()) {
87
                $newData[] = $item;
88
            }
89
        }
90
        return $newData;
91
    }
92
93
    public function getEditColumns()
94
    {
95
        $data = $this->columns;
96
        $newData = [];
97
        foreach($data as $i=>$item) {
98
            /** @var ColumnModel $item */
99
            if($item->getShowEdit()) {
100
                $newData[] = $item;
101
            }
102
        }
103
        return $newData;
104
    }
105
106
    public function getAddColumns()
107
    {
108
        $data = $this->columns;
109
        $newData = [];
110
        foreach($data as $i=>$item) {
111
            /** @var ColumnModel $item */
112
            if($item->getShowAdd()) {
113
                $newData[] = $item;
114
            }
115
        }
116
        return $newData;
117
    }
118
119
    public function getFilterableColumns()
120
    {
121
        $data = $this->columns;
122
        $newData = [];
123
        foreach($data as $i=>$item) {
124
            /** @var ColumnModel $item */
125
            if($item->getFilterable()) {
126
                $newData[] = $item;
127
            }
128
        }
129
        return $newData;
130
    }
131
132
    public function getDetailColumns()
133
    {
134
        $data = $this->columns;
135
        $newData = [];
136
        foreach($data as $i=>$item) {
137
            /** @var ColumnModel $item */
138
            if($item->getShowDetail()) {
139
                $newData[] = $item;
140
            }
141
        }
142
        return $newData;
143
    }
144
145
    public function getAssignmentData()
146
    {
147
        $data = [];
148
        foreach($this->columns as $column) {
149
            /** @var ColumnModel $column */
150
151
            if(is_array($column->getValue())) {
152
                foreach($column->getValue() as $key=>$val) {
153
                    $data[$key] = $val;
154
                }
155
            }else{
156
                $data[$column->getField()] = $column->getValue();
157
            }
158
        }
159
        return $data;
160
    }
161
162
    public function removeColumn($label_or_name)
163
    {
164
        $data = $this->getColumns();
165
        foreach($data as $i=>$d)
166
        {
167
            /** @var ColumnModel $d */
168
            if($d->getLabel() == $label_or_name || $d->getName() == $label_or_name) {
169
                unset($data[$i]);
170
            }
171
        }
172
        $this->columns = $data;
173
    }
174
175
    public function getColumnNameOnly()
176
    {
177
        $result = [];
178
        foreach($this->columns as $column) {
179
            /** @var ColumnModel $column */
180
            $result[] = $column->getName();
181
        }
182
        return $result;
183
    }
184
185
    /**
186
     * @param int $index
187
     * @param ColumnModel $value
188
     */
189
    public function setColumn($index, ColumnModel $value)
190
    {
191
        $this->columns[$index] = $value;
192
    }
193
194
    /**
195
     * @param int $index
196
     * @return ColumnModel
197
     */
198
    public function getColumn($index) {
199
        return $this->columns[$index];
200
    }
201
202
    public function setColumnArray($index, $key, $values)
203
    {
204
        $this->columns[$index][$key][] = $values;
205
    }
206
}