1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AsyncPHP\Icicle\Database; |
4
|
|
|
|
5
|
|
|
use Icicle\Coroutine; |
6
|
|
|
use Icicle\Promise\PromiseInterface; |
7
|
|
|
|
8
|
|
|
final class Manager |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Connector |
12
|
|
|
*/ |
13
|
|
|
private $connector; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Builder |
17
|
|
|
*/ |
18
|
|
|
private $builder; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $operations = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Connector $connector |
27
|
|
|
* @param Builder $builder |
28
|
|
|
*/ |
29
|
1 |
|
public function __construct(Connector $connector, Builder $builder) |
30
|
|
|
{ |
31
|
1 |
|
$this->connector = $connector; |
32
|
1 |
|
$this->builder = $builder; |
33
|
1 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $method |
37
|
|
|
* @param array $parameters |
38
|
|
|
* |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
1 |
|
public function __call($method, array $parameters = []) |
42
|
|
|
{ |
43
|
1 |
|
$operations = $this->operations; |
44
|
1 |
|
$operations[] = [$method, $parameters]; |
45
|
|
|
|
46
|
1 |
|
return $this->cloneWith("operations", $operations); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $key |
51
|
|
|
* @param mixed $value |
52
|
|
|
* |
53
|
|
|
* @return static |
54
|
|
|
*/ |
55
|
1 |
|
public function cloneWith($key, $value) |
56
|
|
|
{ |
57
|
1 |
|
$clone = clone $this; |
58
|
1 |
|
$clone->$key = $value; |
59
|
|
|
|
60
|
1 |
|
return $clone; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $table |
65
|
|
|
* |
66
|
|
|
* @return static |
67
|
|
|
*/ |
68
|
1 |
|
public function table($table) |
69
|
|
|
{ |
70
|
1 |
|
return $this->cloneWith("builder", $this->builder->table($table)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $columns |
75
|
|
|
* |
76
|
|
|
* @return static |
77
|
|
|
*/ |
78
|
1 |
|
public function select($columns = "*") |
79
|
|
|
{ |
80
|
1 |
|
return $this->cloneWith("builder", $this->builder->select($columns)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return PromiseInterface |
85
|
|
|
*/ |
86
|
1 |
|
public function first() |
87
|
|
|
{ |
88
|
|
|
return Coroutine\create(function () { |
89
|
1 |
|
$rows = (yield $this->limit(1)->get()); |
90
|
1 |
|
yield reset($rows); |
91
|
1 |
|
}); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return PromiseInterface |
96
|
|
|
*/ |
97
|
1 |
|
public function get() |
98
|
|
|
{ |
99
|
|
|
return Coroutine\create(function () { |
100
|
1 |
|
$builder = $this->applyOperationsTo($this->builder); |
101
|
|
|
|
102
|
1 |
|
list($statement, $values) = $builder->build(); |
103
|
1 |
|
yield $this->connector->query($statement, $values); |
104
|
1 |
|
}); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param Builder $builder |
109
|
|
|
* |
110
|
|
|
* @return Builder |
111
|
|
|
*/ |
112
|
1 |
|
private function applyOperationsTo($builder) |
113
|
|
|
{ |
114
|
1 |
|
foreach ($this->operations as $operation) { |
115
|
1 |
|
list($method, $parameters) = $operation; |
116
|
1 |
|
$builder = call_user_func_array([$builder, $method], $parameters); |
117
|
1 |
|
} |
118
|
|
|
|
119
|
1 |
|
return $builder; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array $data |
124
|
|
|
* |
125
|
|
|
* @return PromiseInterface |
126
|
|
|
*/ |
127
|
1 |
View Code Duplication |
public function insert(array $data) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
return Coroutine\create(function () use ($data) { |
130
|
1 |
|
$builder = $this->builder->insert($data); |
131
|
1 |
|
$builder = $this->applyOperationsTo($builder); |
132
|
|
|
|
133
|
1 |
|
list($statement, $values) = $builder->build(); |
134
|
1 |
|
yield $this->connector->query($statement, $values); |
135
|
1 |
|
}); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param array $data |
140
|
|
|
* |
141
|
|
|
* @return PromiseInterface |
142
|
|
|
*/ |
143
|
|
View Code Duplication |
public function update(array $data) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
return Coroutine\create(function () use ($data) { |
146
|
|
|
$builder = $this->builder->update($data); |
147
|
|
|
$builder = $this->applyOperationsTo($builder); |
148
|
|
|
|
149
|
|
|
list($statement, $values) = $builder->build(); |
150
|
|
|
yield $this->connector->query($statement, $values); |
151
|
|
|
}); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return PromiseInterface |
156
|
|
|
*/ |
157
|
|
View Code Duplication |
public function delete() |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
return Coroutine\create(function () { |
160
|
|
|
$builder = $this->builder->delete(); |
161
|
|
|
$builder = $this->applyOperationsTo($builder); |
162
|
|
|
|
163
|
|
|
list($statement, $values) = $builder->build(); |
164
|
|
|
yield $this->connector->query($statement, $values); |
165
|
|
|
}); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
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.