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