GenericConnectionManager::checkName()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
/*
3
 * This file is part of Yolk - Gamer Network's PHP Framework.
4
 *
5
 * Copyright (c) 2014 Gamer Network Ltd.
6
 * 
7
 * Distributed under the MIT License, a copy of which is available in the
8
 * LICENSE file that was bundled with this package, or online at:
9
 * https://github.com/gamernetwork/yolk-database
10
 */
11
12
namespace yolk\database;
13
14
use yolk\contracts\database\DatabaseConnection;
15
use yolk\contracts\database\ConnectionManager;
16
17
use yolk\database\exceptions\DatabaseException;
18
use yolk\database\exceptions\ConfigurationException;
19
20
class GenericConnectionManager implements ConnectionManager {
21
22
	/**
23
	 * Array of database connections
24
	 * @var []
25
	 */
26
	protected $connections = [];
27
28
	/**
29
	 * Name of the default connection.
30
	 * @var string
31
	 */
32
	protected $default = '';
33
34
	public function add( $name, $connection ) {
35
36
		$this->checkName($name);
37
38
		if( $connection instanceof DatabaseConnection )
39
			$this->connections[$name] = $connection;
40
		else
41
			$this->connections[$name] = $this->create($connection);
42
43
		// no default connection so use the first one
44
		if( !$this->default )
45
			$this->default = $name;
46
47
		return $this->connections[$name];
48
49
	}
50
51
	public function remove( $name ) {
52
		unset($this->connections[$name]);
53
		return $this;
54
	}
55
56
	public function get( $name ) {
57
		return isset($this->connections[$name]) ? $this->connections[$name] : null;
58
	}
59
60
	public function has( $name ) {
61
		return isset($this->connections[$name]);
62
	}
63
64
	public function getDefault() {
65
		return $this->connections[$this->default];
66
	}
67
68
	public function setDefault( $name ) {
69
70
		if( !isset($this->connections[$name]) )
71
			throw new \InvalidArgumentException("Unknown Connection: {$name}");
72
73
		$this->default = $name;
74
75
		return $this;
76
77
	}
78
79
	/**
80
	 * Create a suitable implementation of DatabaseConnection based on the specified DSN.
81
	 * @param  mixed $dsn
82
	 * @return DatabaseConnection
83
	 */
84
	protected function create( $dsn ) {
85
86
		$dsn = $this->validateDSN($dsn);
87
88
		$factories = [
89
			DSN::TYPE_MYSQL  => 'createMySQL',
90
			DSN::TYPE_PGSQL  => 'createPgSQL',
91
			DSN::TYPE_SQLITE => 'createSQLite',
92
		];
93
94
		if( !isset($factories[$dsn->type]) )
95
			throw new ConfigurationException('Invalid database type: '. $dsn->type);
96
97
		$factory = $factories[$dsn->type];
98
99
		return $this->$factory($dsn);
100
101
	}
102
103
	/**
104
	 * Ensure we have a valid DSN instance.
105
	 * @param  mixed $dsn a string, array or DSN instance
106
	 * @return DSN
107
	 */
108
	protected function validateDSN( $dsn ) {
109
		if( $dsn instanceof DSN )
110
			return $dsn;
111
		elseif( is_string($dsn) )
112
			return DSN::fromString($dsn);
113
		elseif( is_array($dsn) )
114
			return new DSN($dsn);
115
		else
116
			throw new ConfigurationException('Invalid DSN: '. $dsn);
117
	}
118
119
	/**
120
	 * Create a MySQL connection.
121
	 * @param  DSN    $dsn
122
	 * @return adapters\MySQLConnection
123
	 */
124
	protected function createMySQL( DSN $dsn ) {
125
		return new adapters\MySQLConnection($dsn);
126
	}
127
128
	/**
129
	 * Create a PgSQL connection.
130
	 * @param  DSN    $dsn
131
	 * @return adapters\PgSQLConnection
132
	 */
133
	protected function createPgSQL( DSN $dsn ) {
134
		return new adapters\PgSQLConnection($dsn);
135
	}
136
137
	/**
138
	 * Create a SQLite connection.
139
	 * @param  DSN    $dsn
140
	 * @return adapters\SQLiteConnection
141
	 */
142
	protected function createSQLite( DSN $dsn ) {
143
		return new adapters\SQLiteConnection($dsn);
144
	}
145
146
	/**
147
	 * Ensure we have a valid connection name, i.e. it's not empty and doesn't already exist.
148
	 * @param  string $name
149
	 * @return void
150
	 */
151
	protected function checkName( $name ) {
152
		if( !$name )
153
			throw new DatabaseException('Managed database connections must have a name');
154
		if( $this->has($name) )
155
			throw new DatabaseException('Connection already exists with name: '. $name);
156
	}
157
158
}
159
160
// EOF