Connector::tryAgainLostConnection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 4
Bugs 1 Features 1
Metric Value
cc 2
eloc 5
c 4
b 1
f 1
nc 2
nop 2
dl 0
loc 9
ccs 0
cts 4
cp 0
crap 6
rs 9.6666
1
<?php
2
namespace JayaCode\Framework\Core\Database\Connector;
3
4
use Exception;
5
use PDO;
6
use Stringy\Stringy;
7
8
/**
9
 * Class Connector
10
 * @package JayaCode\Framework\Core\Database\Connector
11
 */
12
abstract class Connector
13
{
14
    /**
15
     * @var
16
     */
17
    protected $username;
18
19
    /**
20
     * @var
21
     */
22
    protected $password;
23
24
    /**
25
     * @var
26
     */
27
    protected $options;
28
29
    /**
30
     * Creates a PDO instance representing a connection to a database
31
     * @param string $dsn
32
     * @param $config
33
     * @return PDO
34
     */
35
    public function createConnection($dsn, $config)
36
    {
37
        $this->username = arr_get($config, "username");
38
        $this->password = arr_get($config, "password");
39
        $this->options = arr_get($config, "options", array());
40
41
        try {
42
            return new PDO($dsn, $this->username, $this->password, $this->options);
43
        } catch (Exception $exception) {
44
            return $this->tryAgainLostConnection($exception, $dsn);
45
        }
46
    }
47
48
    /**
49
     * @param Exception $exception
50
     * @return bool
51
     */
52
    protected function isErrorLostConnection(Exception $exception)
53
    {
54
        $message = $exception->getMessage();
55
56
        $inStr = array(
57
            'server has gone away',
58
            'no connection to the server',
59
            'Lost connection',
60
            'is dead or not enabled',
61
            'Error while sending',
62
            'decryption failed or bad record mac',
63
            'server closed the connection unexpectedly',
64
            'SSL connection has been closed unexpectedly',
65
            'Deadlock found when trying to get lock',
66
            'Error writing data to the connection',
67
            'Resource deadlock avoided',
68
        );
69
70
        return Stringy::create($message)->containsAny($inStr, false);
71
    }
72
73
    /**
74
     * @param Exception $exception
75
     * @param $dsn
76
     * @return PDO
77
     * @throws Exception
78
     */
79
    protected function tryAgainLostConnection(Exception $exception, $dsn)
80
    {
81
82
        if ($this->isErrorLostConnection($exception)) {
83
            return new PDO($dsn, $this->username, $this->password, $this->options);
84
        } else {
85
            throw $exception;
86
        }
87
    }
88
89
    /**
90
     * @param $config
91
     * @return string
92
     */
93
    abstract protected function getDsn($config);
94
95
    /**
96
     * @param $config
97
     * @return PDO
98
     */
99
    abstract public function connect($config);
100
}
101