Passed
Push — main ( afd658...282368 )
by Aspirant
01:55
created

RevisionAPI::getListData()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 12
nc 2
nop 4
dl 0
loc 17
rs 9.8666
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace aspirantzhang\octopusRevision;
6
7
use think\facade\Db;
8
9
class RevisionAPI
10
{
11
    public function listAPI(string $tableName, int $recordId, int $page = 1, int $perPage = 5)
12
    {
13
        $dataSource = $this->getListData($tableName, $recordId, $page, $perPage);
14
15
        if (empty($dataSource)) {
16
            return [
17
                'success' => false,
18
                'message' => __('record is empty'),
19
                'data' => []
20
            ];
21
        }
22
23
        return [
24
            'success' => true,
25
            'message' => '',
26
            'data' => [
27
                'dataSource' => $dataSource,
28
            ]
29
        ];
30
    }
31
32
    private function getListData(string $tableName, int $recordId, int $page, int $perPage)
33
    {
34
        if (empty($tableName) || empty($recordId)) {
35
            throw new \InvalidArgumentException('Table name and record id should not be empty.');
36
        }
37
38
        $list = Db::name('revision')
39
            ->where('table_name', $tableName)
40
            ->where('original_id', $recordId)
41
            ->where('status', 1)
42
            ->order('id', 'desc')
43
            ->paginate([
44
                'list_rows' => $perPage,
45
                'page' => $page
46
            ])->toArray();
47
48
        return $list['data'] ?? $list['dataSource'] ?? [];
49
    }
50
}
51