@@ -22,8 +22,7 @@ final class Cluster
22
22
private const
23
23
STATE_NOT_CONNECTED = 0 ,
24
24
STATE_CONNECTING = 1 ,
25
- STATE_CONNECTED = 2 ,
26
- STATE_DISCONNECTING = 3
25
+ STATE_CONNECTED = 2
27
26
;
28
27
29
28
/**
@@ -42,9 +41,9 @@ final class Cluster
42
41
private $ state = self ::STATE_NOT_CONNECTED ;
43
42
44
43
/**
45
- * @var Connection
44
+ * @var Events
46
45
*/
47
- private $ connection ;
46
+ private $ events ;
48
47
49
48
/**
50
49
* @param Config $config
@@ -71,17 +70,32 @@ public static function build(string $dsn): self
71
70
public function options (): Promise
72
71
{
73
72
return call (function () {
74
- if ($ this ->connection === null ) {
75
- $ this ->connection = yield $ this ->open ();
76
- }
73
+ /** @var Connection $connection */
74
+ $ connection = yield $ this ->open ();
77
75
78
76
/** @var Response\Supported $response */
79
- $ response = yield $ this ->connection ->send (new Request \Options );
77
+ $ response = yield $ connection ->send (new Request \Options );
78
+
79
+ $ connection ->close ();
80
80
81
81
return $ response ->options ;
82
82
});
83
83
}
84
84
85
+ /**
86
+ * @return Promise<Events>
87
+ */
88
+ public function events (): Promise
89
+ {
90
+ return call (function () {
91
+ if ($ this ->events ) {
92
+ return $ this ->events ;
93
+ }
94
+
95
+ return $ this ->events = new Events (yield $ this ->startup ());
96
+ });
97
+ }
98
+
85
99
/**
86
100
* @param string $keyspace
87
101
*
@@ -96,17 +110,7 @@ public function connect(string $keyspace = null): Promise
96
110
97
111
$ this ->state = self ::STATE_CONNECTING ;
98
112
99
- if (null === $ this ->connection ) {
100
- $ this ->connection = yield $ this ->open ();
101
- }
102
-
103
- $ frame = yield $ this ->connection ->send (new Request \Startup ($ this ->config ->options ()));
104
-
105
- if ($ frame instanceof Response \Authenticate) {
106
- yield $ this ->authenticate ();
107
- }
108
-
109
- $ session = new Session ($ this ->connection );
113
+ $ session = new Session (yield $ this ->startup ());
110
114
111
115
if ($ keyspace !== null ) {
112
116
yield $ session ->keyspace ($ keyspace );
@@ -119,38 +123,27 @@ public function connect(string $keyspace = null): Promise
119
123
}
120
124
121
125
/**
122
- * @param int $code
123
- * @param string $reason
124
- *
125
- * @return Promise<void>
126
+ * @return Promise<Connection>
126
127
*/
127
- public function disconnect ( int $ code = 0 , string $ reason = '' ): Promise
128
+ private function startup ()
128
129
{
129
- return call (function () use ($ code , $ reason ) {
130
- if ($ this ->state === self ::STATE_DISCONNECTING ) {
131
- return ;
132
- }
130
+ return call (function () {
131
+ /** @var Connection $connection */
132
+ $ connection = yield $ this ->open ();
133
133
134
- $ this ->state = self ::STATE_DISCONNECTING ;
134
+ $ request = new Request \Startup ($ this ->config ->options ());
135
+ $ response = yield $ connection ->send ($ request );
135
136
136
- if ($ this -> connection !== null ) {
137
- $ this ->connection -> close ( );
137
+ if ($ response instanceof Response \Authenticate ) {
138
+ yield $ this ->authenticate ( $ connection );
138
139
}
139
140
140
- $ this -> state = self :: STATE_NOT_CONNECTED ;
141
+ return $ connection ;
141
142
});
142
143
}
143
144
144
145
/**
145
- * @return bool
146
- */
147
- public function isConnected (): bool
148
- {
149
- return $ this ->state === self ::STATE_CONNECTED ;
150
- }
151
-
152
- /**
153
- * @return Promise
146
+ * @return Promise<Connection>
154
147
*/
155
148
private function open (): Promise
156
149
{
@@ -178,32 +171,25 @@ private function open(): Promise
178
171
}
179
172
180
173
/**
181
- * @return Promise
174
+ * @param Connection $connection
175
+ *
176
+ * @return Promise<Response\AuthSuccess>
182
177
*/
183
- private function authenticate (): Promise
178
+ private function authenticate (Connection $ connection ): Promise
184
179
{
185
- return call (function () {
186
- $ request = new Request \AuthResponse (
187
- $ this ->config ->user (),
188
- $ this ->config ->password ()
189
- );
190
-
191
- /** @var Frame $frame */
192
- $ frame = yield $ this ->connection ->send ($ request );
180
+ return call (function () use ($ connection ) {
181
+ $ request = new Request \AuthResponse ($ this ->config ->user (), $ this ->config ->password ());
182
+ $ response = yield $ connection ->send ($ request );
193
183
194
184
switch (true ) {
195
- case $ frame instanceof Response \AuthChallenge:
196
- // TODO
197
-
198
- break ;
199
- case $ frame instanceof Response \AuthSuccess:
200
- break ;
185
+ case $ response instanceof Response \AuthSuccess:
186
+ return $ response ;
201
187
default :
202
- throw Exception \ServerException::unexpectedFrame ($ frame ->opcode );
188
+ throw Exception \ServerException::unexpectedFrame ($ response ->opcode );
203
189
}
204
190
});
205
191
}
206
-
192
+
207
193
/**
208
194
* @return Compressor
209
195
*/
0 commit comments