Completed
Push — master ( 21bf70...cc5276 )
by Kamil
05:50
created

Command::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Dazzle\MySQL\Command;
4
5
use Dazzle\Event\BaseEventEmitter;
6
use Dazzle\MySQL\DatabaseInterface;
7
8
abstract class Command extends BaseEventEmitter implements CommandInterface
9
{
10
    /**
11
     * Code of mysql internat thread state.
12
     *
13
     * @var int
14
     */
15
    const SLEEP = 0x00;
16
17
    /**
18
     * Code of mysql_close.
19
     *
20
     * @var int
21
     */
22
    const QUIT = 0x01;
23
24
    /**
25
     * Code of mysql_select_db.
26
     *
27
     * @var int
28
     */
29
    const INIT_DB = 0x02;
30
31
    /**
32
     * Code of mysql_real_query.
33
     *
34
     * @var int
35
     */
36
    const QUERY = 0x03;
37
38
    /**
39
     * Code of mysql_list_fields.
40
     *
41
     * @var int
42
     */
43
    const FIELD_LIST = 0x04;
44
45
    /**
46
     * Code of mysql_create_db (deprecated).
47
     *
48
     * @var int
49
     */
50
    const CREATE_DB = 0x05;
51
52
    /**
53
     * Code of mysql_drop_db (deprecated).
54
     *
55
     * @var int
56
     */
57
    const DROP_DB = 0x06;
58
59
    /**
60
     * Code of mysql_refresh.
61
     *
62
     * @var int
63
     */
64
    const REFRESH = 0x07;
65
66
    /**
67
     * Code of mysql_shutdown.
68
     *
69
     * @var int
70
     */
71
    const SHUTDOWN = 0x08;
72
73
    /**
74
     * Code of mysql_stat.
75
     *
76
     * @var int
77
     */
78
    const STATISTICS = 0x09;
79
80
    /**
81
     * Code of mysql_list_processes.
82
     *
83
     * @var int
84
     */
85
    const PROCESS_INFO = 0x0a;
86
87
    /**
88
     * Code representing internal thread state.
89
     *
90
     * @var int
91
     */
92
    const CONNECT = 0x0b;
93
94
    /**
95
     * Code of mysql_kill.
96
     *
97
     * @var int
98
     */
99
    const PROCESS_KILL = 0x0c;
100
101
    /**
102
     * Code of mysql_dump_debug_info.
103
     *
104
     * @var int
105
     */
106
    const DEBUG = 0x0d;
107
108
    /**
109
     * Code of mysql_ping.
110
     *
111
     * @var int
112
     */
113
    const PING = 0x0e;
114
115
    /**
116
     * Code representing internal thread state.
117
     *
118
     * @var int
119
     */
120
    const TIME = 0x0f;
121
122
    /**
123
     * Code representing internal thread state.
124
     *
125
     * @var int
126
     */
127
    const DELAYED_INSERT = 0x10;
128
129
    /**
130
     * Code of mysql_change_user.
131
     *
132
     * @var int
133
     */
134
    const CHANGE_USER = 0x11;
135
136
    /**
137
     * Code sent by the slave IO thread to request a binlog
138
     *
139
     * @var int
140
     */
141
    const BINLOG_DUMP = 0x12;
142
143
    /**
144
     * Code of "LOAD TABLE ... FROM MASTER" (deprecated).
145
     *
146
     * @var int
147
     */
148
    const TABLE_DUMP = 0x13;
149
150
    /**
151
     * Code representing internal thread state
152
     *
153
     * @var int
154
     */
155
    const CONNECT_OUT = 0x14;
156
157
    /**
158
     * Code sent by the slave to register with the master (optional).
159
     *
160
     * @var int
161
     */
162
    const REGISTER_SLAVE = 0x15;
163
164
    /**
165
     * Code of mysql_stmt_prepare.
166
     *
167
     * @var int
168
     */
169
    const STMT_PREPARE = 0x16;
170
171
    /**
172
     * Code of mysql_stmt_execute
173
     *
174
     * @var int
175
     */
176
    const STMT_EXECUTE = 0x17;
177
178
    /**
179
     * Code of mysql_stmt_send_long_data
180
     *
181
     * @var int
182
     */
183
    const STMT_SEND_LONG_DATA = 0x18;
184
185
    /**
186
     * Code of mysql_stmt_close
187
     *
188
     * @var int
189
     */
190
    const STMT_CLOSE = 0x19;
191
192
    /**
193
     * Code of mysql_stmt_reset
194
     *
195
     * @var int
196
     */
197
    const STMT_RESET = 0x1a;
198
199
    /**
200
     * Code of mysql_set_server_option
201
     *
202
     * @var int
203
     */
204
    const SET_OPTION = 0x1b;
205
206
    /**
207
     * Code of mysql_stmt_fetch
208
     *
209
     * @var int
210
     */
211
    const STMT_FETCH = 0x1c;
212
213
    /**
214
     * Authenticate after the connection is established.
215
     *
216
     * @var int
217
     */
218
    const INIT_AUTHENTICATE = 0xf1;
219
220
    protected $database;
221
222
    private $states = [];
223
224
    private $error;
225
226
    public function __construct(DatabaseInterface $database)
227
    {
228
        $this->database = $database;
229
    }
230
231
    public function getState($name, $default = null)
232
    {
233
        if (isset($this->states[$name]))
234
        {
235
            return $this->states[$name];
236
        }
237
        return $default;
238
    }
239
240
    public function setState($name, $value)
241
    {
242
        $this->states[$name] = $value;
243
244
        return $this;
245
    }
246
247
    public function equals($commandId)
248
    {
249
        return $this->getID() === $commandId;
250
    }
251
252
    public function setError(\Exception $error)
253
    {
254
        $this->error = $error;
255
    }
256
257
    public function getError()
258
    {
259
        return $this->error;
260
    }
261
262
    public function hasError()
263
    {
264
        return (boolean) $this->error;
265
    }
266
267
    public function getConnection()
268
    {
269
        return $this->database;
270
    }
271
}
272