Passed
Push — main ( f426a0...b96293 )
by Aspirant
08:52
created

RevisionAPI   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 4
Metric Value
eloc 52
dl 0
loc 83
rs 10
c 5
b 1
f 4
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A readAPI() 0 14 2
A getListData() 0 20 3
A restoreAPI() 0 15 2
A listAPI() 0 20 2
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 RevisionAPI
11
{
12
    public function readAPI(int $id)
13
    {
14
        $data = Db::table('revision')->where('id', (int)$id)->find();
15
        if ($data) {
16
            return [
17
                'success' => true,
18
                'message' => '',
19
                'data' => $data
20
            ];
21
        }
22
        return [
23
            'success' => false,
24
            'message' => __('get revision data failed'),
25
            'data' => []
26
        ];
27
    }
28
29
    public function restoreAPI(string $tableName, int $originalId, int $revisionId)
30
    {
31
        try {
32
            (new Revision($tableName, $originalId))->add('Auto save before restore');
33
            (new Revision($tableName, $originalId))->restore($revisionId);
34
            return [
35
                'success' => true,
36
                'message' => __('revision restore successfully'),
37
                'data' => []
38
            ];
39
        } catch (Exception $e) {
40
            return [
41
                'success' => false,
42
                'message' => $e->getMessage(),
43
                'data' => []
44
            ];
45
        }
46
    }
47
48
    public function listAPI(string $tableName, int $recordId, int $page = 1, int $perPage = 5)
49
    {
50
        $data = $this->getListData($tableName, $recordId, $page, $perPage);
51
52
        if ($data['total'] === 0) {
53
            return [
54
                'success' => false,
55
                'message' => __('record is empty'),
56
                'data' => []
57
            ];
58
        }
59
60
        return [
61
            'success' => true,
62
            'message' => '',
63
            'data' => [
64
                'dataSource' => $data['dataSource'],
65
                'meta' => [
66
                    'total' => $data['total'],
67
                    'page' => $data['page'],
68
                ]
69
            ]
70
        ];
71
    }
72
73
    private function getListData(string $tableName, int $recordId, int $page, int $perPage)
74
    {
75
        if (empty($tableName) || empty($recordId)) {
76
            throw new \InvalidArgumentException('Table name and record id should not be empty.');
77
        }
78
79
        $list = Db::name('revision')
80
            ->where('table_name', $tableName)
81
            ->where('original_id', $recordId)
82
            ->where('status', 1)
83
            ->order('id', 'desc')
84
            ->paginate([
85
                'list_rows' => $perPage,
86
                'page' => $page
87
            ])->toArray();
88
89
        return [
90
            'dataSource' => $list['data'] ?? $list['dataSource'] ?? [],
91
            'total' => $list['total'] ?? $list['pagination']['total'] ?? 0,
92
            'page' => $list['current_page'] ?? $list['pagination']['page'] ?? 1,
93
        ];
94
    }
95
}
96