4
4
5
5
use Http \Discovery \Psr17FactoryDiscovery ;
6
6
use Http \Discovery \Psr18ClientDiscovery ;
7
+ use http \Exception \RuntimeException ;
8
+ use InvalidArgumentException ;
7
9
use Neo4j \QueryAPI \Exception \Neo4jException ;
8
10
use Psr \Http \Client \ClientInterface ;
9
11
use Neo4j \QueryAPI \Authentication \AuthenticateInterface ;
15
17
final class Neo4jQueryAPI
16
18
{
17
19
public function __construct (
18
- private ClientInterface $ client ,
19
- private ResponseParser $ responseParser ,
20
- private Neo4jRequestFactory $ requestFactory
20
+ private ClientInterface $ client ,
21
+ private ResponseParser $ responseParser ,
22
+ private Neo4jRequestFactory $ requestFactory ,
23
+ private ?Configuration $ config
21
24
) {
25
+
22
26
}
23
27
24
28
/**
25
29
* @api
26
30
*/
27
- public static function login (string $ address, AuthenticateInterface $ auth = null ): self
31
+ public static function login (string $ address = null , ? AuthenticateInterface $ auth = null , ? Configuration $ config = null ): self
28
32
{
33
+ $ config = $ config ?? new Configuration (baseUri: $ address ?? '' );
34
+ if (
35
+ trim ($ config ->baseUri ) !== '' &&
36
+ $ address !== null &&
37
+ trim ($ address ) !== '' &&
38
+ $ config ->baseUri !== $ address
39
+ ) {
40
+ throw new InvalidArgumentException (sprintf ('Address (%s) as argument is different from address in configuration (%s) ' , $ config ->baseUri , $ address ));
41
+ }
42
+
29
43
$ client = Psr18ClientDiscovery::find ();
30
44
31
45
return new self (
32
46
client: $ client ,
33
- responseParser: new ResponseParser (
34
- ogm: new OGM ()
35
- ),
47
+ responseParser: new ResponseParser (new OGM ()),
36
48
requestFactory: new Neo4jRequestFactory (
37
49
psr17Factory: Psr17FactoryDiscovery::findRequestFactory (),
38
50
streamFactory: Psr17FactoryDiscovery::findStreamFactory (),
39
- configuration: new Configuration (
40
- baseUri: $ address
41
- ),
51
+ configuration: $ config ,
42
52
auth: $ auth ?? Authentication::fromEnvironment ()
43
- )
53
+ ),
54
+ config: $ config
44
55
);
45
56
}
46
57
58
+ /**
59
+ * @api
60
+ */
61
+ public function create (Configuration $ configuration , AuthenticateInterface $ auth = null ): self
62
+ {
63
+ return self ::login (auth: $ auth , config: $ configuration );
64
+ }
65
+
66
+ public function getConfig (): Configuration
67
+ {
68
+ return $ this ->config ;
69
+ }
70
+
71
+ /**
72
+ * Executes a Cypher query.
73
+ */
47
74
public function run (string $ cypher , array $ parameters = []): ResultSet
48
75
{
49
76
$ request = $ this ->requestFactory ->buildRunQueryRequest ($ cypher , $ parameters );
@@ -53,13 +80,13 @@ public function run(string $cypher, array $parameters = []): ResultSet
53
80
} catch (RequestExceptionInterface $ e ) {
54
81
$ this ->handleRequestException ($ e );
55
82
}
83
+
56
84
return $ this ->responseParser ->parseRunQueryResponse ($ response );
57
85
}
58
86
59
87
public function beginTransaction (): Transaction
60
88
{
61
89
$ request = $ this ->requestFactory ->buildBeginTransactionRequest ();
62
- $ response = $ this ->client ->sendRequest ($ request );
63
90
64
91
try {
65
92
$ response = $ this ->client ->sendRequest ($ request );
@@ -81,8 +108,6 @@ public function beginTransaction(): Transaction
81
108
);
82
109
}
83
110
84
-
85
-
86
111
/**
87
112
* Handles request exceptions by parsing error details and throwing a Neo4jException.
88
113
*
0 commit comments