14
14
namespace Laudis \Neo4j ;
15
15
16
16
use Bolt \Bolt ;
17
- use Bolt \connection \AConnection ;
18
- use Bolt \connection \Socket ;
19
- use Bolt \connection \StreamSocket ;
20
- use Bolt \error \ConnectException ;
21
- use Bolt \error \MessageException ;
17
+ use Bolt \connection \IConnection ;
22
18
use Bolt \protocol \V3 ;
23
19
use function explode ;
24
- use function extension_loaded ;
20
+ use Laudis \ Neo4j \ Bolt \ AConnectionFactory ;
25
21
use Laudis \Neo4j \Bolt \BoltConnection ;
26
- use Laudis \Neo4j \Bolt \SslConfigurator ;
22
+ use Laudis \Neo4j \Bolt \ProtocolFactory ;
27
23
use Laudis \Neo4j \Common \ConnectionConfiguration ;
28
24
use Laudis \Neo4j \Contracts \AuthenticateInterface ;
29
25
use Laudis \Neo4j \Contracts \ConnectionFactoryInterface ;
30
26
use Laudis \Neo4j \Contracts \ConnectionInterface ;
31
27
use Laudis \Neo4j \Databags \DatabaseInfo ;
32
28
use Laudis \Neo4j \Databags \SessionConfiguration ;
33
29
use Laudis \Neo4j \Databags \SslConfiguration ;
34
- use Laudis \Neo4j \Databags \TransactionConfiguration ;
35
30
use Laudis \Neo4j \Enum \ConnectionProtocol ;
36
- use Laudis \Neo4j \Exception \Neo4jException ;
37
31
use Psr \Http \Message \UriInterface ;
38
- use RuntimeException ;
39
32
40
33
/**
41
34
* Small wrapper around the bolt library to easily guarantee only bolt version 3 and up will be created and authenticated.
42
35
*
43
- * @implements ConnectionFactoryInterface<array{0: V3, 1: AConnection }>
36
+ * @implements ConnectionFactoryInterface<array{0: V3, 1: IConnection }>
44
37
*/
45
38
final class BoltFactory implements ConnectionFactoryInterface
46
39
{
47
40
private UriInterface $ uri ;
48
- private SslConfigurator $ sslConfigurator ;
41
+ private AConnectionFactory $ connectionFactory ;
42
+ private ProtocolFactory $ protocolFactory ;
49
43
50
44
/**
51
45
* @psalm-external-mutation-free
52
46
*/
53
- public function __construct (
54
- UriInterface $ uri ,
55
- SslConfigurator $ sslConfigurator
56
- ) {
47
+ public function __construct (UriInterface $ uri , AConnectionFactory $ connectionFactory , ProtocolFactory $ protocolFactory )
48
+ {
57
49
$ this ->uri = $ uri ;
58
- $ this ->sslConfigurator = $ sslConfigurator ;
50
+ $ this ->connectionFactory = $ connectionFactory ;
51
+ $ this ->protocolFactory = $ protocolFactory ;
59
52
}
60
53
61
- public function createConnection (string $ userAgent , SslConfiguration $ sslConfig , SessionConfiguration $ sessionConfig , AuthenticateInterface $ auth ): ConnectionInterface
62
- {
63
- [$ encryptionLevel , $ sslConfig ] = $ this ->sslConfigurator ->configure ($ this ->uri , $ sslConfig );
64
- $ port = $ this ->uri ->getPort () ?? 7687 ;
65
- if (extension_loaded ('sockets ' ) && $ sslConfig === null ) {
66
- $ connection = new Socket ($ this ->uri ->getHost (), $ port , TransactionConfiguration::DEFAULT_TIMEOUT );
67
- } else {
68
- $ connection = new StreamSocket ($ this ->uri ->getHost (), $ port , TransactionConfiguration::DEFAULT_TIMEOUT );
69
- if ($ sslConfig !== null ) {
70
- $ connection ->setSslContextOptions ($ sslConfig );
71
- }
72
- }
73
-
74
- $ bolt = new Bolt ($ connection );
75
-
76
- try {
77
- $ bolt ->setProtocolVersions (4.4 , 4.3 , 4.2 , 3 );
78
- try {
79
- $ protocol = $ bolt ->build ();
80
- } catch (ConnectException $ exception ) {
81
- $ bolt ->setProtocolVersions (4.1 , 4.0 , 4 , 3 );
82
- $ protocol = $ bolt ->build ();
83
- }
84
-
85
- if (!$ protocol instanceof V3 ) {
86
- throw new RuntimeException ('Client only supports bolt version 3 and up. ' );
87
- }
88
-
89
- $ response = $ auth ->authenticateBolt ($ protocol , $ userAgent );
90
- } catch (MessageException $ e ) {
91
- throw Neo4jException::fromMessageException ($ e );
92
- }
54
+ public function createConnection (
55
+ string $ userAgent ,
56
+ SslConfiguration $ sslConfig ,
57
+ SessionConfiguration $ sessionConfig ,
58
+ AuthenticateInterface $ auth
59
+ ): ConnectionInterface {
60
+ [$ connection , $ encryptionLevel ] = $ this ->connectionFactory ->create ($ sslConfig );
61
+ [$ protocol , $ authResponse ] = $ this ->protocolFactory ->createProtocol ($ connection , $ userAgent );
93
62
94
63
$ sessionConfig = new ConnectionConfiguration (
95
- $ response ['server ' ],
64
+ $ authResponse ['server ' ],
96
65
$ this ->uri ,
97
- explode ('/ ' , $ response ['server ' ])[1 ] ?? '' ,
66
+ explode ('/ ' , $ authResponse ['server ' ])[1 ] ?? '' ,
98
67
ConnectionProtocol::determineBoltVersion ($ protocol ),
99
68
$ sessionConfig ->getAccessMode (),
100
69
$ sessionConfig ->getDatabase () === null ? null : new DatabaseInfo ($ sessionConfig ->getDatabase ())
101
70
);
102
71
103
- return new BoltConnection ($ protocol , $ connection , $ sessionConfig , $ auth , $ encryptionLevel );
72
+ return new BoltConnection ($ protocol , $ connection , $ sessionConfig , $ auth , $ encryptionLevel, $ userAgent );
104
73
}
105
74
106
- public function canReuseConnection (ConnectionInterface $ connection , string $ userAgent , SslConfiguration $ sslConfig , AuthenticateInterface $ auth ): bool
107
- {
75
+ public function canReuseConnection (
76
+ ConnectionInterface $ connection ,
77
+ string $ userAgent ,
78
+ SslConfiguration $ sslConfig ,
79
+ AuthenticateInterface $ auth
80
+ ): bool {
108
81
return $ connection ->getAuthentication ()->toString ($ this ->uri ) === $ auth ->toString ($ this ->uri ) &&
109
- $ connection ->getEncryptionLevel () === $ this ->sslConfigurator -> configure ( $ this -> uri , $ sslConfig )[ 0 ] &&
110
- $ connection ->getUserAgent () === $ userAgent &&
111
- $ connection ->getServerAddress ()->getHost () === $ this ->uri ->getHost () &&
112
- $ connection ->getServerAddress ()->getPort () === $ this ->uri ->getPort ();
82
+ $ this -> connectionFactory -> sameEncryptionLevel ( $ connection ->getEncryptionLevel (), $ this ->uri , $ sslConfig ) &&
83
+ $ connection ->getUserAgent () === $ userAgent &&
84
+ $ connection ->getServerAddress ()->getHost () === $ this ->uri ->getHost () &&
85
+ $ connection ->getServerAddress ()->getPort () === $ this ->uri ->getPort ();
113
86
}
114
87
115
88
public function reuseConnection (ConnectionInterface $ connection , SessionConfiguration $ config ): ConnectionInterface
@@ -125,6 +98,13 @@ public function reuseConnection(ConnectionInterface $connection, SessionConfigur
125
98
126
99
[$ protocol , $ connectionImpl ] = $ connection ->getImplementation ();
127
100
128
- return new BoltConnection ($ protocol , $ connectionImpl , $ config , $ connection ->getAuthentication (), $ connection ->getEncryptionLevel ());
101
+ return new BoltConnection (
102
+ $ protocol ,
103
+ $ connectionImpl ,
104
+ $ config ,
105
+ $ connection ->getAuthentication (),
106
+ $ connection ->getEncryptionLevel (),
107
+ $ connection ->getUserAgent ()
108
+ );
129
109
}
130
110
}
0 commit comments