Passed
Push β€” master ( 420e39...e64d28 )
by hugh
12:20
created

Connection::availableDateToDateTime()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 8
rs 10
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 Aliyun\OTS\OTSClient;
12
use DateTime;
13
use Exception;
14
use Illuminate\Database\Connection as IlluminateConnection;
15
use Illuminate\Support\Arr;
16
use Illuminate\Support\Carbon;
17
18
/**
19
 * @mixin OTSClient
20
 */
21
class Connection extends IlluminateConnection
22
{
23
    /**
24
     * @var OTSClient
25
     */
26
    private $ots;
27
28
    /**
29
     * Create a new database connection instance.
30
     *
31
     * @param  array  $config
32
     */
33
    public function __construct(array $config)
34
    {
35
        $this->config = $config;
36
    }
37
38
    /**
39
     * @return OtsClient
40
     */
41
    public function getOts(): OTSClient
42
    {
43
        if (!$this->ots instanceof OTSClient) {
0 ignored issues
show
introduced by
$this->ots is always a sub-type of Aliyun\OTS\OTSClient.
Loading history...
44
            $this->ots = $this->createConnection($this->config);
45
        }
46
47
        return $this->ots;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getDatabaseName()
54
    {
55
        return $this->getOts()->getClientConfig()->getInstanceName();
56
    }
57
58
    /**
59
     * Create a new OTSClient connection.
60
     *
61
     * @param  array  $config
62
     *
63
     * @return OTSClient
64
     */
65
    protected function createConnection(array $config): OTSClient
66
    {
67
        $config['ErrorLogHandler'] = Arr::get($config, 'ErrorLogHandler', false);
68
        $config['DebugLogHandler'] = Arr::get($config, 'DebugLogHandler', false);
69
70
        return new OTSClient($config);
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    public function disconnect()
77
    {
78
        unset($this->ots);
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public function getDriverName(): string
85
    {
86
        return 'ots';
87
    }
88
89
    /**
90
     * @param $response
91
     * @param  string  $name
92
     * @return int
93
     * @throws Exception
94
     */
95
    public function parseAutoIncId($response, string $name = 'id'): int
96
    {
97
        $id = null;
98
        foreach (($response['primary_key'] ?? []) as $key) {
99
            if ($name === Arr::get($key, 0) && !empty($key[1]) && is_int($key[1])) {
100
                $id = $key[1];
101
            }
102
        }
103
104
        if (empty($id) || !is_int($id)) {
105
            throw new Exception('Failed to obtain the ID!');
106
        }
107
108
        return $id;
109
    }
110
111
    /**
112
     * @param $row
113
     * @return array
114
     */
115
    public function parseRowColumns($row): array
116
    {
117
        $row = array_merge(
118
            Arr::get($row, 'primary_key', []),
119
            Arr::get($row, 'attribute_columns', [])
120
        );
121
122
        $columns = [];
123
        foreach ($row as $item) {
124
            if (isset($item[0], $item[1])) {
125
                $columns[$item[0]] = $item[1];
126
            }
127
        }
128
        return $columns;
129
    }
130
131
    /**
132
     * @param  int  $delay
133
     * @return string
134
     */
135
    public function availableDate(int $delay = 0): string
136
    {
137
        return Carbon::now()->addRealSeconds($delay)->rawFormat(DateTime::RFC3339_EXTENDED);
138
    }
139
140
    /**
141
     * @param  mixed  $date
142
     * @return Carbon|null
143
     */
144
    public function availableDateToDateTime($date): ?Carbon
145
    {
146
        if (empty($date)) {
147
            return null;
148
        }
149
150
        $dateTime = Carbon::createFromFormat(DateTime::RFC3339_EXTENDED, $date);
151
        return $dateTime instanceof Carbon ? $dateTime : null;
152
    }
153
154
    /**
155
     * Dynamically pass methods to the connection.
156
     *
157
     * @param  string  $method
158
     * @param  array  $parameters
159
     * @return mixed
160
     */
161
    public function __call(string $method, array $parameters = [])
162
    {
163
        return call_user_func_array([$this->getOts(), $method], $parameters);
164
    }
165
}
166