Passed
Push — master ( 6033fd...d6a87a )
by hugh
11:30 queued 04:10
created

Connection::createConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
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 DateTimeInterface;
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  mixed  $row
91
     * @param  string  $name
92
     * @return null|int
93
     * @throws Exception
94
     * @deprecated 放在Ots实现
95
     */
96
    public function parseAutoIncId($row, string $name = 'id'): ?int
97
    {
98
        return Ots::parseRowAutoId($row, $name);
99
    }
100
101
    /**
102
     * @param  mixed  $row
103
     * @return array
104
     * @deprecated 放在Ots实现
105
     */
106
    public function parseRowColumns($row): array
107
    {
108
        return Ots::parseRow($row);
109
    }
110
111
    /**
112
     * @param  int  $delay
113
     * @return string
114
     * @deprecated 放在Knight依赖实现里面
115
     */
116
    public function availableDate(int $delay = 0): string
117
    {
118
        return Carbon::now()->addRealSeconds($delay)->format(DateTimeInterface::RFC3339_EXTENDED);
119
    }
120
121
    /**
122
     * @param  mixed  $date
123
     * @return Carbon|null
124
     * @deprecated 放在Knight依赖实现里面
125
     */
126
    public function availableDateToDateTime($date): ?Carbon
127
    {
128
        if (empty($date)) {
129
            return null;
130
        }
131
132
        $dateTime = Carbon::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $date);
133
        return $dateTime instanceof Carbon ? $dateTime : null;
134
    }
135
136
    /**
137
     * Dynamically pass methods to the connection.
138
     *
139
     * @param  string  $method
140
     * @param  array  $parameters
141
     * @return mixed
142
     */
143
    public function __call(string $method, array $parameters = [])
144
    {
145
        return call_user_func_array([$this->getOts(), $method], $parameters);
146
    }
147
}
148