55use Http \Discovery \Psr17FactoryDiscovery ;
66use Http \Discovery \Psr18ClientDiscovery ;
77use http \Exception \RuntimeException ;
8+ use InvalidArgumentException ;
89use Neo4j \QueryAPI \Exception \Neo4jException ;
910use Psr \Http \Client \ClientInterface ;
1011use Neo4j \QueryAPI \Authentication \AuthenticateInterface ;
1516
1617final class Neo4jQueryAPI
1718{
19+ private Configuration $ config ;
20+
1821 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
2226 ) {
27+ $ this ->config = $ config ?? new Configuration (baseUri: 'http://myaddress ' ); // Default configuration if not provided
2328 }
2429
2530 /**
2631 * @api
2732 */
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
2934 {
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+
3045 $ client = Psr18ClientDiscovery::find ();
3146
3247 return new self (
3348 client: $ client ,
34- responseParser: new ResponseParser (
35- ogm: new OGM ()
36- ),
49+ responseParser: new ResponseParser (new OGM ()),
3750 requestFactory: new Neo4jRequestFactory (
3851 psr17Factory: Psr17FactoryDiscovery::findRequestFactory (),
3952 streamFactory: Psr17FactoryDiscovery::findStreamFactory (),
40- configuration: new Configuration (
41- baseUri: $ address
42- ),
53+ configuration: $ config ,
4354 auth: $ auth ?? Authentication::fromEnvironment ()
44- )
55+ ),
56+ config: $ config
4557 );
4658 }
4759
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+ */
4876 public function run (string $ cypher , array $ parameters = []): ResultSet
4977 {
5078 $ request = $ this ->requestFactory ->buildRunQueryRequest ($ cypher , $ parameters );
@@ -54,13 +82,13 @@ public function run(string $cypher, array $parameters = []): ResultSet
5482 } catch (RequestExceptionInterface $ e ) {
5583 $ this ->handleRequestException ($ e );
5684 }
85+
5786 return $ this ->responseParser ->parseRunQueryResponse ($ response );
5887 }
5988
6089 public function beginTransaction (): Transaction
6190 {
6291 $ request = $ this ->requestFactory ->buildBeginTransactionRequest ();
63- $ response = $ this ->client ->sendRequest ($ request );
6492
6593 try {
6694 $ response = $ this ->client ->sendRequest ($ request );
@@ -82,8 +110,6 @@ public function beginTransaction(): Transaction
82110 );
83111 }
84112
85-
86-
87113 /**
88114 * Handles request exceptions by parsing error details and throwing a Neo4jException.
89115 *
@@ -96,7 +122,7 @@ private function handleRequestException(RequestExceptionInterface $e): void
96122 $ response = method_exists ($ e , 'getResponse ' ) ? $ e ->getResponse () : null ;
97123
98124 if ($ response instanceof ResponseInterface) {
99- $ errorResponse = json_decode ((string )$ response ->getBody (), true );
125+ $ errorResponse = json_decode ((string ) $ response ->getBody (), true );
100126 throw Neo4jException::fromNeo4jResponse ($ errorResponse , $ e );
101127 }
102128
0 commit comments