Total Complexity | 43 |
Total Lines | 223 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 0 |
Complex classes like Revision 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 Revision, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Revision |
||
11 | { |
||
12 | private $tableName; |
||
13 | private $i18nTableName; |
||
14 | private $originalId; |
||
15 | private $revisionId; |
||
16 | private $mainTableData; |
||
17 | private $i18nTableData; |
||
18 | private $recordUpdateTime; |
||
19 | private $extra; |
||
20 | private $extraTableData; |
||
21 | |||
22 | public function __construct(string $tableName, int $originalId, array $extra = []) |
||
23 | { |
||
24 | $this->tableName = $tableName; |
||
25 | $this->i18nTableName = $tableName . '_i18n'; |
||
26 | $this->originalId = (int)$originalId; |
||
27 | $this->mainTableData = '[]'; |
||
28 | $this->i18nTableData = '[]'; |
||
29 | $this->extraTableData = '[]'; |
||
30 | $this->extra = $extra; |
||
31 | } |
||
32 | |||
33 | private function setMainTableData(array $data) |
||
34 | { |
||
35 | unset($data['id']); |
||
36 | $this->mainTableData = json_encode($data); |
||
37 | } |
||
38 | |||
39 | private function getMainTableData() |
||
40 | { |
||
41 | return json_decode($this->mainTableData, true); |
||
42 | } |
||
43 | |||
44 | private function setI18nTableData(array $data) |
||
45 | { |
||
46 | foreach ($data as &$singleI18nRecord) { |
||
47 | unset($singleI18nRecord['_id']); |
||
48 | } |
||
49 | $this->i18nTableData = json_encode($data); |
||
50 | } |
||
51 | |||
52 | private function setRecordUpdateTime(string $time) |
||
53 | { |
||
54 | $this->recordUpdateTime = $time; |
||
55 | } |
||
56 | |||
57 | private function getI18nTableData() |
||
58 | { |
||
59 | return json_decode($this->i18nTableData, true); |
||
60 | } |
||
61 | |||
62 | private function getExtraTableData() |
||
65 | } |
||
66 | |||
67 | private function i18nTableExists(): bool |
||
68 | { |
||
69 | return $this->tableExists($this->i18nTableName); |
||
70 | } |
||
71 | |||
72 | private function setExtraTableData() |
||
73 | { |
||
74 | if (empty($this->extra)) { |
||
75 | return; |
||
76 | } |
||
77 | $result = []; |
||
78 | if (isAssocArray($this->extra)) { |
||
79 | foreach ($this->extra as $tableName => $idFieldName) { |
||
80 | if ($this->tableExists($tableName)) { |
||
81 | $record = Db::table($tableName)->where($idFieldName, $this->originalId)->select()->toArray(); |
||
82 | foreach ($record as &$arr) { |
||
83 | unset($arr['id']); |
||
84 | } |
||
85 | $result[$tableName] = $record; |
||
86 | } |
||
87 | } |
||
88 | } else { |
||
89 | $idFieldName = $this->tableName . '_id'; |
||
90 | foreach ($this->extra as $tableName) { |
||
91 | $record = Db::table($tableName)->where($idFieldName, $this->originalId)->select()->toArray(); |
||
92 | foreach ($record as &$arr) { |
||
93 | unset($arr['id']); |
||
94 | } |
||
95 | $result[$tableName] = $record; |
||
96 | } |
||
97 | } |
||
98 | $this->extraTableData = json_encode($result); |
||
99 | } |
||
100 | |||
101 | private function setTableData(): void |
||
102 | { |
||
103 | // main table |
||
104 | $record = Db::table($this->tableName)->where('id', $this->originalId)->find(); |
||
105 | $this->setMainTableData($record); |
||
106 | $this->setRecordUpdateTime($record['update_time']); |
||
107 | // i18n table |
||
108 | if ($this->i18nTableExists()) { |
||
109 | $i18nRecord = Db::table($this->i18nTableName)->where('original_id', $this->originalId)->select()->toArray(); |
||
110 | $this->setI18nTableData($i18nRecord); |
||
111 | } |
||
112 | // extra table |
||
113 | $this->setExtraTableData(); |
||
114 | } |
||
115 | |||
116 | private function saveRevision(string $title) |
||
117 | { |
||
118 | $currentTime = date('Y-m-d H:i:s'); |
||
119 | $data = [ |
||
120 | 'table_name' => $this->tableName, |
||
121 | 'original_id' => $this->originalId, |
||
122 | 'title' => $title, |
||
123 | 'main_data' => $this->mainTableData, |
||
124 | 'i18n_data' => $this->i18nTableData, |
||
125 | 'extra_data' => $this->extraTableData, |
||
126 | 'create_time' => $this->recordUpdateTime, |
||
127 | 'update_time' => $currentTime |
||
128 | ]; |
||
129 | return Db::name('revision')->insertGetId($data); |
||
130 | } |
||
131 | |||
132 | public function add(string $title) |
||
133 | { |
||
134 | // TODO: catch exception |
||
135 | $this->setTableData(); |
||
136 | $revisionId = $this->saveRevision($title); |
||
137 | return $revisionId; |
||
138 | } |
||
139 | |||
140 | private function initRevisionData() |
||
141 | { |
||
142 | $revision = Db::table('revision')->where('id', $this->revisionId)->find(); |
||
143 | if ($revision) { |
||
144 | $this->mainTableData = $revision['main_data']; |
||
145 | $this->i18nTableData = $revision['i18n_data']; |
||
146 | $this->extraTableData = $revision['extra_data']; |
||
147 | return [ |
||
148 | 'tableName' => $revision['table_name'], |
||
149 | 'originalId' => $revision['original_id'], |
||
150 | 'title' => $revision['title'], |
||
151 | ]; |
||
152 | } |
||
153 | return []; |
||
154 | } |
||
155 | |||
156 | private function ifRevisionMatchOriginal(array $revisionData): bool |
||
157 | { |
||
158 | return ($revisionData['tableName'] === $this->tableName) && ($revisionData['originalId'] === $this->originalId); |
||
159 | } |
||
160 | |||
161 | private function updateMainTableData() |
||
162 | { |
||
163 | Db::name($this->tableName)->where('id', $this->originalId)->update($this->getMainTableData()); |
||
164 | } |
||
165 | |||
166 | private function deleteOriginalI18nData() |
||
167 | { |
||
168 | Db::table($this->i18nTableName)->where('original_id', $this->originalId)->delete(); |
||
169 | } |
||
170 | |||
171 | private function deleteOriginalExtraTableData() |
||
172 | { |
||
173 | if (!empty($this->extra)) { |
||
174 | if (isAssocArray($this->extra)) { |
||
175 | foreach ($this->extra as $tableName => $idFieldName) { |
||
176 | Db::table($tableName)->where($idFieldName, $this->originalId)->delete(); |
||
177 | } |
||
178 | } else { |
||
179 | $idFieldName = $this->tableName . '_id'; |
||
180 | foreach ($this->extra as $tableName) { |
||
181 | Db::table($tableName)->where($idFieldName, $this->originalId)->delete(); |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | |||
187 | private function insertI18nTableData() |
||
188 | { |
||
189 | Db::name($this->i18nTableName)->insertAll($this->getI18nTableData()); |
||
190 | } |
||
191 | |||
192 | private function insertExtraTableData() |
||
193 | { |
||
194 | if (!empty($this->extra) && !empty($this->getExtraTableData())) { |
||
195 | foreach ($this->getExtraTableData() as $tableName => $records) { |
||
196 | Db::name($tableName)->insertAll($records); |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | |||
201 | public function restore(int $revisionId) |
||
216 | } |
||
217 | } |
||
218 | |||
219 | private function getAllColumnNamesWithoutId(array $record): array |
||
|
|||
220 | { |
||
221 | $names = array_keys($record); |
||
222 | return array_diff($names, ['id', '_id']); |
||
223 | } |
||
224 | |||
225 | private function tableExists(string $tableName): bool |
||
233 | } |
||
234 | } |
||
235 |
This check looks for private methods that have been defined, but are not used inside the class.