Completed
Pull Request — master (#1)
by
unknown
08:32
created

Database::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
namespace Dazzle\PgSQL;
3
4
use Dazzle\Event\BaseEventEmitter;
5
use Dazzle\Loop\LoopAwareTrait;
6
use Dazzle\Loop\LoopInterface;
7
use Dazzle\PgSQL\Connection\Connector;
8
use Dazzle\PgSQL\Transaction\Transaction;
9
use Dazzle\Promise\Promise;
10
use Dazzle\Throwable\Exception;
11
use Dazzle\PgSQL\Transaction\TransactionInterface ;
12
use Dazzle\Throwable\Exception\Runtime\ExecutionException;
13
14
class Database extends BaseEventEmitter implements DatabaseInterface
15
{
16
    use LoopAwareTrait;
17
18
    /**
19
     * @var int
20
     */
21
    const STATE_INIT                 = 0;
22
23
    /**
24
     * @var int
25
     */
26
    const STATE_CONNECT_PENDING      = 4;
27
28
    /**
29
     * @var int
30
     */
31
    const STATE_CONNECT_FAILED       = 2;
32
33
    /**
34
     * @var int
35
     */
36
    const STATE_CONNECT_SUCCEEDED    = 6;
37
38
    /**
39
     * @var int
40
     */
41
    const STATE_AUTH_PENDING         = 5;
42
43
    /**
44
     * @var int
45
     */
46
    const STATE_AUTH_FAILED          = 3;
47
48
    /**
49
     * @var int
50
     */
51
    const STATE_AUTH_SUCCEEDED       = 7;
52
53
    /**
54
     * @var int
55
     */
56
    const STATE_DISCONNECT_PENDING   = 8;
57
58
    /**
59
     * @var int
60
     */
61
    const STATE_DISCONNECT_SUCCEEDED = 1;
62
63
    /**
64
     * @var mixed[]
65
     */
66
    protected $config;
67
68
    /**
69
     * @var int
70
     */
71
    protected $state;
72
73
    /**
74
     * @var Connector
75
     */
76
    protected $connector;
77
78
    /**
79
     * @param LoopInterface $loop
80
     * @param mixed[] $config
81
     */
82
    public function __construct(LoopInterface $loop, $config = [])
83
    {
84
        $this->loop = $loop;
85
        $this->config = $this->createConfig($config);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createConfig($config) of type string is incompatible with the declared type array<integer,*> of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
86
        $this->state = self::STATE_INIT;
87
    }
88
89
    /**
90
     * @override
91
     * @inheritDoc
92
     */
93
    public function isPaused()
94
    {
95
        // TODO
96
        return false;
97
    }
98
99
    /**
100
     * @override
101
     * @inheritDoc
102
     */
103
    public function pause()
104
    {
105
        // TODO
106
    }
107
108
    /**
109
     * @override
110
     * @inheritDoc
111
     */
112
    public function resume()
113
    {
114
        // TODO
115
    }
116
117
    /**
118
     * @override
119
     * @inheritDoc
120
     */
121
    public function isStarted()
122
    {
123
        return $this->state >= self::STATE_CONNECT_PENDING;
124
    }
125
126
    /**
127
     * @override
128
     * @inheritDoc
129
     */
130
    public function start($conn = 'default')
131
    {
132
        if ($this->isStarted())
133
        {
134
            return Promise::doResolve($this->connector->getConnection());
135
        }
136
        $this->connector = new Connector($this->config);
137
        $this->state = self::STATE_CONNECT_PENDING;
138
        $promise = $this->connector->getConnection();
139
        if (!($sock = $this->connector->getSock())) {
140
            return $promise->reject(new Exception('No server connection is currently open'));
141
        }
142
        $this->loop->addReadStream($sock, [$this->connector, 'connect']);
143
        $this->loop->addWriteStream($sock, [$this->connector, 'connect']);
144
145
        return $promise;
146
    }
147
148
    /**
149
     * @override
150
     * @inheritDoc
151
     */
152
    public function stop()
153
    {
154
        if (!$this->isStarted())
155
        {
156
            return Promise::doResolve($this);
157
        }
158
        // TODO
159
        return Promise::doReject(new ExecutionException('Not yet implemented.'));
160
    }
161
162
    /**
163
     * @override
164
     * @inheritDoc
165
     */
166
    public function getState()
167
    {
168
        return $this->state;
169
    }
170
171
    /**
172
     * @inheritDoc
173
     */
174
    public function getInfo()
175
    {
176
        // TODO: Implement getInfo() method.
177
    }
178
179
    /**
180
     * @inheritDoc
181
     */
182
    public function setDatabase($dbname)
183
    {
184
        // TODO: Implement setDatabase() method.
185
    }
186
187
    /**
188
     * @inheritDoc
189
     */
190
    public function getDatabase()
191
    {
192
        // TODO: Implement getDatabase() method.
193
    }
194
195
    /**
196
     * @inheritDoc
197
     */
198
    public function beginTransaction()
199
    {
200
        if ($this->isStarted()) {
201
            $trans = new Transaction($this->connector, $this);
202
203
            return Promise::doResolve($trans);
204
        }
205
        //bad method: append 1 callback
206
        return $this->start()->success(function ($_) {
0 ignored issues
show
Unused Code introduced by
The parameter $_ is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
207
            $trans = new Transaction($this->connector, $this);
208
209
            return $trans->begin();
210
        });
211
    }
212
213
    /**
214
     * @inheritDoc
215
     */
216
    public function endTransaction(TransactionInterface $trans)
217
    {
218
        // TODO: Implement endTransaction() method.
219
    }
220
221
    /**
222
     * @inheritDoc
223
     */
224
    public function inTransaction()
225
    {
226
        // TODO: Implement inTransaction() method.
227
    }
228
229
    public function getConnection()
230
    {
231
        return $this->connector->getConnection();
232
    }
233
234
235
    /**
236
     * Create configuration file.
237
     *
238
     * @param mixed[] $config
239
     * @return mixed[]
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
240
     */
241
    protected function createConfig($config = [])
242
    {
243
        $default = [
244
            'host' => 'tcp://127.0.0.1:5432',
245
            'user'     => 'root',
246
            'password'     => '',
247
            'dbname'   => '',
248
        ];
249
        $config = array_merge($default, $config);
250
        foreach ($config as $key => &$value) {
251
            if (!$value) {
252
                unset($config[$key]);
253
                continue;
254
            }
255
            $value = "$key=$value";
256
        }
257
258
        return implode(' ', $config);
259
    }
260
}
261