Skip to content

Commit 7913dac

Browse files
committed
refactor: Add BoltMessages and BoltMessageFactory
1 parent 6c578a0 commit 7913dac

10 files changed

+426
-31
lines changed

src/Bolt/BoltConnection.php

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Bolt\protocol\V5_3;
2424
use Bolt\protocol\V5_4;
2525
use Exception;
26+
use Laudis\Neo4j\Bolt\Messages\BoltGoodbyeMessage;
2627
use Laudis\Neo4j\Common\ConnectionConfiguration;
2728
use Laudis\Neo4j\Common\Neo4jLogger;
2829
use Laudis\Neo4j\Contracts\AuthenticateInterface;
@@ -37,6 +38,7 @@
3738
use Laudis\Neo4j\Types\CypherList;
3839
use Psr\Http\Message\UriInterface;
3940
use Psr\Log\LogLevel;
41+
use Throwable;
4042
use WeakReference;
4143

4244
/**
@@ -46,6 +48,8 @@
4648
*/
4749
class BoltConnection implements ConnectionInterface
4850
{
51+
private BoltMessageFactory $messageFactory;
52+
4953
/**
5054
* @note We are using references to "subscribed results" to maintain backwards compatibility and try and strike
5155
* a balance between performance and ease of use.
@@ -80,7 +84,9 @@ public function __construct(
8084
/** @psalm-readonly */
8185
private readonly ConnectionConfiguration $config,
8286
private readonly ?Neo4jLogger $logger,
83-
) {}
87+
) {
88+
$this->messageFactory = new BoltMessageFactory($this->protocol(), $this->logger);
89+
}
8490

8591
public function getEncryptionLevel(): string
8692
{
@@ -194,10 +200,8 @@ public function consumeResults(): void
194200
*/
195201
public function reset(): void
196202
{
197-
$this->logger?->log(LogLevel::DEBUG, 'RESET');
198-
$response = $this->protocol()
199-
->reset()
200-
->getResponse();
203+
$message = $this->messageFactory->createResetMessage();
204+
$response = $message->send()->getResponse();
201205
$this->assertNoFailure($response);
202206
$this->subscribedResults = [];
203207
}
@@ -212,10 +216,8 @@ public function begin(?string $database, ?float $timeout, BookmarkHolder $holder
212216
$this->consumeResults();
213217

214218
$extra = $this->buildRunExtra($database, $timeout, $holder, AccessMode::WRITE());
215-
$this->logger?->log(LogLevel::DEBUG, 'BEGIN', $extra);
216-
$response = $this->protocol()
217-
->begin($extra)
218-
->getResponse();
219+
$message = $this->messageFactory->createBeginMessage($extra);
220+
$response = $message->send()->getResponse();
219221
$this->assertNoFailure($response);
220222
}
221223

@@ -227,10 +229,9 @@ public function begin(?string $database, ?float $timeout, BookmarkHolder $holder
227229
public function discard(?int $qid): void
228230
{
229231
$extra = $this->buildResultExtra(null, $qid);
230-
$this->logger?->log(LogLevel::DEBUG, 'DISCARD', $extra);
231-
$response = $this->protocol()
232-
->discard($extra)
233-
->getResponse();
232+
233+
$message = $this->messageFactory->createDiscardMessage($extra);
234+
$response = $message->send()->getResponse();
234235
$this->assertNoFailure($response);
235236
}
236237

@@ -247,14 +248,13 @@ public function run(
247248
?string $database,
248249
?float $timeout,
249250
BookmarkHolder $holder,
250-
?AccessMode $mode
251+
?AccessMode $mode,
251252
): array {
252253
$extra = $this->buildRunExtra($database, $timeout, $holder, $mode);
253-
$this->logger?->log(LogLevel::DEBUG, 'RUN', $extra);
254-
$response = $this->protocol()
255-
->run($text, $parameters, $extra)
256-
->getResponse();
254+
$message = $this->messageFactory->createRunMessage($text, $parameters, $extra);
255+
$response = $message->send()->getResponse();
257256
$this->assertNoFailure($response);
257+
258258
/** @var BoltMeta */
259259
return $response->content;
260260
}
@@ -266,12 +266,10 @@ public function run(
266266
*/
267267
public function commit(): void
268268
{
269-
$this->logger?->log(LogLevel::DEBUG, 'COMMIT');
270269
$this->consumeResults();
271270

272-
$response = $this->protocol()
273-
->commit()
274-
->getResponse();
271+
$message = $this->messageFactory->createCommitMessage();
272+
$response = $message->send()->getResponse();
275273
$this->assertNoFailure($response);
276274
}
277275

@@ -282,12 +280,10 @@ public function commit(): void
282280
*/
283281
public function rollback(): void
284282
{
285-
$this->logger?->log(LogLevel::DEBUG, 'ROLLBACK');
286283
$this->consumeResults();
287284

288-
$response = $this->protocol()
289-
->rollback()
290-
->getResponse();
285+
$message = $this->messageFactory->createRollbackMessage();
286+
$response = $message->send()->getResponse();
291287
$this->assertNoFailure($response);
292288
}
293289

@@ -313,8 +309,10 @@ public function pull(?int $qid, ?int $fetchSize): array
313309
$this->logger?->log(LogLevel::DEBUG, 'PULL', $extra);
314310

315311
$tbr = [];
312+
$message = $this->messageFactory->createPullMessage($extra);
313+
316314
/** @var Response $response */
317-
foreach ($this->protocol()->pull($extra)->getResponses() as $response) {
315+
foreach ($message->send()->getResponses() as $response) {
318316
$this->assertNoFailure($response);
319317
$tbr[] = $response->content;
320318
}
@@ -336,12 +334,15 @@ public function close(): void
336334
$this->consumeResults();
337335
}
338336

339-
$this->logger?->log(LogLevel::DEBUG, 'GOODBYE');
340-
$this->protocol()->goodbye();
337+
$message = new BoltGoodbyeMessage(
338+
$this->protocol(),
339+
$this->logger
340+
);
341+
$message->send();
341342

342343
unset($this->boltProtocol); // has to be set to null as the sockets don't recover nicely contrary to what the underlying code might lead you to believe;
343344
}
344-
} catch (\Throwable) {
345+
} catch (Throwable) {
345346
}
346347
}
347348

