Passed
Push — master ( becae5...2d4555 )
by Jean Paul
12:24
created

Connector::getNewConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 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 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 );
0 ignored issues
show
Unused Code introduced by
The call to Monolog\Logger::pushHandler() has too many arguments starting with Monolog\Logger::DEBUG. ( Ignorable by Annotation )

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

31
        $this->logger->/** @scrutinizer ignore-call */ 
32
                       pushHandler( new StreamHandler( $streamPath ), Logger::DEBUG );

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.

Loading history...
32
    }
33
34
    protected string $driver = "";
35
    protected array $connectionParameters = [];
36
    protected string $user = "";
37
    protected string $password = "";
38
    protected string $tableName = "";
39
    public bool $bulkInsert = false;
40
    public Connection $connection;
41
42
    public function getDriver () : string
43
    {
44
        return $this->driver;
45
    }
46
47
    protected abstract function getConnectionParameters () : array;
48
49
    public function getUser () : string
50
    {
51
        return $this->user;
52
    }
53
54
    public function setUser ( string $user ) : void
55
    {
56
        $this->user = $user;
57
    }
58
59
    public function getPassword () : string
60
    {
61
        return $this->password;
62
    }
63
64
    public function setPassword ( string $password ) : void
65
    {
66
        $this->password = $password;
67
    }
68
69
    public function getTableName () : string
70
    {
71
        return $this->tableName;
72
    }
73
74
    public function setTableName ( string $tableName ) : void
75
    {
76
        $this->tableName = $tableName;
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    public function isBulkInsert () : bool
83
    {
84
        return $this->bulkInsert;
85
    }
86
87
    /**
88
     * @param bool $bulkInsert
89
     */
90
    public function setBulkInsert ( bool $bulkInsert ) : void
91
    {
92
        $this->bulkInsert = $bulkInsert;
93
    }
94
95
    /**
96
     * @return Connection
97
     */
98
    public function getConnection () : Connection
99
    {
100
        return $this->connection;
101
    }
102
103
    /**
104
     * @return Connection
105
     * @throws Exception
106
     */
107
    public function getNewConnection () : Connection
108
    {
109
        return DriverManager::getConnection( $this->getConnectionParameters() );
110
    }
111
112
    /**
113
     * @param Connection $connection
114
     */
115
    protected abstract function executeExtraStatements ( Connection $connection ) : void;
116
117
    /**
118
     * @param Row $row
119
     * @return int
120
     * @throws SourceWatcherException
121
     */
122
    public function insert ( Row $row ) : int
123
    {
124
        if ( $this->tableName == null || $this->tableName == "" ) {
125
            throw new SourceWatcherException( Internationalization::getInstance()->getText( Connector::class,
126
                "No_Table_Name_Found" ) );
127
        }
128
129
        try {
130
            $this->connection = $this->bulkInsert ? ( $this->connection ?? $this->getNewConnection() ) : $this->getNewConnection();
131
132
            if ( !$this->connection->isConnected() ) {
133
                $this->connection->connect();
134
            }
135
        } catch ( Exception $exception ) {
136
            $this->logger->debug( $exception->getMessage(), $exception->getTrace() );
137
138
            throw new SourceWatcherException( Internationalization::getInstance()->getText( Connector::class,
139
                "Connection_Object_Not_Connected_Cannot_Insert" ), 0, $exception );
140
        }
141
142
        try {
143
            $this->executeExtraStatements( $this->connection );
144
145
            $numberOfAffectedRows = $this->connection->insert( $this->tableName, $row->getAttributes() );
146
147
            if ( !$this->bulkInsert ) {
148
                $this->connection->close();
149
            }
150
        } catch ( Exception $exception ) {
151
            $this->logger->debug( $exception->getMessage(), $exception->getTrace() );
152
153
            throw new SourceWatcherException( Internationalization::getInstance()->getText( Connector::class,
154
                "Unexpected_Error" ), 0, $exception );
155
        }
156
157
        return $numberOfAffectedRows;
158
    }
159
160
    /**
161
     * @param string $query
162
     * @return array
163
     * @throws SourceWatcherException
164
     */
165
    public function executePlainQuery ( string $query ) : array
166
    {
167
        try {
168
            $connection = $this->getNewConnection();
169
170
            if ( !$connection->isConnected() ) {
171
                $connection->connect();
172
            }
173
174
            $statement = $connection->executeQuery( $query );
175
176
            return $statement->fetchAllAssociative();
177
        } catch ( \Doctrine\DBAL\Driver\Exception $e ) {
178
            throw new SourceWatcherException( "Something went wrong: " . $e->getMessage(), 0, $e );
179
        } catch ( Exception $e ) {
180
            throw new SourceWatcherException( "Something unexpected went wrong: " . $e->getMessage(), 0, $e );
181
        }
182
    }
183
}
184