77use Neo4j \QueryAPI \Exception \Neo4jException ;
88use Neo4j \QueryAPI \Objects \Authentication ;
99use Neo4j \QueryAPI \Results \ResultSet ;
10+ use Neo4j \QueryAPI \Configuration ;
1011use Nyholm \Psr7 \Factory \Psr17Factory ;
1112use Psr \Http \Client \ClientInterface ;
1213use Psr \Http \Client \RequestExceptionInterface ;
1314use Psr \Http \Message \ResponseInterface ;
1415
1516class Neo4jQueryAPI
1617{
18+ private Configuration $ config ;
19+
1720 public function __construct (
1821 private ClientInterface $ client ,
1922 private ResponseParser $ responseParser ,
20- private Neo4jRequestFactory $ requestFactory
23+ private Neo4jRequestFactory $ requestFactory ,
24+ ?Configuration $ config = null
2125 ) {
26+ $ this ->config = $ config ?? new Configuration (getenv ("NEO4J_ADDRESS " )); // Default configuration if not provided
2227 }
2328
2429 /**
2530 * @api
2631 */
27- public static function login (string $ address , AuthenticateInterface $ auth = null ): self
32+ public static function login (string $ address , ? AuthenticateInterface $ auth = null , ? Configuration $ config = null ): self
2833 {
2934 $ client = new Client ();
35+ $ config = $ config ?? new Configuration (baseUri: $ address );
3036
3137 return new self (
3238 client: $ client ,
33- responseParser: new ResponseParser (
34- ogm: new OGM ()
35- ),
39+ responseParser: new ResponseParser (new OGM ()),
3640 requestFactory: new Neo4jRequestFactory (
3741 psr17Factory: new Psr17Factory (),
3842 streamFactory: new Psr17Factory (),
39- configuration: new Configuration (
40- baseUri: $ address
41- ),
43+ configuration: $ config ,
4244 auth: $ auth ?? Authentication::fromEnvironment ()
43- )
45+ ),
46+ config: $ config
4447 );
4548 }
4649
50+ public function getConfig (): Configuration
51+ {
52+ return $ this ->config ;
53+ }
54+
4755 /**
4856 * Executes a Cypher query.
4957 */
@@ -59,7 +67,6 @@ public function run(string $cypher, array $parameters = []): ResultSet
5967
6068 return $ this ->responseParser ->parseRunQueryResponse ($ response );
6169 }
62-
6370 /**
6471 * Starts a transaction.
6572 */
@@ -86,6 +93,7 @@ public function beginTransaction(): Transaction
8693 );
8794 }
8895
96+
8997 /**
9098 * Handles request exceptions by parsing error details and throwing a Neo4jException.
9199 *
@@ -95,7 +103,7 @@ private function handleRequestException(RequestExceptionInterface $e): void
95103 {
96104 $ response = $ e ->getResponse ();
97105 if ($ response instanceof ResponseInterface) {
98- $ errorResponse = json_decode ((string )$ response ->getBody (), true );
106+ $ errorResponse = json_decode ((string ) $ response ->getBody (), true );
99107 throw Neo4jException::fromNeo4jResponse ($ errorResponse , $ e );
100108 }
101109
0 commit comments