@@ -403,7 +404,8 @@ private function assertNoFailure(Response $response): void
403404
{
404405
if ($response->signature === Signature::FAILURE) {
405406
$this->logger?->log(LogLevel::ERROR, 'FAILURE');
406-
$resetResponse = $this->protocol()->reset()->getResponse();
407+
$message = $this->messageFactory->createResetMessage();
408+
$resetResponse = $message->send()->getResponse();
407409
$this->subscribedResults = [];
408410
if ($resetResponse->signature === Signature::FAILURE) {
409411
throw new Neo4jException([Neo4jError::fromBoltResponse($resetResponse), Neo4jError::fromBoltResponse($response)]);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Neo4j PHP Client and Driver package.
7+
*
8+
* (c) Nagels <https://nagels.tech>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Laudis\Neo4j\Bolt\Messages;
15+
16+
use Bolt\protocol\V4_4;
17+
use Bolt\protocol\V5;
18+
use Bolt\protocol\V5_1;
19+
use Bolt\protocol\V5_2;
20+
use Bolt\protocol\V5_3;
21+
use Bolt\protocol\V5_4;
22+
use Laudis\Neo4j\Common\Neo4jLogger;
23+
use Laudis\Neo4j\Contracts\BoltMessage;
24+
use Psr\Log\LogLevel;
25+
26+
final class BoltBeginMessage extends BoltMessage
27+
{
28+
public function __construct(
29+
private readonly V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol,
30+
private readonly array $extra,
31+
private readonly ?Neo4jLogger $logger,
32+
) {
33+
parent::__construct($protocol);
34+
}
35+
36+
public function send(): BoltBeginMessage
37+
{
38+
$this->logger?->log(LogLevel::DEBUG, 'BEGIN', $this->extra);
39+
$this->protocol->begin($this->extra);
40+
41+
return $this;
42+
}
43+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Neo4j PHP Client and Driver package.
7+
*
8+
* (c) Nagels <https://nagels.tech>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Laudis\Neo4j\Bolt\Messages;
15+
16+
use Bolt\protocol\V4_4;
17+
use Bolt\protocol\V5;
18+
use Bolt\protocol\V5_1;
19+
use Bolt\protocol\V5_2;
20+
use Bolt\protocol\V5_3;
21+
use Bolt\protocol\V5_4;
22+
use Laudis\Neo4j\Common\Neo4jLogger;
23+
use Laudis\Neo4j\Contracts\BoltMessage;
24+
use Psr\Log\LogLevel;
25+
26+
final class BoltCommitMessage extends BoltMessage
27+
{
28+
public function __construct(
29+
private readonly V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol,
30+
private readonly ?Neo4jLogger $logger,
31+
) {
32+
parent::__construct($protocol);
33+
}
34+
35+
public function send(): BoltCommitMessage
36+
{
37+
$this->logger?->log(LogLevel::DEBUG, 'COMMIT');
38+
$this->protocol->commit();
39+
40+
return $this;
41+
}
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Neo4j PHP Client and Driver package.
7+
*
8+
* (c) Nagels <https://nagels.tech>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Laudis\Neo4j\Bolt\Messages;
15+
16+
use Bolt\protocol\V4_4;
17+
use Bolt\protocol\V5;
18+
use Bolt\protocol\V5_1;
19+
use Bolt\protocol\V5_2;
20+
use Bolt\protocol\V5_3;
21+
use Bolt\protocol\V5_4;
22+
use Laudis\Neo4j\Common\Neo4jLogger;
23+
use Laudis\Neo4j\Contracts\BoltMessage;
24+
use Psr\Log\LogLevel;
25+
26+
final class BoltDiscardMessage extends BoltMessage
27+
{
28+
public function __construct(
29+
private readonly V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol,
30+
private readonly array $extra,
31+
private readonly ?Neo4jLogger $logger,
32+
) {
33+
parent::__construct($protocol);
34+
}
35+
36+
public function send(): BoltDiscardMessage
37+
{
38+
$this->logger?->log(LogLevel::DEBUG, 'DISCARD', $this->extra);
39+
$this->protocol->discard($this->extra);
40+
41+
return $this;
42+
}
43+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Neo4j PHP Client and Driver package.
7+
*
8+
* (c) Nagels <https://nagels.tech>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Laudis\Neo4j\Bolt\Messages;
15+
16+
use Bolt\protocol\V4_4;
17+
use Bolt\protocol\V5;
18+
use Bolt\protocol\V5_1;
19+
use Bolt\protocol\V5_2;
20+
use Bolt\protocol\V5_3;
21+
use Bolt\protocol\V5_4;
22+
use Laudis\Neo4j\Common\Neo4jLogger;
23+
use Laudis\Neo4j\Contracts\BoltMessage;
24+
use Psr\Log\LogLevel;
25+
26+
final class BoltGoodbyeMessage extends BoltMessage
27+
{
28+
public function __construct(
29+
private readonly V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol,
30+
private readonly ?Neo4jLogger $logger,
31+
) {
32+
parent::__construct($protocol);
33+
}
34+
35+
public function send(): BoltGoodbyeMessage
36+
{
37+
$this->logger?->log(LogLevel::DEBUG, 'GOODBYE');
38+
$this->protocol->goodbye();
39+
40+
return $this;
41+
}
42+
}

src/Bolt/Messages/BoltPullMessage.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Neo4j PHP Client and Driver package.
7+
*
8+
* (c) Nagels <https://nagels.tech>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Laudis\Neo4j\Bolt\Messages;
15+
16+
use Bolt\protocol\V4_4;
17+
use Bolt\protocol\V5;
18+
use Bolt\protocol\V5_1;
19+
use Bolt\protocol\V5_2;
20+
use Bolt\protocol\V5_3;
21+
use Bolt\protocol\V5_4;
22+
use Laudis\Neo4j\Common\Neo4jLogger;
23+
use Laudis\Neo4j\Contracts\BoltMessage;
24+
use Psr\Log\LogLevel;
25+
26+
final class BoltPullMessage extends BoltMessage
27+
{
28+
public function __construct(
29+
private readonly V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol,
30+
private readonly array $extra,
31+
private readonly ?Neo4jLogger $logger,
32+
) {
33+
parent::__construct($protocol);
34+
}
35+
36+
public function send(): BoltPullMessage
37+
{
38+
$this->logger?->log(LogLevel::DEBUG, 'PULL', $this->extra);
39+
$this->protocol->pull($this->extra);
40+
41+
return $this;
42+
}
43+
}

0 commit comments

Comments
 (0)