Completed
Push — master ( 7f9e22...63c78c )
by David
46s queued 29s
created

ConnectionFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 65
c 1
b 0
f 0
dl 0
loc 115
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resetDatabase() 0 57 3
A createConnection() 0 51 3
1
<?php
2
3
4
namespace TheCodingMachine\TDBM;
5
6
use Doctrine\Common\EventManager;
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\DriverManager;
9
use Doctrine\DBAL\Event\Listeners\OracleSessionInit;
10
use function strtoupper;
11
12
class ConnectionFactory
13
{
14
    /**
15
     * Resets the database and returns a (root) connection
16
     */
17
    public static function resetDatabase(
18
        string $dbDriver,
19
        ?string $dbHost = null,
20
        ?string $dbPort = null,
21
        ?string $dbUserName = null,
22
        ?string $dbAdminUserName = null,
23
        ?string $dbPassword = null,
24
        ?string $dbName = null
25
    ): Connection {
26
        $config = new \Doctrine\DBAL\Configuration();
27
28
        $dbDriver = $dbDriver;
29
30
        if ($dbDriver === 'pdo_sqlite') {
31
            $dbConnection = self::getConnection();
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on TheCodingMachine\TDBM\ConnectionFactory. ( Ignorable by Annotation )

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

31
            /** @scrutinizer ignore-call */ 
32
            $dbConnection = self::getConnection();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
            $dbConnection->exec('PRAGMA foreign_keys = ON;');
33
        } elseif ($dbDriver === 'oci8') {
34
            $connectionParams = array(
35
                'servicename' => 'XE',
36
                'user' => $dbAdminUserName,
37
                // Because of issues in DBAL, admin and normal user password have to be the same.
38
                'password' => $dbPassword,
39
                'host' => $dbHost,
40
                'port' => $dbPort,
41
                'driver' => $dbDriver,
42
                'dbname' => $dbAdminUserName,
43
                'charset' => 'AL32UTF8',
44
            );
45
46
            $adminConn = DriverManager::getConnection($connectionParams, $config);
47
48
            // When dropAndCreateDatabase is run several times, Oracle can have some issues releasing the TDBM user.
49
            // Let's forcefully delete the connection!
50
            $adminConn->exec("select 'alter system kill session ''' || sid || ',' || serial# || ''';' from v\$session where username = '".strtoupper($dbName)."'");
51
52
            $adminConn->getSchemaManager()->dropAndCreateDatabase($dbName);
53
54
            $dbConnection = self::getConnection();
55
        } else {
56
            $connectionParams = array(
57
                'user' => $dbUserName,
58
                'password' => $dbPassword,
59
                'host' => $dbHost,
60
                'port' => $dbPort,
61
                'driver' => $dbDriver,
62
            );
63
64
            $adminConn = DriverManager::getConnection($connectionParams, $config);
65
66
            $adminConn->getSchemaManager()->dropAndCreateDatabase($dbName);
67
68
            $connectionParams['dbname'] = $dbName;
69
70
            $dbConnection = DriverManager::getConnection($connectionParams, $config);
71
        }
72
73
        return $dbConnection;
74
    }
75
76
    public static function createConnection(
77
        string $dbDriver,
78
        ?string $dbHost = null,
79
        ?string $dbPort = null,
80
        ?string $dbUserName = null,
81
        ?string $dbPassword = null,
82
        ?string $dbName = null
83
    ): Connection {
84
        $config = new \Doctrine\DBAL\Configuration();
85
86
        $dbDriver = $dbDriver;
87
88
        if ($dbDriver === 'pdo_sqlite') {
89
            $connectionParams = array(
90
                'memory' => true,
91
                'driver' => 'pdo_sqlite',
92
            );
93
            $dbConnection = DriverManager::getConnection($connectionParams, $config);
94
        } elseif ($dbDriver === 'oci8') {
95
            $evm = new EventManager();
96
            $evm->addEventSubscriber(new OracleSessionInit(array(
97
                'NLS_TIME_FORMAT' => 'HH24:MI:SS',
98
                'NLS_DATE_FORMAT' => 'YYYY-MM-DD HH24:MI:SS',
99
                'NLS_TIMESTAMP_FORMAT' => 'YYYY-MM-DD HH24:MI:SS',
100
            )));
101
102
            $connectionParams = array(
103
                'servicename' => 'XE',
104
                'user' => $dbUserName,
105
                'password' => $dbPassword,
106
                'host' => $dbHost,
107
                'port' => $dbPort,
108
                'driver' => $dbDriver,
109
                'dbname' => $dbName,
110
                'charset' => 'AL32UTF8',
111
            );
112
            $dbConnection = DriverManager::getConnection($connectionParams, $config, $evm);
113
            $dbConnection->setAutoCommit(true);
114
        } else {
115
            $connectionParams = array(
116
                'user' => $dbUserName,
117
                'password' => $dbPassword,
118
                'host' => $dbHost,
119
                'port' => $dbPort,
120
                'driver' => $dbDriver,
121
                'dbname' => $dbName,
122
            );
123
            $dbConnection = DriverManager::getConnection($connectionParams, $config);
124
        }
125
        
126
        return $dbConnection;
127
    }
128
}
129