1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AsyncPHP\Icicle\Database\Connector; |
4
|
|
|
|
5
|
|
|
use AsyncPHP\Icicle\Database\Connector; |
6
|
|
|
use Aura\Sql\ExtendedPdo; |
7
|
|
|
use Icicle\Promise; |
8
|
|
|
use Icicle\Promise\PromiseInterface; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use PDO; |
11
|
|
|
|
12
|
|
|
final class BlockingConnector implements Connector |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ExtendedPdo |
16
|
|
|
*/ |
17
|
|
|
private $connection; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @inheritdoc |
21
|
|
|
* |
22
|
|
|
* @param array $config |
23
|
|
|
* |
24
|
|
|
* @return PromiseInterface |
25
|
|
|
* |
26
|
|
|
* @throws InvalidArgumentException |
27
|
|
|
*/ |
28
|
1 |
|
public function connect(array $config) |
29
|
|
|
{ |
30
|
1 |
|
$config = $this->validate($config); |
31
|
|
|
|
32
|
1 |
|
$this->connection = new ExtendedPdo( |
33
|
1 |
|
new PDO($this->newConnectionString($config), $config["username"], $config["password"]) |
34
|
1 |
|
); |
35
|
1 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns a new dsn string, for PDO to connect to the database with. |
39
|
|
|
* |
40
|
|
|
* @param array $config |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
1 |
|
private function newConnectionString(array $config) |
45
|
|
|
{ |
46
|
1 |
View Code Duplication |
if ($config["driver"] === "mysql") { |
|
|
|
|
47
|
1 |
|
return sprintf("mysql:host=%s;port=%s;dbname=%s;unix_socket=%s;charset=%s", $config["host"], $config["port"], $config["schema"], $config["socket"], $config["charset"]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
if ($config["driver"] === "pgsql") { |
|
|
|
|
51
|
|
|
return sprintf("pgsql:host=%s;port=%s;dbname=%s", $config["host"], $config["port"], $config["schema"]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($config["driver"] === "sqlite") { |
55
|
|
|
return sprintf("sqlite:%s", $config["file"]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
if ($config["driver"] === "sqlsrv") { |
|
|
|
|
59
|
|
|
return sprintf("sqlsrv:Server=%s,%s;Database=%s", $config["host"], $config["port"], $config["schema"]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $config |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
* |
68
|
|
|
* @throws InvalidArgumentException |
69
|
|
|
*/ |
70
|
1 |
|
private function validate(array $config) |
71
|
|
|
{ |
72
|
|
|
$config += [ |
73
|
1 |
|
"host" => "127.0.0.1", |
74
|
1 |
|
"port" => 3306, |
75
|
1 |
|
"charset" => "utf8", |
76
|
1 |
|
"socket" => null, |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
// TODO: validate connection details |
80
|
|
|
|
81
|
1 |
|
return $config; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritdoc |
86
|
|
|
* |
87
|
|
|
* @param string $query |
88
|
|
|
* @param array $values |
89
|
|
|
* |
90
|
|
|
* @return PromiseInterface |
91
|
|
|
* |
92
|
|
|
* @throws InvalidArgumentException |
93
|
|
|
*/ |
94
|
1 |
|
public function query($query, $values) |
95
|
|
|
{ |
96
|
1 |
|
return Promise\resolve( |
97
|
1 |
|
$this->connection->fetchAll($query, $values) |
98
|
1 |
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.