Completed
Push — master ( 8f800b...a10bf5 )
by Biao
03:46
created

CoroutineMySQLConnector::createConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 3
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Hhxsv5\LaravelS\Illuminate\Database\Connectors;
4
5
use Exception;
6
use Illuminate\Support\Arr;
7
use Illuminate\Database\Connectors\Connector;
8
use Illuminate\Database\Connectors\ConnectorInterface;
9
use Hhxsv5\LaravelS\Illuminate\Database\CoroutineMySQL;
10
use Hhxsv5\LaravelS\Illuminate\Database\StatementException;
11
12
class CoroutineMySQLConnector extends Connector implements ConnectorInterface
13
{
14
    /**
15
     * @param string $dsn
16
     * @param array $config
17
     * @param array $options
18
     * @return CoroutineMySQL
19
     * @throws Exception
20
     */
21
    public function createConnection($dsn, array $config, array $options)
22
    {
23
        $username = Arr::get($config, 'username');
24
        $password = Arr::get($config, 'password');
25
26
        try {
27
            $mysql = $this->connect($config);
28
        } catch (\Exception $e) {
29
            $mysql = $this->tryAgainIfCausedByLostConnection(
30
                $e, $dsn, $username, $password, $config
31
            );
32
        }
33
34
        return $mysql;
35
    }
36
37
    protected function tryAgainIfCausedByLostConnection(Exception $e, $dsn, $username, $password, $options)
38
    {
39
        if ($this->causedByLostConnection($e)) {
40
            return $this->connect($options);
41
        }
42
        throw $e;
43
    }
44
45
    /**
46
     * @param array $config
47
     * @return CoroutineMySQL
48
     * @throws StatementException
49
     */
50
    public function connect(array $config)
51
    {
52
        $connection = new CoroutineMySQL();
53
        $connection->connect([
54
            'host'        => Arr::get($config, 'host', '127.0.0.1'),
55
            'port'        => Arr::get($config, 'port', 3306),
56
            'user'        => Arr::get($config, 'username', 'hhxsv5'),
57
            'password'    => Arr::get($config, 'password', '52100'),
58
            'database'    => Arr::get($config, 'database', 'test'),
59
            'timeout'     => Arr::get($config, 'timeout', 5),
60
            'charset'     => Arr::get($config, 'charset', 'utf8mb4'),
61
            'strict_type' => Arr::get($config, 'strict', false),
62
        ]);
63
        if (isset($config['timezone'])) {
64
            $connection->prepare('set time_zone="' . $config['timezone'] . '"')->execute();
65
        }
66
        if (isset($config['strict'])) {
67
            if ($config['strict']) {
68
                $connection->prepare("set session sql_mode='STRICT_ALL_TABLES,ANSI_QUOTES'")->execute();
69
            } else {
70
                $connection->prepare("set session sql_mode='ANSI_QUOTES'")->execute();
71
            }
72
        }
73
        return $connection;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $connection returns the type Hhxsv5\LaravelS\Illuminate\Database\CoroutineMySQL which is incompatible with the return type mandated by Illuminate\Database\Conn...torInterface::connect() of PDO.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
74
    }
75
}