ConnectionFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 55
c 2
b 0
f 0
dl 0
loc 101
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resetDatabase() 0 43 3
A createConnection() 0 51 3
1
<?php
2
3
namespace TheCodingMachine\TDBM;
4
5
use Doctrine\Common\EventManager;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\DriverManager;
8
use Doctrine\DBAL\Event\Listeners\OracleSessionInit;
9
10
class ConnectionFactory
11
{
12
    /**
13
     * Resets the database and returns a (root) connection
14
     */
15
    public static function resetDatabase(
16
        string $dbDriver,
17
        ?string $dbHost = null,
18
        ?string $dbPort = null,
19
        ?string $dbUserName = null,
20
        ?string $dbAdminUserName = null,
21
        ?string $dbPassword = null,
22
        ?string $dbName = null
23
    ): Connection {
24
        $config = new \Doctrine\DBAL\Configuration();
25
26
        if ($dbDriver === 'pdo_sqlite') {
27
            $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

27
            /** @scrutinizer ignore-call */ 
28
            $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...
28
            $dbConnection->exec('PRAGMA foreign_keys = ON;');
29
        } elseif ($dbDriver === 'oci8') {
30
            $adminConn = self::createConnection($dbDriver, $dbHost, $dbPort, $dbAdminUserName, $dbPassword, $dbAdminUserName);
31
32
            // When dropAndCreateDatabase is run several times, Oracle can have some issues releasing the TDBM user.
33
            // Let's forcefully delete the connection!
34
            //$adminConn->exec("select 'alter system kill session ''' || sid || ',' || serial# || ''';' from v\$session where username = '".strtoupper($dbName)."'");
35
36
            $adminConn->getSchemaManager()->dropAndCreateDatabase($dbName);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::getSchemaManager() has been deprecated: Use {@see createSchemaManager()} instead. ( Ignorable by Annotation )

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

36
            /** @scrutinizer ignore-deprecated */ $adminConn->getSchemaManager()->dropAndCreateDatabase($dbName);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Abs...dropAndCreateDatabase() has been deprecated: Use {@see dropDatabase()} and {@see createDatabase()} instead. ( Ignorable by Annotation )

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

36
            /** @scrutinizer ignore-deprecated */ $adminConn->getSchemaManager()->dropAndCreateDatabase($dbName);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
37
38
            $dbConnection = self::createConnection($dbDriver, $dbHost, $dbPort, $dbName, $dbPassword, $dbName);
39
        } else {
40
            $connectionParams = array(
41
                'user' => $dbUserName,
42
                'password' => $dbPassword,
43
                'host' => $dbHost,
44
                'port' => $dbPort,
45
                'driver' => $dbDriver,
46
            );
47
48
            $adminConn = DriverManager::getConnection($connectionParams, $config);
49
50
            $adminConn->getSchemaManager()->dropAndCreateDatabase($dbName);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::getSchemaManager() has been deprecated: Use {@see createSchemaManager()} instead. ( Ignorable by Annotation )

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

50
            /** @scrutinizer ignore-deprecated */ $adminConn->getSchemaManager()->dropAndCreateDatabase($dbName);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Abs...dropAndCreateDatabase() has been deprecated: Use {@see dropDatabase()} and {@see createDatabase()} instead. ( Ignorable by Annotation )

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

50
            /** @scrutinizer ignore-deprecated */ $adminConn->getSchemaManager()->dropAndCreateDatabase($dbName);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
51
52
            $connectionParams['dbname'] = $dbName;
53
54
            $dbConnection = DriverManager::getConnection($connectionParams, $config);
55
        }
56
57
        return $dbConnection;
58
    }
59
60
    public static function createConnection(
61
        string $dbDriver,
62
        ?string $dbHost = null,
63
        ?string $dbPort = null,
64
        ?string $dbUserName = null,
65
        ?string $dbPassword = null,
66
        ?string $dbName = null
67
    ): Connection {
68
        $config = new \Doctrine\DBAL\Configuration();
69
70
        $dbDriver = $dbDriver;
71
72
        if ($dbDriver === 'pdo_sqlite') {
73
            $connectionParams = array(
74
                'memory' => true,
75
                'driver' => 'pdo_sqlite',
76
            );
77
            $dbConnection = DriverManager::getConnection($connectionParams, $config);
78
        } elseif ($dbDriver === 'oci8') {
79
            $evm = new EventManager();
80
            $evm->addEventSubscriber(new OracleSessionInit(array(
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\DBAL\Event\Listeners\OracleSessionInit has been deprecated: Use {@see \Doctrine\DBAL\Driver\OCI8\Middleware\InitializeSession} instead. ( Ignorable by Annotation )

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

80
            $evm->addEventSubscriber(/** @scrutinizer ignore-deprecated */ new OracleSessionInit(array(
Loading history...
81
                'NLS_TIME_FORMAT' => 'HH24:MI:SS',
82
                'NLS_DATE_FORMAT' => 'YYYY-MM-DD HH24:MI:SS',
83
                'NLS_TIMESTAMP_FORMAT' => 'YYYY-MM-DD HH24:MI:SS',
84
            )));
85
86
            $connectionParams = array(
87
                'servicename' => 'XE',
88
                'user' => $dbUserName,
89
                'password' => $dbPassword,
90
                'host' => $dbHost,
91
                'port' => $dbPort,
92
                'driver' => $dbDriver,
93
                'dbname' => $dbName,
94
                'charset' => 'AL32UTF8',
95
            );
96
            $dbConnection = DriverManager::getConnection($connectionParams, $config, $evm);
97
            $dbConnection->setAutoCommit(true);
98
        } else {
99
            $connectionParams = array(
100
                'user' => $dbUserName,
101
                'password' => $dbPassword,
102
                'host' => $dbHost,
103
                'port' => $dbPort,
104
                'driver' => $dbDriver,
105
                'dbname' => $dbName,
106
            );
107
            $dbConnection = DriverManager::getConnection($connectionParams, $config);
108
        }
109
110
        return $dbConnection;
111
    }
112
}
113