Skip to content

Commit cbd1d42

Browse files
committed
winp
1 parent 1038abf commit cbd1d42

9 files changed

+146
-644
lines changed

src/AuthenticateInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Psr\Http\Message\RequestInterface;
66

7-
interface AuthenticateInterface extends RequestInterface
7+
interface AuthenticateInterface
88
{
99
/**
1010
* Authenticates the request by returning a new instance of the request with the authentication information attached.

src/BasicAuthentication.php

Lines changed: 5 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
namespace Neo4j\QueryAPI;
44

5-
use Psr\Http\Message\MessageInterface;
65
use Psr\Http\Message\RequestInterface;
76
use Psr\Http\Message\ResponseInterface;
8-
use Psr\Http\Message\StreamInterface;
9-
use Psr\Http\Message\UriInterface;
107

118
class BasicAuthentication implements AuthenticateInterface
129
{
@@ -19,89 +16,14 @@ public function authenticate(RequestInterface $request): RequestInterface
1916
$authHeader = 'Basic ' . base64_encode($this->username . ':' . $this->password);
2017
return $request->withHeader('Authorization', $authHeader);
2118
}
22-
23-
public function getProtocolVersion(): string
24-
{
25-
// TODO: Implement getProtocolVersion() method.
26-
}
27-
28-
public function withProtocolVersion(string $version): MessageInterface
29-
{
30-
// TODO: Implement withProtocolVersion() method.
31-
}
32-
33-
public function getHeaders(): array
34-
{
35-
// TODO: Implement getHeaders() method.
36-
}
37-
38-
public function hasHeader(string $name): bool
19+
public function getHeader(): string
3920
{
40-
// TODO: Implement hasHeader() method.
21+
return 'Basic ' . base64_encode($this->username . ':' . $this->password);
4122
}
4223

43-
public function getHeader(string $name): array
24+
public function getType(): string
4425
{
45-
// TODO: Implement getHeader() method.
26+
return 'Basic';
4627
}
4728

48-
public function getHeaderLine(string $name): string
49-
{
50-
// TODO: Implement getHeaderLine() method.
51-
}
52-
53-
public function withHeader(string $name, $value): MessageInterface
54-
{
55-
// TODO: Implement withHeader() method.
56-
}
57-
58-
public function withAddedHeader(string $name, $value): MessageInterface
59-
{
60-
// TODO: Implement withAddedHeader() method.
61-
}
62-
63-
public function withoutHeader(string $name): MessageInterface
64-
{
65-
// TODO: Implement withoutHeader() method.
66-
}
67-
68-
public function getBody(): StreamInterface
69-
{
70-
// TODO: Implement getBody() method.
71-
}
72-
73-
public function withBody(StreamInterface $body): MessageInterface
74-
{
75-
// TODO: Implement withBody() method.
76-
}
77-
78-
public function getRequestTarget(): string
79-
{
80-
// TODO: Implement getRequestTarget() method.
81-
}
82-
83-
public function withRequestTarget(string $requestTarget): RequestInterface
84-
{
85-
// TODO: Implement withRequestTarget() method.
86-
}
87-
88-
public function getMethod(): string
89-
{
90-
// TODO: Implement getMethod() method.
91-
}
92-
93-
public function withMethod(string $method): RequestInterface
94-
{
95-
// TODO: Implement withMethod() method.
96-
}
97-
98-
public function getUri(): UriInterface
99-
{
100-
// TODO: Implement getUri() method.
101-
}
102-
103-
public function withUri(UriInterface $uri, bool $preserveHost = false): RequestInterface
104-
{
105-
// TODO: Implement withUri() method.
106-
}
107-
}
29+
}

src/BearerAuthentication.php

Lines changed: 5 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
namespace Neo4j\QueryAPI;
44

5-
use Psr\Http\Message\MessageInterface;
65
use Psr\Http\Message\RequestInterface;
7-
use Psr\Http\Message\StreamInterface;
8-
use Psr\Http\Message\UriInterface;
96

107
class BearerAuthentication implements AuthenticateInterface
118
{
@@ -18,89 +15,13 @@ public function authenticate(RequestInterface $request): RequestInterface
1815
$authHeader = 'Bearer ' . $this->token;
1916
return $request->withHeader('Authorization', $authHeader);
2017
}
21-
22-
public function getProtocolVersion(): string
23-
{
24-
// TODO: Implement getProtocolVersion() method.
25-
}
26-
27-
public function withProtocolVersion(string $version): MessageInterface
28-
{
29-
// TODO: Implement withProtocolVersion() method.
30-
}
31-
32-
public function getHeaders(): array
33-
{
34-
// TODO: Implement getHeaders() method.
35-
}
36-
37-
public function hasHeader(string $name): bool
38-
{
39-
// TODO: Implement hasHeader() method.
40-
}
41-
42-
public function getHeader(string $name): array
43-
{
44-
// TODO: Implement getHeader() method.
45-
}
46-
47-
public function getHeaderLine(string $name): string
48-
{
49-
// TODO: Implement getHeaderLine() method.
50-
}
51-
52-
public function withHeader(string $name, $value): MessageInterface
53-
{
54-
// TODO: Implement withHeader() method.
55-
}
56-
57-
public function withAddedHeader(string $name, $value): MessageInterface
58-
{
59-
// TODO: Implement withAddedHeader() method.
60-
}
61-
62-
public function withoutHeader(string $name): MessageInterface
63-
{
64-
// TODO: Implement withoutHeader() method.
65-
}
66-
67-
public function getBody(): StreamInterface
68-
{
69-
// TODO: Implement getBody() method.
70-
}
71-
72-
public function withBody(StreamInterface $body): MessageInterface
73-
{
74-
// TODO: Implement withBody() method.
75-
}
76-
77-
public function getRequestTarget(): string
78-
{
79-
// TODO: Implement getRequestTarget() method.
80-
}
81-
82-
public function withRequestTarget(string $requestTarget): RequestInterface
83-
{
84-
// TODO: Implement withRequestTarget() method.
85-
}
86-
87-
public function getMethod(): string
88-
{
89-
// TODO: Implement getMethod() method.
90-
}
91-
92-
public function withMethod(string $method): RequestInterface
93-
{
94-
// TODO: Implement withMethod() method.
95-
}
96-
97-
public function getUri(): UriInterface
18+
public function getHeader(): string
9819
{
99-
// TODO: Implement getUri() method.
20+
return 'Bearer ' . $this->token;
10021
}
10122

102-
public function withUri(UriInterface $uri, bool $preserveHost = false): RequestInterface
23+
public function getType(): string
10324
{
104-
// TODO: Implement withUri() method.
25+
return 'Bearer';
10526
}
106-
}
27+
}

src/Neo4jQueryAPI.php

Lines changed: 51 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -55,59 +55,56 @@ public static function login(string $address, AuthenticateInterface $auth = null
5555
*/
5656
public function run(string $cypher, array $parameters = [], string $database = 'neo4j', Bookmarks $bookmark = null): ResultSet
5757
{
58-
$payload = [
59-
'statement' => $cypher,
60-
'parameters' => empty($parameters) ? new stdClass() : $parameters,
61-
'includeCounters' => true,
62-
];
63-
64-
if ($bookmark !== null) {
65-
$payload['bookmarks'] = $bookmark->getBookmarks();
66-
}
67-
68-
$request = new Request('POST', '/db/' . $database . '/query/v2');
69-
$request = $this->auth->authenticate($request);
70-
$request = $request->withHeader('Content-Type', 'application/json');
71-
$request = $request->withBody(Utils::streamFor(json_encode($payload)));
72-
73-
// Send the request to Neo4j
74-
$response = $this->client->sendRequest($request);
75-
76-
// Get the response content
77-
$contents = $response->getBody()->getContents();
78-
$data = json_decode($contents, true, flags: JSON_THROW_ON_ERROR);
79-
80-
// Check for Neo4j errors in the response
81-
if (isset($data['errors']) && count($data['errors']) > 0) {
82-
$error = $data['errors'][0];
83-
$errorCode = $error['code'] ?? '';
84-
$errorMessage = $error['message'] ?? '';
85-
86-
// Handle specific error types
87-
if ($errorCode === 'Neo.ClientError.Schema.EquivalentSchemaRuleAlreadyExists') {
88-
// Provide error details as an array, not a string
89-
$errorDetails = [
90-
'code' => $errorCode,
91-
'message' => $errorMessage,
92-
];
93-
throw new Neo4jException($errorDetails); // Pass error details as an array
58+
try {
59+
// Prepare the payload
60+
$payload = [
61+
'statement' => $cypher,
62+
'parameters' => empty($parameters) ? new stdClass() : $parameters,
63+
'includeCounters' => true,
64+
];
65+
66+
// Include bookmarks if provided
67+
if ($bookmark !== null) {
68+
$payload['bookmarks'] = $bookmark->getBookmarks();
9469
}
9570

96-
// You can handle other Neo4j-specific errors similarly
97-
if ($errorCode) {
98-
$errorDetails = [
99-
'code' => $errorCode,
100-
'message' => $errorMessage,
101-
];
102-
throw new Neo4jException($errorDetails); // Pass error details as an array
103-
}
104-
}
71+
// Create the HTTP request
72+
$request = new Request('POST', '/db/' . $database . '/query/v2');
73+
$request = $this->auth->authenticate($request);
74+
$request = $request->withHeader('Content-Type', 'application/json');
75+
$request = $request->withBody(Utils::streamFor(json_encode($payload)));
10576

106-
// If no error, return the result set
107-
return $this->parseResultSet($data);
108-
}
77+
// Send the request and get the response
78+
$response = $this->client->sendRequest($request);
79+
$contents = $response->getBody()->getContents();
80+
81+
// Parse the response data
82+
$data = json_decode($contents, true, flags: JSON_THROW_ON_ERROR);
10983

84+
// Check for errors in the response from Neo4j
85+
if (isset($data['errors']) && count($data['errors']) > 0) {
86+
// If errors exist in the response, throw a Neo4jException
87+
$error = $data['errors'][0];
88+
throw new Neo4jException(
89+
$error, // Pass the entire error array instead of just the message
90+
0,
91+
null,
92+
$error
93+
94+
);
95+
}
11096

97+
// Parse the result set and return it
98+
return $this->parseResultSet($data);
99+
100+
} catch (RequestExceptionInterface $e) {
101+
// Handle exceptions from the HTTP request
102+
$this->handleException($e);
103+
} catch (Neo4jException $e) {
104+
// Catch Neo4j specific exceptions (if thrown)
105+
throw $e; // Re-throw the exception
106+
}
107+
}
111108

112109
private function parseResultSet(array $data): ResultSet
113110
{
@@ -169,46 +166,15 @@ private function handleException(RequestExceptionInterface $e): void
169166

170167
public function beginTransaction(string $database = 'neo4j'): Transaction
171168
{
172-
// Create the request to begin a transaction
173-
$request = new Request('POST', '/db/' . $database . '/query/v2/tx');
169+
$response = $this->client->sendRequest(new Request('POST', '/db/neo4j/query/v2/tx'));
174170

175-
// Authenticate the request by adding necessary headers (e.g., Authorization)
176-
$request = $this->auth->authenticate($request);
171+
$clusterAffinity = $response->getHeaderLine('neo4j-cluster-affinity');
172+
$responseData = json_decode($response->getBody(), true);
173+
$transactionId = $responseData['transaction']['id'];
177174

178-
try {
179-
// Send the request
180-
$response = $this->client->sendRequest($request);
181-
182-
// Check for a successful response (status code 200 or 202)
183-
if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 202) {
184-
throw new \RuntimeException('Failed to begin transaction: ' . $response->getReasonPhrase());
185-
}
186-
187-
// Extract the necessary information from the response
188-
$clusterAffinity = $response->getHeaderLine('neo4j-cluster-affinity');
189-
$responseData = json_decode($response->getBody(), true);
190-
191-
// Ensure that the transaction ID exists and is not empty
192-
if (!isset($responseData['transaction']['id']) || empty($responseData['transaction']['id'])) {
193-
throw new \Exception('Transaction ID is missing or empty in the response.');
194-
}
195-
196-
// Get the transaction ID
197-
$transactionId = $responseData['transaction']['id'];
198-
199-
// Return the Transaction object with the necessary details
200-
return new Transaction($this->client, $clusterAffinity, $transactionId);
201-
202-
} catch (\Exception $e) {
203-
// Handle any exceptions (e.g., network, authentication issues, etc.)
204-
// Optionally log the error or rethrow a more specific exception
205-
throw new \RuntimeException('Error initiating transaction: ' . $e->getMessage(), 0, $e);
206-
}
175+
return new Transaction($this->client, $clusterAffinity, $transactionId);
207176
}
208177

209-
210-
211-
212178
private function createProfileData(array $data): ProfiledQueryPlan
213179
{
214180
$ogm = new OGM();
@@ -263,4 +229,4 @@ private function createProfileData(array $data): ProfiledQueryPlan
263229

264230
return $profiledQueryPlan;
265231
}
266-
}
232+
}

0 commit comments

Comments
 (0)