13
13
14
14
namespace Laudis \Neo4j \Bolt ;
15
15
16
- use BadMethodCallException ;
16
+ use Bolt \ connection \ AConnection ;
17
17
use Bolt \error \IgnoredException ;
18
18
use Bolt \error \MessageException ;
19
19
use Bolt \protocol \V3 ;
20
20
use Bolt \protocol \V4 ;
21
- use Laudis \Neo4j \BoltFactory ;
22
21
use Laudis \Neo4j \Common \ConnectionConfiguration ;
22
+ use Laudis \Neo4j \Contracts \AuthenticateInterface ;
23
23
use Laudis \Neo4j \Contracts \ConnectionInterface ;
24
24
use Laudis \Neo4j \Contracts \FormatterInterface ;
25
25
use Laudis \Neo4j \Databags \BookmarkHolder ;
26
26
use Laudis \Neo4j \Databags \DatabaseInfo ;
27
- use Laudis \Neo4j \Databags \DriverConfiguration ;
28
27
use Laudis \Neo4j \Enum \AccessMode ;
29
28
use Laudis \Neo4j \Enum \ConnectionProtocol ;
30
29
use Laudis \Neo4j \Types \CypherList ;
31
- use LogicException ;
32
30
use Psr \Http \Message \UriInterface ;
33
31
use RuntimeException ;
34
32
use function str_starts_with ;
41
39
*/
42
40
final class BoltConnection implements ConnectionInterface
43
41
{
44
- private ? V3 $ boltProtocol ;
42
+ private V3 $ boltProtocol ;
45
43
/** @psalm-readonly */
46
44
private ConnectionConfiguration $ config ;
47
- /** @psalm-readonly */
48
- private BoltFactory $ factory ;
45
+ private string $ serverState ;
49
46
50
- private string $ serverState = 'DISCONNECTED ' ;
51
47
/**
52
48
* @note We are using references to "subscribed results" to maintain backwards compatibility and try and strike
53
49
* a balance between performance and ease of use.
@@ -62,32 +58,45 @@ final class BoltConnection implements ConnectionInterface
62
58
* @var list<WeakReference<CypherList>>
63
59
*/
64
60
private array $ subscribedResults = [];
61
+ private AuthenticateInterface $ auth ;
62
+ private AConnection $ connection ;
63
+ private string $ encryptionLevel ;
65
64
66
65
/**
67
66
* @psalm-mutation-free
68
67
*/
69
- public function __construct (BoltFactory $ factory , ? V3 $ boltProtocol , ConnectionConfiguration $ config )
68
+ public function __construct (V3 $ protocol , AConnection $ connection , ConnectionConfiguration $ config, AuthenticateInterface $ auth , string $ encryptionLevel )
70
69
{
71
- $ this ->factory = $ factory ;
72
- $ this ->boltProtocol = $ boltProtocol ;
70
+ $ this ->boltProtocol = $ protocol ;
73
71
$ this ->config = $ config ;
74
- if ($ boltProtocol ) {
75
- $ this ->serverState = 'READY ' ;
76
- }
72
+ $ this ->serverState = 'READY ' ;
73
+ $ this ->auth = $ auth ;
74
+ $ this ->connection = $ connection ;
75
+ $ this ->encryptionLevel = $ encryptionLevel ;
77
76
}
78
77
79
78
/**
80
79
* @psalm-mutation-free
81
80
*/
82
81
public function getImplementation (): V3
83
82
{
84
- if ($ this ->boltProtocol === null ) {
83
+ if (! $ this ->isOpen () ) {
85
84
throw new RuntimeException ('Connection is closed ' );
86
85
}
87
86
88
87
return $ this ->boltProtocol ;
89
88
}
90
89
90
+ /**
91
+ * Encryption level can be either '', 's' or 'ssc', which stand for 'no encryption', 'full encryption' and 'self-signed encryption' respectively.
92
+ *
93
+ * @return string
94
+ */
95
+ public function getEncryptionLevel (): string
96
+ {
97
+ return $ this ->encryptionLevel ;
98
+ }
99
+
91
100
/**
92
101
* @psalm-mutation-free
93
102
*/
@@ -136,6 +145,11 @@ public function getDatabaseInfo(): ?DatabaseInfo
136
145
return $ this ->config ->getDatabaseInfo ();
137
146
}
138
147
148
+ public function getAuthentication (): AuthenticateInterface
149
+ {
150
+ return $ this ->auth ;
151
+ }
152
+
139
153
/**
140
154
* @psalm-mutation-free
141
155
*/
@@ -146,7 +160,7 @@ public function isOpen(): bool
146
160
147
161
public function setTimeout (float $ timeout ): void
148
162
{
149
- $ this ->factory -> getConnection () ->setTimeout ($ timeout );
163
+ $ this ->connection ->setTimeout ($ timeout );
150
164
}
151
165
152
166
public function consumeResults (): void
@@ -170,7 +184,7 @@ public function consumeResults(): void
170
184
public function reset (): void
171
185
{
172
186
try {
173
- $ this ->protocol ()->reset ();
187
+ $ this ->getImplementation ()->reset ();
174
188
} catch (MessageException $ e ) {
175
189
$ this ->serverState = 'DEFUNCT ' ;
176
190
@@ -192,7 +206,7 @@ public function begin(?string $database, ?float $timeout, BookmarkHolder $holder
192
206
193
207
$ extra = $ this ->buildRunExtra ($ database , $ timeout , $ holder );
194
208
try {
195
- $ this ->protocol ()->begin ($ extra );
209
+ $ this ->getImplementation ()->begin ($ extra );
196
210
} catch (IgnoredException $ e ) {
197
211
$ this ->serverState = 'INTERRUPTED ' ;
198
212
@@ -206,11 +220,6 @@ public function begin(?string $database, ?float $timeout, BookmarkHolder $holder
206
220
$ this ->serverState = 'TX_READY ' ;
207
221
}
208
222
209
- public function getFactory (): BoltFactory
210
- {
211
- return $ this ->factory ;
212
- }
213
-
214
223
/**
215
224
* Discards a result.
216
225
*
@@ -220,7 +229,7 @@ public function discard(?int $qid): void
220
229
{
221
230
try {
222
231
$ extra = $ this ->buildResultExtra (null , $ qid );
223
- $ bolt = $ this ->protocol ();
232
+ $ bolt = $ this ->getImplementation ();
224
233
225
234
if ($ bolt instanceof V4 ) {
226
235
$ result = $ bolt ->discard ($ extra );
@@ -256,7 +265,7 @@ public function run(string $text, array $parameters, ?string $database, ?float $
256
265
try {
257
266
$ extra = $ this ->buildRunExtra ($ database , $ timeout , $ holder );
258
267
259
- $ tbr = $ this ->protocol ()->run ($ text , $ parameters , $ extra );
268
+ $ tbr = $ this ->getImplementation ()->run ($ text , $ parameters , $ extra );
260
269
261
270
if (str_starts_with ($ this ->serverState , 'TX_ ' )) {
262
271
$ this ->serverState = 'TX_STREAMING ' ;
@@ -287,7 +296,7 @@ public function commit(): void
287
296
$ this ->consumeResults ();
288
297
289
298
try {
290
- $ this ->protocol ()->commit ();
299
+ $ this ->getImplementation ()->commit ();
291
300
} catch (MessageException $ e ) {
292
301
$ this ->serverState = 'FAILED ' ;
293
302
@@ -311,7 +320,7 @@ public function rollback(): void
311
320
$ this ->consumeResults ();
312
321
313
322
try {
314
- $ this ->protocol ()->rollback ();
323
+ $ this ->getImplementation ()->rollback ();
315
324
} catch (MessageException $ e ) {
316
325
$ this ->serverState = 'FAILED ' ;
317
326
@@ -336,7 +345,7 @@ public function pull(?int $qid, ?int $fetchSize): array
336
345
{
337
346
$ extra = $ this ->buildResultExtra ($ fetchSize , $ qid );
338
347
339
- $ bolt = $ this ->protocol ();
348
+ $ bolt = $ this ->getImplementation ();
340
349
try {
341
350
if (!$ bolt instanceof V4 ) {
342
351
/** @var non-empty-list<list> $tbr */
@@ -360,23 +369,15 @@ public function pull(?int $qid, ?int $fetchSize): array
360
369
return $ tbr ;
361
370
}
362
371
363
- /**
364
- * @psalm-mutation-free
365
- */
366
- public function getDriverConfiguration (): DriverConfiguration
367
- {
368
- return $ this ->config ->getDriverConfiguration ();
369
- }
370
-
371
372
public function __destruct ()
372
373
{
373
374
if ($ this ->serverState !== 'FAILED ' && $ this ->isOpen ()) {
374
375
$ this ->consumeResults ();
375
376
376
- $ this ->protocol ()->goodbye ();
377
+ $ this ->getImplementation ()->goodbye ();
377
378
378
379
$ this ->serverState = 'DEFUNCT ' ;
379
- $ this ->boltProtocol = null ; // has to be set to null as the sockets don't recover nicely contrary to what the underlying code might lead you to believe;
380
+ unset( $ this ->boltProtocol ) ; // has to be set to null as the sockets don't recover nicely contrary to what the underlying code might lead you to believe;
380
381
}
381
382
}
382
383
@@ -421,15 +422,6 @@ public function subscribeResult(CypherList $result): void
421
422
$ this ->subscribedResults [] = WeakReference::create ($ result );
422
423
}
423
424
424
- private function protocol (): V3
425
- {
426
- if ($ this ->boltProtocol === null ) {
427
- throw new LogicException ('Cannot use protocol if it is not created ' );
428
- }
429
-
430
- return $ this ->boltProtocol ;
431
- }
432
-
433
425
private function interpretResult (array $ result ): void
434
426
{
435
427
if (str_starts_with ($ this ->serverState , 'TX_ ' )) {
0 commit comments