Passed
Push — master ( 6021d7...3aed48 )
by Jean Paul
06:27
created

Connector::connect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
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
11
/**
12
 * Class Connector
13
 * @package Coco\SourceWatcher\Core\Database\Connections
14
 */
15
abstract class Connector
16
{
17
    /**
18
     * @var string
19
     */
20
    protected string $driver = "";
21
22
    /**
23
     * @var array
24
     */
25
    protected array $connectionParameters = [];
26
27
    /**
28
     * @var string
29
     */
30
    protected string $user = "";
31
32
    /**
33
     * @var string
34
     */
35
    protected string $password = "";
36
37
    /**
38
     * @var string
39
     */
40
    protected string $tableName = "";
41
42
    /**
43
     * @return string
44
     */
45
    public function getDriver () : string
46
    {
47
        return $this->driver;
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    protected abstract function getConnectionParameters () : array;
54
55
    /**
56
     * @return string
57
     */
58
    public function getUser () : string
59
    {
60
        return $this->user;
61
    }
62
63
    /**
64
     * @param string $user
65
     */
66
    public function setUser ( string $user ) : void
67
    {
68
        $this->user = $user;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getPassword () : string
75
    {
76
        return $this->password;
77
    }
78
79
    /**
80
     * @param string $password
81
     */
82
    public function setPassword ( string $password ) : void
83
    {
84
        $this->password = $password;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getTableName () : string
91
    {
92
        return $this->tableName;
93
    }
94
95
    /**
96
     * @param string $tableName
97
     */
98
    public function setTableName ( string $tableName ) : void
99
    {
100
        $this->tableName = $tableName;
101
    }
102
103
    /**
104
     * @return Connection
105
     * @throws SourceWatcherException
106
     */
107
    public function connect () : Connection
108
    {
109
        try {
110
            return DriverManager::getConnection( $this->getConnectionParameters() );
111
        } catch ( DBALException $dbalException ) {
112
            throw new SourceWatcherException( "Something went wrong trying to get a connection: ", 0, $dbalException->getMessage() );
0 ignored issues
show
Bug introduced by
$dbalException->getMessage() of type string is incompatible with the type Throwable|null expected by parameter $previous of Coco\SourceWatcher\Core\...xception::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
            throw new SourceWatcherException( "Something went wrong trying to get a connection: ", 0, /** @scrutinizer ignore-type */ $dbalException->getMessage() );
Loading history...
113
        }
114
    }
115
116
    /**
117
     * @param Row $row
118
     * @return int
119
     */
120
    public abstract function insert ( Row $row ) : int;
121
}
122