Passed
Push — master ( 15d64f...e25937 )
by Jean Paul
01:30
created

MySqlConnector::getUnixSocket()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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