|
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 Coco\SourceWatcher\Utils\FileUtils; |
|
8
|
|
|
use Coco\SourceWatcher\Utils\Internationalization; |
|
9
|
|
|
use Doctrine\DBAL\Connection; |
|
10
|
|
|
use Doctrine\DBAL\DriverManager; |
|
11
|
|
|
use Exception; |
|
12
|
|
|
use Monolog\Handler\StreamHandler; |
|
13
|
|
|
use Monolog\Logger; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class Connector |
|
17
|
|
|
* |
|
18
|
|
|
* @package Coco\SourceWatcher\Core\Database\Connections |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class Connector |
|
21
|
|
|
{ |
|
22
|
|
|
private Logger $logger; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct () |
|
25
|
|
|
{ |
|
26
|
|
|
$this->logger = new Logger( "Connector" ); |
|
27
|
|
|
|
|
28
|
|
|
$streamPath = FileUtils::file_build_path( __DIR__, "..", "..", "..", "..", "logs", |
|
29
|
|
|
"Connector" . "-" . gmdate( "Y-m-d-H-i-s", time() ) . "-" . getmypid() . ".txt" ); |
|
30
|
|
|
|
|
31
|
|
|
$this->logger->pushHandler( new StreamHandler( $streamPath ), Logger::DEBUG ); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected string $driver = ""; |
|
35
|
|
|
|
|
36
|
|
|
protected array $connectionParameters = []; |
|
37
|
|
|
|
|
38
|
|
|
protected string $user = ""; |
|
39
|
|
|
|
|
40
|
|
|
protected string $password = ""; |
|
41
|
|
|
|
|
42
|
|
|
protected string $tableName = ""; |
|
43
|
|
|
|
|
44
|
|
|
public function getDriver () : string |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->driver; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected abstract function getConnectionParameters () : array; |
|
50
|
|
|
|
|
51
|
|
|
public function getUser () : string |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->user; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function setUser ( string $user ) : void |
|
57
|
|
|
{ |
|
58
|
|
|
$this->user = $user; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getPassword () : string |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->password; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function setPassword ( string $password ) : void |
|
67
|
|
|
{ |
|
68
|
|
|
$this->password = $password; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getTableName () : string |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->tableName; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function setTableName ( string $tableName ) : void |
|
77
|
|
|
{ |
|
78
|
|
|
$this->tableName = $tableName; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return Connection |
|
83
|
|
|
* @throws Exception |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getConnection () : Connection |
|
86
|
|
|
{ |
|
87
|
|
|
return DriverManager::getConnection( $this->getConnectionParameters() ); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param Row $row |
|
92
|
|
|
* @return int |
|
93
|
|
|
* @throws SourceWatcherException |
|
94
|
|
|
*/ |
|
95
|
|
|
public function insert ( Row $row ) : int |
|
96
|
|
|
{ |
|
97
|
|
|
if ( $this->tableName == null || $this->tableName == "" ) { |
|
98
|
|
|
throw new SourceWatcherException( Internationalization::getInstance()->getText( Connector::class, |
|
99
|
|
|
"No_Table_Name_Found" ) ); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
try { |
|
103
|
|
|
$connection = $this->getConnection(); |
|
104
|
|
|
|
|
105
|
|
|
if ( !$connection->isConnected() ) { |
|
106
|
|
|
$connection->connect(); |
|
107
|
|
|
} |
|
108
|
|
|
} catch ( Exception $exception ) { |
|
109
|
|
|
throw new SourceWatcherException( Internationalization::getInstance()->getText( Connector::class, |
|
110
|
|
|
"Connection_Object_Not_Connected_Cannot_Insert" ), 0, $exception ); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
try { |
|
114
|
|
|
$numberOfAffectedRows = $connection->insert( $this->tableName, $row->getAttributes() ); |
|
115
|
|
|
|
|
116
|
|
|
$connection->close(); |
|
117
|
|
|
} catch ( Exception $exception ) { |
|
118
|
|
|
$this->logger->debug( $exception->getMessage() ); |
|
119
|
|
|
|
|
120
|
|
|
throw new SourceWatcherException( Internationalization::getInstance()->getText( Connector::class, |
|
121
|
|
|
"Unexpected_Error" ), 0, $exception ); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $numberOfAffectedRows; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param string $query |
|
129
|
|
|
* @return array |
|
130
|
|
|
* @throws SourceWatcherException |
|
131
|
|
|
*/ |
|
132
|
|
|
public function executePlainQuery ( string $query ) : array |
|
133
|
|
|
{ |
|
134
|
|
|
try { |
|
135
|
|
|
$connection = $this->getConnection(); |
|
136
|
|
|
|
|
137
|
|
|
if ( !$connection->isConnected() ) { |
|
138
|
|
|
$connection->connect(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$statement = $connection->executeQuery( $query ); |
|
142
|
|
|
|
|
143
|
|
|
return $statement->fetchAllAssociative(); |
|
144
|
|
|
} catch ( \Doctrine\DBAL\Driver\Exception $e ) { |
|
145
|
|
|
throw new SourceWatcherException( "Something went wrong: " . $e->getMessage(), 0, $e ); |
|
146
|
|
|
} catch ( Exception $e ) { |
|
147
|
|
|
throw new SourceWatcherException( "Something unexpected went wrong: " . $e->getMessage(), 0, $e ); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.