Passed
Push — master ( 72d254...356a09 )
by Jean Paul
01:39
created

MySqlConnector::insert()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 22
rs 9.5555
1
<?php
2
3
namespace Coco\SourceWatcher\Core\Database\Connections;
4
5
use Coco\SourceWatcher\Core\SourceWatcherException;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\DriverManager;
9
10
/**
11
 * Class MySqlConnector
12
 * @package Coco\SourceWatcher\Core\Database\Connections
13
 *
14
 * Following the definition from: https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#pdo-mysql
15
 */
16
class MySqlConnector extends ClientServerDatabaseConnector
17
{
18
    /**
19
     * @var string
20
     */
21
    protected string $unixSocket = "";
22
23
    /**
24
     * @var string
25
     */
26
    protected string $charset = "";
27
28
    /**
29
     * MySqlConnector constructor.
30
     */
31
    public function __construct ()
32
    {
33
        $this->driver = "pdo_mysql";
34
35
        $this->port = 3306;
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function getUnixSocket () : string
42
    {
43
        return $this->unixSocket;
44
    }
45
46
    /**
47
     * @param string $unixSocket
48
     */
49
    public function setUnixSocket ( string $unixSocket ) : void
50
    {
51
        $this->unixSocket = $unixSocket;
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getCharset () : string
58
    {
59
        return $this->charset;
60
    }
61
62
    /**
63
     * @param string $charset
64
     */
65
    public function setCharset ( string $charset ) : void
66
    {
67
        $this->charset = $charset;
68
    }
69
70
    /**
71
     * @return Connection
72
     * @throws SourceWatcherException
73
     */
74
    public function connect () : Connection
75
    {
76
        try {
77
            return DriverManager::getConnection( $this->getConnectionParameters() );
78
        } catch ( DBALException $dbalException ) {
79
            throw new SourceWatcherException( "Something went wrong trying to get a connection: " . $dbalException->getMessage() );
80
        }
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    protected function getConnectionParameters () : array
87
    {
88
        $this->connectionParameters = array();
89
90
        $this->connectionParameters["driver"] = $this->driver;
91
        $this->connectionParameters["user"] = $this->user;
92
        $this->connectionParameters["password"] = $this->password;
93
        $this->connectionParameters["host"] = $this->host;
94
        $this->connectionParameters["port"] = $this->port;
95
        $this->connectionParameters["dbname"] = $this->dbName;
96
97
        if ( isset( $this->unixSocket ) && $this->unixSocket !== "" ) {
98
            $this->connectionParameters["unix_socket"] = $this->unixSocket;
99
        }
100
101
        if ( isset( $this->charset ) && $this->charset !== "" ) {
102
            $this->connectionParameters["charset"] = $this->charset;
103
        }
104
105
        return $this->connectionParameters;
106
    }
107
}
108