Completed
Push — master ( 2d2cbb...e6311d )
by Biao
03:46
created

tryAgainIfCausedByLostConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 5
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hhxsv5\LaravelS\Illuminate\Database\Connectors;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Database\Connectors\Connector;
7
use Illuminate\Database\Connectors\ConnectorInterface;
8
use Hhxsv5\LaravelS\Illuminate\Database\CoroutineMySQL;
9
10
class CoroutineMySQLConnector extends Connector implements ConnectorInterface
11
{
12
    /**
13
     * @param string $dsn
14
     * @param array $config
15
     * @param array $options
16
     * @return CoroutineMySQL
17
     * @throws \Throwable
18
     */
19
    public function createConnection($dsn, array $config, array $options)
20
    {
21
        $username = Arr::get($config, 'username');
22
        $password = Arr::get($config, 'password');
23
24
        try {
25
            $mysql = $this->connect($config);
26
        } catch (\Exception $e) {
27
            $mysql = $this->_tryAgainIfCausedByLostConnection(
28
                $e, $dsn, $username, $password, $config
29
            );
30
        }
31
32
        return $mysql;
33
    }
34
35
    /**
36
     * @param \Throwable $e
37
     * @param string $dsn
38
     * @param string $username
39
     * @param string $password
40
     * @param array $options
41
     * @return CoroutineMySQL
42
     * @throws \Throwable
43
     */
44
    protected function _tryAgainIfCausedByLostConnection($e, $dsn, $username, $password, $options)
0 ignored issues
show
Unused Code introduced by
The parameter $username 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

44
    protected function _tryAgainIfCausedByLostConnection($e, $dsn, /** @scrutinizer ignore-unused */ $username, $password, $options)

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...
Unused Code introduced by
The parameter $password 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

44
    protected function _tryAgainIfCausedByLostConnection($e, $dsn, $username, /** @scrutinizer ignore-unused */ $password, $options)

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...
Unused Code introduced by
The parameter $dsn 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

44
    protected function _tryAgainIfCausedByLostConnection($e, /** @scrutinizer ignore-unused */ $dsn, $username, $password, $options)

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...
45
    {
46
        if ($this->causedByLostConnection($e)) {
47
            return $this->connect($options);
48
        }
49
        throw $e;
50
    }
51
52
    /**
53
     * @param array $config
54
     * @return CoroutineMySQL
55
     */
56
    public function connect(array $config)
57
    {
58
        $connection = new CoroutineMySQL();
59
        $connection->connect([
60
            'host'        => Arr::get($config, 'host', '127.0.0.1'),
61
            'port'        => Arr::get($config, 'port', 3306),
62
            'user'        => Arr::get($config, 'username', 'hhxsv5'),
63
            'password'    => Arr::get($config, 'password', '52100'),
64
            'database'    => Arr::get($config, 'database', 'test'),
65
            'timeout'     => Arr::get($config, 'timeout', 5),
66
            'charset'     => Arr::get($config, 'charset', 'utf8mb4'),
67
            'strict_type' => Arr::get($config, 'strict', false),
68
        ]);
69
        if (isset($config['timezone'])) {
70
            $connection->prepare('set time_zone="' . $config['timezone'] . '"')->execute();
71
        }
72
        if (isset($config['strict'])) {
73
            if ($config['strict']) {
74
                $connection->prepare("set session sql_mode='STRICT_ALL_TABLES,ANSI_QUOTES'")->execute();
75
            } else {
76
                $connection->prepare("set session sql_mode='ANSI_QUOTES'")->execute();
77
            }
78
        }
79
        return $connection;
80
    }
81
}