Skip to content

Commit 2fff1fd

Browse files
committed
correctly handle commit url
1 parent 987d776 commit 2fff1fd

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

src/Http/HttpSession.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
use Laudis\Neo4j\Databags\TransactionConfiguration;
2727
use Laudis\Neo4j\Enum\AccessMode;
2828
use Laudis\Neo4j\Types\CypherList;
29-
use function microtime;
30-
use function parse_url;
31-
use const PHP_URL_PATH;
3229
use Psr\Http\Client\ClientInterface;
3330
use Psr\Http\Message\RequestInterface;
3431
use Psr\Http\Message\StreamFactoryInterface;
32+
use stdClass;
33+
use function is_object;
34+
use function microtime;
35+
use function parse_url;
36+
use const PHP_URL_PATH;
3537

3638
/**
3739
* @template T
@@ -76,21 +78,22 @@ final class HttpSession implements SessionInterface
7678
/**
7779
* @psalm-mutation-free
7880
*
79-
* @param FormatterInterface<T> $formatter
81+
* @param FormatterInterface<T> $formatter
8082
* @param Resolvable<StreamFactoryInterface> $factory
81-
* @param Resolvable<string> $uri
82-
* @param Resolvable<RequestFactory> $requestFactory
83+
* @param Resolvable<string> $uri
84+
* @param Resolvable<RequestFactory> $requestFactory
8385
*/
8486
public function __construct(
85-
Resolvable $factory,
86-
HttpConnectionPool $manager,
87-
SessionConfiguration $config,
88-
FormatterInterface $formatter,
89-
Resolvable $requestFactory,
90-
Resolvable $uri,
87+
Resolvable $factory,
88+
HttpConnectionPool $manager,
89+
SessionConfiguration $config,
90+
FormatterInterface $formatter,
91+
Resolvable $requestFactory,
92+
Resolvable $uri,
9193
AuthenticateInterface $auth,
92-
string $userAgent
93-
) {
94+
string $userAgent
95+
)
96+
{
9497
$this->streamFactory = $factory;
9598
$this->config = $config;
9699
$this->pool = $manager;
@@ -131,7 +134,7 @@ public function openTransaction(iterable $statements = null, ?TransactionConfigu
131134

132135
public function writeTransaction(callable $tsxHandler, ?TransactionConfiguration $config = null)
133136
{
134-
return TransactionHelper::retry(fn () => $this->openTransaction(), $tsxHandler);
137+
return TransactionHelper::retry(fn() => $this->openTransaction(), $tsxHandler);
135138
}
136139

137140
public function readTransaction(callable $tsxHandler, ?TransactionConfiguration $config = null)
@@ -171,11 +174,21 @@ public function beginTransaction(?iterable $statements = null, ?TransactionConfi
171174
{
172175
$request = $this->requestFactory->resolve()->createRequest('POST', $this->uri->resolve());
173176
$connection = $this->pool->acquire($request->getUri(), $this->auth, $this->config);
177+
178+
$request = $this->formatter->decorateRequest($request, $connection);
174179
$request->getBody()->write(HttpHelper::statementsToJson($connection, $this->formatter, $statements ?? []));
175180
$response = $connection->getImplementation()->sendRequest($request);
176181

177-
/** @var string */
178-
$url = HttpHelper::interpretResponse($response)->commit;
182+
$response = HttpHelper::interpretResponse($response);
183+
/** @var stdClass|null $info */
184+
$info = $response->info;
185+
if (is_object($info)) {
186+
/** @var string */
187+
$url = $info->commit;
188+
} else {
189+
/** @var string */
190+
$url = $response->commit;
191+
}
179192
$path = str_replace('/commit', '', parse_url($url, PHP_URL_PATH));
180193
$uri = $request->getUri()->withPath($path);
181194
$request = $request->withUri($uri);
@@ -200,7 +213,7 @@ private function makeTransaction(ConnectionInterface $connection, RequestInterfa
200213

201214
private function instantCommitRequest(RequestInterface $request): RequestInterface
202215
{
203-
$path = $request->getUri()->getPath().'/commit';
216+
$path = $request->getUri()->getPath() . '/commit';
204217
$uri = $request->getUri()->withPath($path);
205218

206219
return $request->withUri($uri);

0 commit comments

Comments
 (0)