Passed
Push — master ( e25937...5ae73b )
by Jean Paul
01:18
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\Row;
6
use Coco\SourceWatcher\Core\SourceWatcherException;
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\DBALException;
9
use Doctrine\DBAL\DriverManager;
10
use Exception;
11
12
/**
13
 * Class MySqlConnector
14
 * @package Coco\SourceWatcher\Core\Database\Connections
15
 *
16
 * Following the definition from: https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#pdo-mysql
17
 */
18
class MySqlConnector extends Connector
19
{
20
    protected string $unixSocket = "";
21
    protected string $charset = "";
22
23
    public function __construct ()
24
    {
25
        $this->driver = "pdo_mysql";
26
27
        $this->port = 3306;
28
    }
29
30
    /**
31
     * @return string
32
     */
33
    public function getUnixSocket () : string
34
    {
35
        return $this->unixSocket;
36
    }
37
38
    /**
39
     * @param string $unixSocket
40
     */
41
    public function setUnixSocket ( string $unixSocket ) : void
42
    {
43
        $this->unixSocket = $unixSocket;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getCharset () : string
50
    {
51
        return $this->charset;
52
    }
53
54
    /**
55
     * @param string $charset
56
     */
57
    public function setCharset ( string $charset ) : void
58
    {
59
        $this->charset = $charset;
60
    }
61
62
    public function connect () : Connection
63
    {
64
        try {
65
            return DriverManager::getConnection( $this->getConnectionParameters() );
66
        } catch ( DBALException $dbalException ) {
67
            throw new SourceWatcherException( "Something went wrong trying to get a connection: " . $dbalException->getMessage() );
68
        } catch ( Exception $exception ) {
69
            throw new SourceWatcherException( "Something unexpected went wrong trying to get a connection: " . $exception->getMessage() );
70
        }
71
    }
72
73
    protected function getConnectionParameters () : array
74
    {
75
        $this->connectionParameters = array();
76
77
        $this->connectionParameters["driver"] = $this->driver;
78
        $this->connectionParameters["user"] = $this->user;
79
        $this->connectionParameters["password"] = $this->password;
80
        $this->connectionParameters["host"] = $this->host;
81
        $this->connectionParameters["port"] = $this->port;
82
        $this->connectionParameters["dbname"] = $this->dbName;
83
84
        if ( isset( $this->unixSocket ) && $this->unixSocket !== "" ) {
85
            $this->connectionParameters["unix_socket"] = $this->unixSocket;
86
        }
87
88
        if ( isset( $this->charset ) && $this->charset !== "" ) {
89
            $this->connectionParameters["charset"] = $this->charset;
90
        }
91
92
        return $this->connectionParameters;
93
    }
94
95
    public function insert ( Row $row ) : void
96
    {
97
        if ( $this->tableName == null || $this->tableName == "" ) {
98
            throw new SourceWatcherException( "No table name found." );
99
        }
100
101
        $connection = $this->connect();
102
103
        $numberOfAffectedRows = $connection->insert( $this->tableName, $row->getAttributes() );
0 ignored issues
show
Unused Code introduced by
The assignment to $numberOfAffectedRows is dead and can be removed.
Loading history...
104
    }
105
}
106