| Total Complexity | 45 |
| Total Lines | 230 |
| 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() |
||
| 99 | } |
||
| 100 | |||
| 101 | private function setTableData(): void |
||
| 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 | try { |
||
| 130 | return Db::name('revision')->insertGetId($data); |
||
| 131 | } catch (Exception $e) { |
||
| 132 | throw new Exception(__('write revision data failed')); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | public function save(string $title) |
||
| 137 | { |
||
| 138 | try { |
||
| 139 | $this->setTableData(); |
||
| 140 | $revisionId = $this->saveRevision($title); |
||
| 141 | return $revisionId; |
||
| 142 | } catch (Exception $e) { |
||
| 143 | throw new Exception($e->getMessage()); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | private function initRevisionData() |
||
| 148 | { |
||
| 149 | $revision = Db::table('revision')->where('id', $this->revisionId)->find(); |
||
| 150 | if ($revision) { |
||
| 151 | $this->mainTableData = $revision['main_data']; |
||
| 152 | $this->i18nTableData = $revision['i18n_data']; |
||
| 153 | $this->extraTableData = $revision['extra_data']; |
||
| 154 | return [ |
||
| 155 | 'tableName' => $revision['table_name'], |
||
| 156 | 'originalId' => $revision['original_id'], |
||
| 157 | 'title' => $revision['title'], |
||
| 158 | ]; |
||
| 159 | } |
||
| 160 | return []; |
||
| 161 | } |
||
| 162 | |||
| 163 | private function ifRevisionMatchOriginal(array $revisionData): bool |
||
| 164 | { |
||
| 165 | return ($revisionData['tableName'] === $this->tableName) && ($revisionData['originalId'] === $this->originalId); |
||
| 166 | } |
||
| 167 | |||
| 168 | private function updateMainTableData() |
||
| 169 | { |
||
| 170 | Db::name($this->tableName)->where('id', $this->originalId)->update($this->getMainTableData()); |
||
| 171 | } |
||
| 172 | |||
| 173 | private function deleteOriginalI18nData() |
||
| 174 | { |
||
| 175 | Db::table($this->i18nTableName)->where('original_id', $this->originalId)->delete(); |
||
| 176 | } |
||
| 177 | |||
| 178 | private function deleteOriginalExtraTableData() |
||
| 179 | { |
||
| 180 | if (!empty($this->extra)) { |
||
| 181 | if (isAssocArray($this->extra)) { |
||
| 182 | foreach ($this->extra as $tableName => $idFieldName) { |
||
| 183 | Db::table($tableName)->where($idFieldName, $this->originalId)->delete(); |
||
| 184 | } |
||
| 185 | } else { |
||
| 186 | $idFieldName = $this->tableName . '_id'; |
||
| 187 | foreach ($this->extra as $tableName) { |
||
| 188 | Db::table($tableName)->where($idFieldName, $this->originalId)->delete(); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | private function insertI18nTableData() |
||
| 195 | { |
||
| 196 | Db::name($this->i18nTableName)->insertAll($this->getI18nTableData()); |
||
| 197 | } |
||
| 198 | |||
| 199 | private function insertExtraTableData() |
||
| 200 | { |
||
| 201 | if (!empty($this->extra) && !empty($this->getExtraTableData())) { |
||
| 202 | foreach ($this->getExtraTableData() as $tableName => $records) { |
||
| 203 | Db::name($tableName)->insertAll($records); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | public function restore(int $revisionId) |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | private function getAllColumnNamesWithoutId(array $record): array |
||
|
|
|||
| 227 | { |
||
| 228 | $names = array_keys($record); |
||
| 229 | return array_diff($names, ['id', '_id']); |
||
| 230 | } |
||
| 231 | |||
| 232 | private function tableExists(string $tableName): bool |
||
| 240 | } |
||
| 241 | } |
||
| 242 |
This check looks for private methods that have been defined, but are not used inside the class.