Skip to content

Commit 3108307

Browse files
committed
fixed typing errors Bolt
1 parent 48ec92f commit 3108307

File tree

9 files changed

+92
-77
lines changed

9 files changed

+92
-77
lines changed

src/Common/BoltConnection.php renamed to src/Bolt/BoltConnection.php

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
* file that was distributed with this source code.
1212
*/
1313

14-
namespace Laudis\Neo4j\Common;
14+
namespace Laudis\Neo4j\Bolt;
1515

1616
use BadMethodCallException;
1717
use Bolt\protocol\V3;
1818
use Bolt\protocol\V4;
19-
use Laudis\Neo4j\Bolt\ServerStateTransition;
20-
use Laudis\Neo4j\Bolt\ServerStateTransitionRepository;
2119
use Laudis\Neo4j\BoltFactory;
20+
use Laudis\Neo4j\Common\ConnectionConfiguration;
2221
use Laudis\Neo4j\Contracts\ConnectionInterface;
2322
use Laudis\Neo4j\Databags\DatabaseInfo;
2423
use Laudis\Neo4j\Databags\DriverConfiguration;
@@ -53,11 +52,8 @@ final class BoltConnection implements ConnectionInterface
5352
/**
5453
* @psalm-mutation-free
5554
*/
56-
public function __construct(
57-
BoltFactory $factory,
58-
?V3 $boltProtocol,
59-
ConnectionConfiguration $config
60-
) {
55+
public function __construct(BoltFactory $factory, ?V3 $boltProtocol, ConnectionConfiguration $config)
56+
{
6157
$this->factory = $factory;
6258
$this->boltProtocol = $boltProtocol;
6359
$this->transitions = ServerStateTransitionRepository::getInstance();
@@ -119,7 +115,7 @@ public function getAccessMode(): AccessMode
119115
/**
120116
* @psalm-mutation-free
121117
*/
122-
public function getDatabaseInfo(): DatabaseInfo
118+
public function getDatabaseInfo(): ?DatabaseInfo
123119
{
124120
return $this->config->getDatabaseInfo();
125121
}
@@ -148,8 +144,8 @@ public function setTimeout(float $timeout): void
148144

149145
public function close(): void
150146
{
151-
$this->handleMessage('GOODBYE', function () {
152-
$this->boltProtocol->goodbye();
147+
$this->handleMessage('GOODBYE', static function (V3 $bolt) {
148+
$bolt->goodbye();
153149
});
154150

155151
$this->boltProtocol = null;
@@ -159,9 +155,7 @@ public function close(): void
159155

160156
public function reset(): void
161157
{
162-
$this->handleMessage('RESET', function () {
163-
$this->boltProtocol->reset();
164-
});
158+
$this->handleMessage('RESET', static fn (V3 $bolt) => $bolt->reset());
165159

166160
$this->boltProtocol = $this->factory->build()[0];
167161
$this->beforeTransitionEventListeners = [];
@@ -174,16 +168,31 @@ public function reset(): void
174168
*/
175169
public function begin(?string $database, ?float $timeout): void
176170
{
177-
$this->handleMessage('BEGIN', function () use ($database, $timeout) {
178-
$this->boltProtocol->begin($this->buildExtra($database, $timeout));
171+
$extra = $this->buildExtra($database, $timeout);
172+
$this->handleMessage('BEGIN', static fn (V3 $bolt) => $bolt->begin($extra));
173+
}
174+
175+
/**
176+
* @param string|null $database the database to connect to
177+
* @param float|null $timeout timeout in seconds
178+
*/
179+
public function discard(?int $qid): void
180+
{
181+
$extra = $this->buildExtraParam($qid, null);
182+
$this->handleMessage('DISCARD', function (V3 $bolt) use ($extra) {
183+
if ($bolt instanceof V4) {
184+
$bolt->discard($extra);
185+
} else {
186+
$bolt->discardAll($extra);
187+
}
179188
});
180189
}
181190

182191
/**
183192
* @template T
184193
*
185-
* @param string $message the bolt message we are trying to send
186-
* @param callable(): T $action the actual action to send the message
194+
* @param string $message the bolt message we are trying to send
195+
* @param callable(V3): T $action the actual action to send the message
187196
*
188197
* @return T
189198
*/
@@ -203,7 +212,7 @@ private function handleMessage(string $message, $action)
203212
$this->triggerBeforeEvents($transitions);
204213

205214
try {
206-
$tbr = $action();
215+
$tbr = $action($this->boltProtocol);
207216

208217
// If no exceptions are thrown, we know the underlying bolt library
209218
// received a success response, making sure we can use the success transition
@@ -220,7 +229,9 @@ private function handleMessage(string $message, $action)
220229
} finally {
221230
// In the end, all listeners need to be able to handle the state transition,
222231
// regardless of the call stack.
223-
$this->triggerAfterEvents($transition);
232+
if (isset($transition)) {
233+
$this->triggerAfterEvents($transition);
234+
}
224235
}
225236

226237
return $tbr;
@@ -231,43 +242,37 @@ private function handleMessage(string $message, $action)
231242
*/
232243
public function run(string $text, array $parameters, ?string $database, ?float $timeout): array
233244
{
234-
return $this->handleMessage('RUN', function () use ($text, $parameters, $database, $timeout) {
235-
return $this->boltProtocol->run($text, $parameters, $this->buildExtra($database, $timeout));
245+
return $this->handleMessage('RUN', function (V3 $bolt) use ($text, $parameters, $database, $timeout) {
246+
/** @var BoltMeta */
247+
return $bolt->run($text, $parameters, $this->buildExtra($database, $timeout));
236248
});
237249
}
238250

239251
public function commit(): void
240252
{
241-
$this->handleMessage('COMMIT', fn () => $this->boltProtocol->commit());
253+
$this->handleMessage('COMMIT', static fn (V3 $bolt) => $bolt->commit());
242254
}
243255

244256
public function rollback(): void
245257
{
246-
$this->handleMessage('ROLLBACK', fn () => $this->boltProtocol->commit());
258+
$this->handleMessage('ROLLBACK', static fn (V3 $bolt) => $bolt->commit());
247259
}
248260

249261
/**
250262
* @return non-empty-list<list>
251263
*/
252264
public function pull(?int $qid, ?int $fetchSize): array
253265
{
254-
return $this->handleMessage('PULL', function () use ($qid, $fetchSize) {
255-
$extra = [];
256-
if ($fetchSize) {
257-
$extra['n'] = $fetchSize;
258-
}
266+
$extra = $this->buildExtraParam($fetchSize, $qid);
259267

260-
if ($qid) {
261-
$extra['qid'] = $qid;
262-
}
263-
264-
if (!$this->boltProtocol instanceof V4) {
268+
return $this->handleMessage('PULL', function (V3 $bolt) use ($extra) {
269+
if (!$bolt instanceof V4) {
265270
/** @var non-empty-list<list> */
266-
return $this->boltProtocol->pullAll($extra);
271+
return $bolt->pullAll($extra);
267272
}
268273

269274
/** @var non-empty-list<list> */
270-
return $this->boltProtocol->pull($extra);
275+
return $bolt->pull($extra);
271276
});
272277
}
273278

@@ -316,6 +321,20 @@ public function bindBeforeTransitionEventListener($listener): void
316321
$this->beforeTransitionEventListeners[] = $listener;
317322
}
318323

324+
public function buildExtraParam(?int $fetchSize, ?int $qid): array
325+
{
326+
$extra = [];
327+
if ($fetchSize) {
328+
$extra['n'] = $fetchSize;
329+
}
330+
331+
if ($qid) {
332+
$extra['qid'] = $qid;
333+
}
334+
335+
return $extra;
336+
}
337+
319338
/**
320339
* @param callable(ServerStateTransition): void $listener
321340
*/

src/Bolt/BoltConnectionPool.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313

1414
namespace Laudis\Neo4j\Bolt;
1515

16-
use Bolt\Bolt;
17-
use Bolt\protocol\V3;
1816
use Exception;
1917
use Laudis\Neo4j\BoltFactory;
20-
use Laudis\Neo4j\Common\BoltConnection;
18+
use Laudis\Neo4j\Common\ConnectionConfiguration;
2119
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2220
use Laudis\Neo4j\Contracts\ConnectionPoolInterface;
2321
use Laudis\Neo4j\Databags\DatabaseInfo;
@@ -26,7 +24,9 @@
2624
use Laudis\Neo4j\Enum\ConnectionProtocol;
2725
use Laudis\Neo4j\Neo4j\RoutingTable;
2826
use Psr\Http\Message\UriInterface;
27+
use Bolt\protocol\V3;
2928
use Throwable;
29+
use function explode;
3030

3131
/**
3232
* Manages singular Bolt connections.
@@ -104,9 +104,6 @@ public function canConnect(UriInterface $uri, AuthenticateInterface $authenticat
104104
return true;
105105
}
106106

107-
/**
108-
* @throws \ReflectionException
109-
*/
110107
private function getConnection(
111108
UriInterface $connectingTo,
112109
AuthenticateInterface $authenticate,
@@ -115,16 +112,16 @@ private function getConnection(
115112
$factory = BoltFactory::fromVariables($connectingTo, $authenticate, $this->driverConfig);
116113
[$bolt, $response] = $factory->build();
117114

118-
return new BoltConnection(
115+
$config = new ConnectionConfiguration(
119116
$response['server'],
120117
$connectingTo,
121118
explode('/', $response['server'])[1] ?? '',
122119
ConnectionProtocol::determineBoltVersion($bolt),
123120
$config->getAccessMode(),
124-
new DatabaseInfo($config->getDatabase()),
125-
$factory,
126-
$bolt,
127-
$this->driverConfig
121+
$this->driverConfig,
122+
$config->getDatabase() === null ? null : new DatabaseInfo($config->getDatabase())
128123
);
124+
125+
return new BoltConnection($factory, $bolt, $config);
129126
}
130127
}

src/Bolt/BoltResult.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313

1414
namespace Laudis\Neo4j\Bolt;
1515

16-
use function array_splice;
17-
use Bolt\protocol\V4;
18-
use function call_user_func;
19-
use function count;
16+
use BadMethodCallException;
2017
use Generator;
2118
use Iterator;
22-
use Laudis\Neo4j\Common\BoltConnection;
2319
use Laudis\Neo4j\Enum\ConnectionProtocol;
20+
use function array_splice;
21+
use function call_user_func;
22+
use function count;
2423

2524
/**
2625
* @psalm-import-type BoltCypherStats from \Laudis\Neo4j\Contracts\FormatterInterface
@@ -34,7 +33,7 @@ final class BoltResult implements Iterator
3433
/** @var list<list> */
3534
private array $rows = [];
3635
private ?array $meta = null;
37-
/** @var callable(array):void|null */
36+
/** @var (callable(array):void)|null */
3837
private $finishedCallback;
3938
private int $qid;
4039

@@ -150,6 +149,7 @@ public function valid(): bool
150149
public function rewind(): void
151150
{
152151
// Rewind is impossible
152+
throw new BadMethodCallException('Cannot rewind a bolt result.');
153153
}
154154

155155
public function __destruct()
@@ -165,9 +165,6 @@ public function __destruct()
165165

166166
public function discard(): void
167167
{
168-
$v3 = $this->connection->getImplementation();
169-
if ($v3 instanceof V4 && $this->meta === null) {
170-
$v3->discard(['qid' => $this->qid]);
171-
}
168+
$this->connection->discard($this->qid);
172169
}
173170
}

src/Bolt/BoltUnmanagedTransaction.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use Bolt\error\ConnectionTimeoutException;
1717
use Bolt\error\MessageException;
18-
use Laudis\Neo4j\Common\BoltConnection;
1918
use Laudis\Neo4j\Common\TransactionHelper;
2019
use Laudis\Neo4j\Contracts\FormatterInterface;
2120
use Laudis\Neo4j\Contracts\UnmanagedTransactionInterface;
@@ -26,8 +25,8 @@
2625
use Laudis\Neo4j\ParameterHelper;
2726
use Laudis\Neo4j\Types\AbstractCypherSequence;
2827
use Laudis\Neo4j\Types\CypherList;
29-
use function microtime;
3028
use Throwable;
29+
use function microtime;
3130

3231
/**
3332
* Manages a transaction over the bolt protocol.

src/Bolt/Session.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use Bolt\error\MessageException;
1717
use Exception;
18-
use Laudis\Neo4j\Common\BoltConnection;
1918
use Laudis\Neo4j\Common\TransactionHelper;
2019
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2120
use Laudis\Neo4j\Contracts\ConnectionPoolInterface;
@@ -160,9 +159,13 @@ private function beginInstantTransaction(
160159
private function acquireConnection(TransactionConfiguration $config, SessionConfiguration $sessionConfig): BoltConnection
161160
{
162161
$connection = $this->pool->acquire($this->uri, $this->auth, $sessionConfig);
162+
163163
// We try and let the server do the timeout management.
164164
// Since the client should not run indefinitely, we just multiply the client side by two, just in case
165-
$connection->setTimeout($config->getTimeout() * 2);
165+
$timeout = $config->getTimeout();
166+
if ($timeout) {
167+
$connection->setTimeout($timeout * 2);
168+
}
166169

167170
return $connection;
168171
}

src/Common/ConnectionConfiguration.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ final class ConnectionConfiguration
2929
private string $serverVersion;
3030
private ConnectionProtocol $protocol;
3131
private AccessMode $accessMode;
32-
private DatabaseInfo $databaseInfo;
3332
private DriverConfiguration $driverConfiguration;
33+
private ?DatabaseInfo $databaseInfo;
3434

3535
public function __construct(
3636
string $serverAgent,
3737
UriInterface $serverAddress,
3838
string $serverVersion,
3939
ConnectionProtocol $protocol,
4040
AccessMode $accessMode,
41-
DatabaseInfo $databaseInfo,
42-
DriverConfiguration $driverConfiguration
41+
DriverConfiguration $driverConfiguration,
42+
?DatabaseInfo $databaseInfo
4343
) {
4444
$this->serverAgent = $serverAgent;
4545
$this->serverAddress = $serverAddress;
4646
$this->serverVersion = $serverVersion;
4747
$this->protocol = $protocol;
4848
$this->accessMode = $accessMode;
49-
$this->databaseInfo = $databaseInfo;
5049
$this->driverConfiguration = $driverConfiguration;
50+
$this->databaseInfo = $databaseInfo;
5151
}
5252

5353
public function getServerAgent(): string
@@ -75,13 +75,13 @@ public function getAccessMode(): AccessMode
7575
return $this->accessMode;
7676
}
7777

78-
public function getDatabaseInfo(): DatabaseInfo
78+
public function getDriverConfiguration(): DriverConfiguration
7979
{
80-
return $this->databaseInfo;
80+
return $this->driverConfiguration;
8181
}
8282

83-
public function getDriverConfiguration(): DriverConfiguration
83+
public function getDatabaseInfo(): ?DatabaseInfo
8484
{
85-
return $this->driverConfiguration;
85+
return $this->databaseInfo;
8686
}
8787
}

src/Contracts/ConnectionInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function getAccessMode(): AccessMode;
7474
*
7575
* @psalm-mutation-free
7676
*/
77-
public function getDatabaseInfo(): DatabaseInfo;
77+
public function getDatabaseInfo(): ?DatabaseInfo;
7878

7979
/**
8080
* Opens the connection.

0 commit comments

Comments
 (0)