Skip to content

Commit 05a23fb

Browse files
committed
correctly modified transaction state flags
1 parent fb773fb commit 05a23fb

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

src/Http/HttpUnmanagedTransaction.php

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,22 @@
1313

1414
namespace Laudis\Neo4j\Http;
1515

16-
use JsonException;
16+
use function array_intersect;
17+
use function array_unique;
18+
use Laudis\Neo4j\Common\TransactionHelper;
1719
use Laudis\Neo4j\Contracts\ConnectionInterface;
1820
use Laudis\Neo4j\Contracts\FormatterInterface;
1921
use Laudis\Neo4j\Contracts\UnmanagedTransactionInterface;
22+
use Laudis\Neo4j\Databags\Neo4jError;
2023
use Laudis\Neo4j\Databags\Statement;
24+
use Laudis\Neo4j\Exception\Neo4jException;
2125
use Laudis\Neo4j\Types\CypherList;
2226
use function microtime;
23-
use Psr\Http\Client\ClientExceptionInterface;
2427
use Psr\Http\Client\ClientInterface;
2528
use Psr\Http\Message\RequestInterface;
29+
use Psr\Http\Message\ResponseInterface;
2630
use Psr\Http\Message\StreamFactoryInterface;
31+
use stdClass;
2732

2833
/**
2934
* @template T
@@ -71,25 +76,16 @@ public function __construct(
7176
$this->formatter = $formatter;
7277
}
7378

74-
/**
75-
* @throws JsonException|ClientExceptionInterface
76-
*/
7779
public function run(string $statement, iterable $parameters = [])
7880
{
7981
return $this->runStatement(new Statement($statement, $parameters));
8082
}
8183

82-
/**
83-
* @throws JsonException|ClientExceptionInterface
84-
*/
8584
public function runStatement(Statement $statement)
8685
{
8786
return $this->runStatements([$statement])->first();
8887
}
8988

90-
/**
91-
* @throws JsonException
92-
*/
9389
public function runStatements(iterable $statements): CypherList
9490
{
9591
$request = $this->request->withMethod('POST');
@@ -100,39 +96,39 @@ public function runStatements(iterable $statements): CypherList
10096
$start = microtime(true);
10197
$response = $this->connection->getImplementation()->sendRequest($request);
10298
$total = microtime(true) - $start;
103-
$data = HttpHelper::interpretResponse($response);
99+
100+
$data = $this->handleResponse($response);
104101

105102
return $this->formatter->formatHttpResult($response, $data, $this->connection, $total, $total, $statements);
106103
}
107104

108-
/**
109-
* @throws JsonException
110-
*/
111105
public function commit(iterable $statements = []): CypherList
112106
{
113107
$uri = $this->request->getUri();
114108
$request = $this->request->withUri($uri->withPath($uri->getPath().'/commit'))->withMethod('POST');
109+
115110
$content = HttpHelper::statementsToJson($this->formatter, $statements);
116111
$request = $request->withBody($this->factory->createStream($content));
117112

118113
$start = microtime(true);
119114
$response = $this->connection->getImplementation()->sendRequest($request);
120115
$total = microtime(true) - $start;
121116

122-
$data = HttpHelper::interpretResponse($response);
117+
$data = $this->handleResponse($response);
118+
119+
$this->isCommitted = true;
123120

124121
return $this->formatter->formatHttpResult($response, $data, $this->connection, $total, $total, $statements);
125122
}
126123

127-
/**
128-
* @throws JsonException
129-
*/
130124
public function rollback(): void
131125
{
132126
$request = $this->request->withMethod('DELETE');
133127
$response = $this->connection->getImplementation()->sendRequest($request);
134128

135-
HttpHelper::interpretResponse($response);
129+
$this->handleResponse($response);
130+
131+
$this->isRolledBack = true;
136132
}
137133

138134
public function __destruct()
@@ -163,4 +159,36 @@ public function isFinished(): bool
163159
{
164160
return $this->isRolledBack() || $this->isCommitted();
165161
}
162+
163+
/**
164+
* @throws Neo4jException
165+
*
166+
* @return never
167+
*/
168+
private function handleNeo4jException(Neo4jException $e): void
169+
{
170+
$classifications = array_map(static fn (Neo4jError $e) => $e->getClassification(), $e->getErrors());
171+
$classifications = array_unique($classifications);
172+
173+
$intersection = array_intersect($classifications, TransactionHelper::ROLLBACK_CLASSIFICATIONS);
174+
if ($intersection !== []) {
175+
$this->isRolledBack = true;
176+
}
177+
178+
throw $e;
179+
}
180+
181+
/**
182+
* @throws Neo4jException
183+
*/
184+
private function handleResponse(ResponseInterface $response): stdClass
185+
{
186+
try {
187+
$data = HttpHelper::interpretResponse($response);
188+
} catch (Neo4jException $e) {
189+
$this->handleNeo4jException($e);
190+
}
191+
192+
return $data;
193+
}
166194
}

0 commit comments

Comments
 (0)