Skip to content

Commit 508554e

Browse files
p123-stackPratiksha
andauthored
Configuration injection factory method (#28)
* Expose-Configuration via Constructor Injection with Optional Factory Method --------- Co-authored-by: Pratiksha <pratiksha@Pratiksha-Nagels>
1 parent 75f84c8 commit 508554e

File tree

3 files changed

+96
-15
lines changed

3 files changed

+96
-15
lines changed

src/Configuration.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,30 @@
22

33
namespace Neo4j\QueryAPI;
44

5+
use InvalidArgumentException;
56
use Neo4j\QueryAPI\Objects\Bookmarks;
67
use Neo4j\QueryAPI\Enums\AccessMode;
78

89
final class Configuration
910
{
11+
/**
12+
* Constructor for Configuration class.
13+
*
14+
* @param string $baseUri The base URI for the Neo4j instance.
15+
* @param string $database The name of the database to connect to.
16+
* @param bool $includeCounters Whether to include counters in the response.
17+
* @param Bookmarks $bookmarks Bookmarks for tracking queries.
18+
* @param AccessMode $accessMode The access mode for the connection (read/write).
19+
*
20+
* @throws InvalidArgumentException if $baseUri is empty.
21+
*/
1022
public function __construct(
1123
public readonly string $baseUri,
1224
public readonly string $database = 'neo4j',
1325
public readonly bool $includeCounters = true,
1426
public readonly Bookmarks $bookmarks = new Bookmarks([]),
1527
public readonly AccessMode $accessMode = AccessMode::WRITE,
1628
) {
29+
1730
}
1831
}

src/Neo4jQueryAPI.php

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Http\Discovery\Psr17FactoryDiscovery;
66
use Http\Discovery\Psr18ClientDiscovery;
77
use http\Exception\RuntimeException;
8+
use InvalidArgumentException;
89
use Neo4j\QueryAPI\Exception\Neo4jException;
910
use Psr\Http\Client\ClientInterface;
1011
use Neo4j\QueryAPI\Authentication\AuthenticateInterface;
@@ -15,36 +16,63 @@
1516

1617
final 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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Tests\Integration;
4+
5+
use InvalidArgumentException;
6+
use PHPUnit\Framework\TestCase;
7+
use Neo4j\QueryAPI\Neo4jQueryAPI;
8+
use Neo4j\QueryAPI\Objects\Authentication;
9+
use Neo4j\QueryAPI\Configuration;
10+
11+
final class Neo4jQueryAPITest extends TestCase
12+
{
13+
public function testLoginWithValidConfiguration(): void
14+
{
15+
$config = new Configuration(baseUri: 'http://valid.address');
16+
17+
$this->expectException(InvalidArgumentException::class);
18+
$this->expectExceptionMessage('Address (http://valid.address) as argument is different from address in configuration (http://myaddress)');
19+
20+
Neo4jQueryAPI::login('http://myaddress', Authentication::fromEnvironment(), $config);
21+
}
22+
23+
public function testLoginWithNullConfiguration(): void
24+
{
25+
$config = null;
26+
27+
$api = Neo4jQueryAPI::login('http://myaddress', Authentication::fromEnvironment(), $config);
28+
29+
$this->assertInstanceOf(Neo4jQueryAPI::class, $api);
30+
$this->assertEquals('http://myaddress', $api->getConfig()->baseUri);
31+
}
32+
33+
public function testConfigOnly(): void
34+
{
35+
$config = new Configuration(baseUri: 'http://valid.address');
36+
37+
$api = Neo4jQueryAPI::login(auth: Authentication::fromEnvironment(), config: $config);
38+
39+
$this->assertInstanceOf(Neo4jQueryAPI::class, $api);
40+
$this->assertEquals('http://valid.address', $api->getConfig()->baseUri);
41+
}
42+
}

0 commit comments

Comments
 (0)