Issues (29)

src/Ots.php (1 issue)

Severity
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: hugh.li
5
 * Date: 2021/6/8
6
 * Time: 5:47 下午.
7
 */
8
9
namespace HughCube\Laravel\OTS;
10
11
use Exception;
12
use Illuminate\Support\Facades\DB;
13
14
class Ots
15
{
16
    /**
17
     * @param string $name
18
     *
19
     * @throws
20
     *
21
     * @return Connection
22
     * @phpstan-ignore-next-line
23
     */
24
    public static function connection(string $name = 'ots'): Connection
25
    {
26
        $connection = DB::connection($name);
27
28
        if (!$connection instanceof Connection) {
29
            throw new Exception('Only ots connections can be obtained');
30
        }
31
32
        return $connection;
33
    }
34
35
    /**
36
     * @param mixed $row
37
     *
38
     * @return array
39
     */
40
    public static function parseRow($row): array
41
    {
42
        $row = array_merge(($row['primary_key'] ?? []), ($row['attribute_columns'] ?? []));
43
44
        $columns = [];
45
        foreach ($row as $item) {
46
            if (isset($item[0], $item[1])) {
47
                $columns[$item[0]] = $item[1];
48
            }
49
        }
50
51
        return $columns;
52
    }
53
54
    /**
55
     * @param mixed  $row
56
     * @param string $name
57
     *
58
     * @return int
59
     */
60
    public static function parseRowAutoId($row, string $name = 'id'): ?int
61
    {
62
        foreach (($row['primary_key'] ?? []) as $key) {
63
            if (isset($key[0], $key[1]) && $name === $key[0] && is_int($key[1])) {
64
                return $key[1];
65
            }
66
        }
67
68
        return null;
69
    }
70
71
    /**
72
     * @param mixed  $row
73
     * @param string $name
74
     *
75
     * @throws
76
     *
77
     * @return int
78
     * @phpstan-ignore-next-line
79
     */
80
    public static function mustParseRowAutoId($row, string $name = 'id'): int
0 ignored issues
show
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

80
    public static function mustParseRowAutoId($row, /** @scrutinizer ignore-unused */ string $name = 'id'): int

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
        $id = Ots::parseRowAutoId($row);
83
84
        if (!is_int($id)) {
85
            throw new Exception('Failed to obtain id.');
86
        }
87
88
        return $id;
89
    }
90
91
    /**
92
     * @param mixed $response
93
     *
94
     * @return bool
95
     */
96
    public static function isBatchWriteSuccess($response): bool
97
    {
98
        if (empty($response['tables']) || !is_array($response['tables'])) {
99
            return false;
100
        }
101
102
        foreach ($response['tables'] as $table) {
103
            foreach (($table['rows'] ?? []) as $row) {
104
                if (empty($row['is_ok'])) {
105
                    return false;
106
                }
107
            }
108
        }
109
110
        return true;
111
    }
112
113
    /**
114
     * @param mixed $response
115
     *
116
     * @throws Exception
117
     *
118
     * @return void
119
     */
120
    public static function throwBatchWriteException($response)
121
    {
122
        if (empty($response['tables']) || !is_array($response['tables'])) {
123
            throw new Exception('Abnormal operation.');
124
        }
125
126
        foreach ($response['tables'] as $table) {
127
            foreach ($table['rows'] as $row) {
128
                if (empty($row['is_ok'])) {
129
                    throw new Exception(sprintf('Failed to write the "%s" table.', $table['table_name']));
130
                }
131
            }
132
        }
133
    }
134
}
135