Passed
Pull Request — main (#1)
by Aspirant
01:31
created

Revision::updateMainTableData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace aspirantzhang\octopusRevision;
6
7
use think\facade\Db;
8
use think\Exception;
9
10
class Revision
11
{
12
    private $tableName;
13
    private $i18nTableName;
14
    private $originalId;
15
    private $revisionId;
16
    private $mainTableData;
17
    private $i18nTableData;
18
19
    public function __construct(string $tableName, int $originalId)
20
    {
21
        $this->tableName = $tableName;
22
        $this->i18nTableName = $tableName . '_i18n';
23
        $this->originalId = (int)$originalId;
24
        $this->mainTableData = '[]';
25
        $this->i18nTableData = '[]';
26
    }
27
28
    private function setMainTableData(array $data)
29
    {
30
        unset($data['id']);
31
        $this->mainTableData = json_encode($data);
32
    }
33
34
    private function getMainTableData()
35
    {
36
        return json_decode($this->mainTableData, true);
37
    }
38
39
    private function setI18nTableData(array $data)
40
    {
41
        foreach ($data as &$singleI18nRecord) {
42
            unset($singleI18nRecord['_id']);
43
        }
44
        $this->i18nTableData = json_encode($data);
45
    }
46
47
    private function getI18nTableData()
48
    {
49
        return json_decode($this->i18nTableData, true);
50
    }
51
52
    private function i18nTableExists(): bool
53
    {
54
        return $this->tableExists($this->i18nTableName);
55
    }
56
57
    private function setTableData(): void
58
    {
59
        $record = Db::table($this->tableName)->where('id', $this->originalId)->find();
60
        $this->setMainTableData($record);
61
62
        if ($this->i18nTableExists()) {
63
            $i18nRecord = Db::table($this->i18nTableName)->where('original_id', $this->originalId)->select()->toArray();
64
            $this->setI18nTableData($i18nRecord);
65
        }
66
    }
67
68
    private function saveRevision(string $title)
69
    {
70
        $currentTime = date('Y-m-d H:i:s');
71
        $data = [
72
            'table_name' => $this->tableName,
73
            'original_id' => $this->originalId,
74
            'title' => $title,
75
            'main_data' => $this->mainTableData,
76
            'i18n_data' => $this->i18nTableData,
77
            'create_time' => $currentTime,
78
            'update_time' => $currentTime
79
        ];
80
        return Db::name('revision')->insertGetId($data);
81
    }
82
83
    public function add(string $title)
84
    {
85
        $this->setTableData();
86
        $revisionId = $this->saveRevision($title);
87
        return $revisionId;
88
    }
89
90
    private function initRevisionData()
91
    {
92
        $revision = Db::table('revision')->where('id', $this->revisionId)->find();
93
        if ($revision) {
94
            $this->mainTableData = $revision['main_data'];
95
            $this->i18nTableData = $revision['i18n_data'];
96
            return [
97
                'tableName' => $revision['table_name'],
98
                'originalId' => $revision['original_id'],
99
                'title' => $revision['title'],
100
            ];
101
        }
102
        return [];
103
    }
104
105
    private function ifRevisionMathOriginal(array $revisionData): bool
106
    {
107
        return ($revisionData['tableName'] === $this->tableName) && ($revisionData['originalId'] === $this->originalId);
108
    }
109
110
    private function updateMainTableData()
111
    {
112
        Db::name($this->tableName)->where('id', $this->originalId)->update($this->getMainTableData());
113
    }
114
115
    private function deleteOriginalI18nData()
116
    {
117
        Db::table($this->i18nTableName)->where('original_id', $this->originalId)->delete();
118
    }
119
120
    private function insertI18nTableData()
121
    {
122
        $num = Db::name($this->i18nTableName)->insertAll($this->getI18nTableData());
123
        var_dump($num);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($num) looks like debug code. Are you sure you do not want to remove it?
Loading history...
124
    }
125
126
    public function restore(int $revisionId)
127
    {
128
        $this->revisionId = $revisionId;
129
        $revisionData = $this->initRevisionData();
130
        if (false === $this->ifRevisionMathOriginal($revisionData)) {
131
            throw new Exception("The revision does not match the original record.");
132
        }
133
        $this->updateMainTableData();
134
        $this->deleteOriginalI18nData();
135
        $this->insertI18nTableData();
136
    }
137
138
    private function getAllColumnNamesWithoutId(array $record): array
0 ignored issues
show
Unused Code introduced by
The method getAllColumnNamesWithoutId() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
139
    {
140
        $names = array_keys($record);
141
        return array_diff($names, ['id', '_id']);
142
    }
143
144
    private function tableExists(string $tableName): bool
145
    {
146
        try {
147
            Db::query("select 1 from `$tableName` LIMIT 1");
148
        } catch (Exception $e) {
149
            return false;
150
        }
151
        return true;
152
    }
153
}
154