Skip to content

Commit bc53a5c

Browse files
PratikshaPratiksha
authored andcommitted
Expose-Configuration via Constructor Injection with Optional Factory Method
1 parent 889765b commit bc53a5c

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/Neo4jQueryAPI.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,51 @@
77
use Neo4j\QueryAPI\Exception\Neo4jException;
88
use Neo4j\QueryAPI\Objects\Authentication;
99
use Neo4j\QueryAPI\Results\ResultSet;
10+
use Neo4j\QueryAPI\Configuration;
1011
use Nyholm\Psr7\Factory\Psr17Factory;
1112
use Psr\Http\Client\ClientInterface;
1213
use Psr\Http\Client\RequestExceptionInterface;
1314
use Psr\Http\Message\ResponseInterface;
1415

1516
class 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

Comments
 (0)