Completed
Push — master ( 212c5f...8d6a89 )
by Aimeos
08:59
created

DBAL::createConnection()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 10
nop 2
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package MW
7
 * @subpackage DB
8
 */
9
10
11
namespace Aimeos\MW\DB\Manager;
12
13
14
/**
15
 * Manager for database connections using the DBAL library
16
 *
17
 * @package MW
18
 * @subpackage DB
19
 */
20
class DBAL implements \Aimeos\MW\DB\Manager\Iface
21
{
22
	private $config = null;
23
	private $connections = [];
24
	private $count = [];
25
26
27
	/**
28
	 * Initializes the database manager object
29
	 *
30
	 * @param \Aimeos\MW\Config\Iface $config Object holding the configuration data
31
	 */
32
	public function __construct( \Aimeos\MW\Config\Iface $config )
33
	{
34
		$this->config = $config;
35
	}
36
37
38
	/**
39
	 * Cleans up the object
40
	 */
41
	public function __destruct()
42
	{
43
		foreach( $this->connections as $name => $list )
44
		{
45
			foreach( $list as $conn ) {
46
				unset( $conn );
47
			}
48
		}
49
	}
50
51
52
	/**
53
	 * Clones the objects inside.
54
	 */
55
	public function __clone()
56
	{
57
		$this->config = clone $this->config;
58
59
		foreach( $this->connections as $name => $list ) {
60
			unset( $this->connections[$name] );
61
		}
62
	}
63
64
65
	/**
66
	 * Returns a database connection.
67
	 *
68
	 * @param string $name Name of the resource in configuration
69
	 * @return \Aimeos\MW\DB\Connection\Iface
70
	 */
71
	public function acquire( $name = 'db' )
72
	{
73
		try
74
		{
75
			$adapter = $this->config->get( 'resource/' . $name . '/adapter', 'mysql' );
76
77
			if( !isset( $this->connections[$name] ) || empty( $this->connections[$name] ) )
78
			{
79
				if( !isset( $this->count[$name] ) ) {
80
					$this->count[$name] = 0;
81
				}
82
83
				$limit = $this->config->get( 'resource/' . $name . '/limit', -1 );
84
85
				if( $limit >= 0 && $this->count[$name] >= $limit )
86
				{
87
					$msg = sprintf( 'Maximum number of connections (%1$d) for "%2$s" exceeded', $limit, $name );
88
					throw new \Aimeos\MW\DB\Exception( $msg );
89
				}
90
91
				$this->connections[$name] = array( $this->createConnection( $name, $adapter ) );
92
				$this->count[$name]++;
93
			}
94
95
			return array_pop( $this->connections[$name] );
96
97
		}
98
		catch( \Exception $e ) {
99
			throw new \Aimeos\MW\DB\Exception( $e->getMessage(), $e->getCode() );
100
		}
101
	}
102
103
104
	/**
105
	 * Releases the connection for reuse
106
	 *
107
	 * @param \Aimeos\MW\DB\Connection\Iface $connection Connection object
108
	 * @param string $name Name of resource
109
	 */
110
	public function release( \Aimeos\MW\DB\Connection\Iface $connection, $name = 'db' )
111
	{
112
		if( ( $connection instanceof \Aimeos\MW\DB\Connection\DBAL ) === false ) {
113
			throw new \Aimeos\MW\DB\Exception( 'Connection object isn\'t of type DBAL' );
114
		}
115
116
		$this->connections[$name][] = $connection;
117
	}
118
119
120
	/**
121
	 * Creates a new database connection.
122
	 *
123
	 * @param string $name Name to the database configuration in the resource file
124
	 * @param string $adapter Name of the database adapter, e.g. "mysql"
125
	 * @return \Aimeos\MW\DB\Connection\Iface Database connection
126
	 */
127
	protected function createConnection( $name, $adapter )
128
	{
129
		$params = $this->config->get( 'resource/' . $name );
130
131
		$params['user'] = $this->config->get( 'resource/' . $name . '/username' );
132
		$params['dbname'] = $this->config->get( 'resource/' . $name . '/database' );
133
134
		if( ( $socket = $this->config->get( 'resource/' . $name . '/socket' ) ) != null ) {
135
			$params['unix_socket'] = $socket;
136
		}
137
138
		switch( $adapter )
139
		{
140
			case 'mysql': $params['driver'] = 'pdo_mysql'; break;
141
			case 'oracle': $params['driver'] = 'oci8'; break;
142
			case 'pgsql': $params['driver'] = 'pdo_pgsql'; break;
143
			case 'sqlite': $params['driver'] = 'pdo_sqlite'; break;
144
			default: $params['driver'] = $adapter;
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
145
		}
146
147
		$stmts = $this->config->get( 'resource/' . $name . '/stmt', [] );
148
149
		return new \Aimeos\MW\DB\Connection\DBAL( $params, $stmts );
150
	}
151
}
152