Skip to content

Commit 0b1c2aa

Browse files
committed
split bolt connection factory into seperate factories
1 parent f27475f commit 0b1c2aa

8 files changed

+341
-111
lines changed

src/Bolt/AConnectionFactory.php

Lines changed: 0 additions & 108 deletions
This file was deleted.

src/Bolt/Connection.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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;
15+
16+
use Bolt\connection\IConnection;
17+
18+
class Connection
19+
{
20+
private IConnection $connection;
21+
/** ''|'s'|'ssc' */
22+
private string $ssl;
23+
24+
/**
25+
* @param IConnection $connection
26+
* @param ''|'s'|'ssc' $ssl
27+
*/
28+
public function __construct(IConnection $connection, string $ssl)
29+
{
30+
$this->connection = $connection;
31+
$this->ssl = $ssl;
32+
}
33+
34+
public function connect(): bool
35+
{
36+
return $this->connection->connect();
37+
}
38+
39+
public function write(string $buffer): void
40+
{
41+
$this->connection->write($buffer);
42+
}
43+
44+
public function read(int $length = 2048): string
45+
{
46+
return $this->connection->read($length);
47+
}
48+
49+
public function disconnect(): void
50+
{
51+
$this->connection->disconnect();
52+
}
53+
54+
public function getIp(): string
55+
{
56+
return $this->connection->getIp();
57+
}
58+
59+
public function getPort(): int
60+
{
61+
return $this->connection->getPort();
62+
}
63+
64+
public function getTimeout(): float
65+
{
66+
return $this->connection->getTimeout();
67+
}
68+
69+
public function setTimeout(float $timeout): void
70+
{
71+
$this->connection->setTimeout($timeout);
72+
}
73+
74+
/**
75+
* @return ''|'s'|'ssc'
76+
*/
77+
public function getEncryptionLevel(): string
78+
{
79+
return $this->ssl;
80+
}
81+
}

src/Bolt/SocketConnectionFactory.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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;
15+
16+
use Bolt\connection\Socket;
17+
18+
final class SocketConnectionFactory
19+
{
20+
private StreamConnectionFactory $factory;
21+
22+
public function __construct(StreamConnectionFactory $factory)
23+
{
24+
$this->factory = $factory;
25+
}
26+
27+
public function create(UriConfiguration $config): Connection
28+
{
29+
if ($config->getSslLevel() !== '') {
30+
return $this->factory->create($config);
31+
}
32+
33+
$connection = new Socket($config->getHost(), $config->getPort(), $config->getTimeout());
34+
35+
return new Connection($connection, '');
36+
}
37+
}

src/Bolt/SslConfigurationFactory.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laudis\Neo4j\Bolt;
6+
7+
use Laudis\Neo4j\Databags\SslConfiguration;
8+
use Laudis\Neo4j\Enum\SslMode;
9+
use Psr\Http\Message\UriInterface;
10+
11+
use function explode;
12+
use function filter_var;
13+
14+
use const FILTER_VALIDATE_IP;
15+
16+
class SslConfigurationFactory
17+
{
18+
/**
19+
* @param UriInterface $uri
20+
* @param SslConfiguration $config
21+
*
22+
* @return array{0: 's'|'ssc'|'s', 1: array}
23+
*/
24+
public function create(UriInterface $uri, SslConfiguration $config): array
25+
{
26+
$mode = $config->getMode();
27+
$sslConfig = '';
28+
if ($mode === SslMode::FROM_URL()) {
29+
$scheme = $uri->getScheme();
30+
$explosion = explode('+', $scheme, 2);
31+
$sslConfig = $explosion[1] ?? '';
32+
} elseif ($mode === SslMode::ENABLE()) {
33+
$sslConfig = 's';
34+
} elseif ($mode === SslMode::ENABLE_WITH_SELF_SIGNED()) {
35+
$sslConfig = 'ssc';
36+
}
37+
38+
if (str_starts_with($sslConfig, 's')) {
39+
return [$sslConfig, $this->enableSsl($uri->getHost(), $sslConfig, $config)];
40+
}
41+
42+
return [$sslConfig, null];
43+
}
44+
45+
private function enableSsl(string $host, string $sslConfig, SslConfiguration $config): ?array
46+
{
47+
$options = [
48+
'verify_peer' => $config->isVerifyPeer(),
49+
'peer_name' => $host,
50+
];
51+
if (!filter_var($host, FILTER_VALIDATE_IP)) {
52+
$options['SNI_enabled'] = true;
53+
}
54+
if ($sslConfig === 's') {
55+
return $options;
56+
}
57+
58+
if ($sslConfig === 'ssc') {
59+
$options['allow_self_signed'] = true;
60+
61+
return $options;
62+
}
63+
64+
return null;
65+
}
66+
}

src/Bolt/StreamConnectionFactory.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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;
15+
16+
use Bolt\connection\IConnection;
17+
use Bolt\connection\StreamSocket;
18+
use function explode;
19+
use const FILTER_VALIDATE_IP;
20+
use function filter_var;
21+
use Laudis\Neo4j\Databags\SslConfiguration;
22+
use Laudis\Neo4j\Enum\SslMode;
23+
use Psr\Http\Message\UriInterface;
24+
25+
final class StreamConnectionFactory
26+
{
27+
public function create(UriConfiguration $config): Connection
28+
{
29+
$connection = new StreamSocket($config->getHost(), $config->getPort(), $config->getTimeout());
30+
if ($config->getSslLevel() !== '') {
31+
$connection->setSslContextOptions($config->getSslConfiguration());
32+
}
33+
34+
return new Connection($connection, $config->getSslLevel());
35+
}
36+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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;
15+
16+
use function extension_loaded;
17+
18+
/**
19+
* Singleton connection factory based on the installed extensions.
20+
*/
21+
class SystemWideConnectionFactory
22+
{
23+
/**
24+
* @var SocketConnectionFactory|StreamConnectionFactory
25+
*/
26+
private $factory;
27+
private static ?SystemWideConnectionFactory $instance = null;
28+
29+
/**
30+
* @param SocketConnectionFactory|StreamConnectionFactory $factory
31+
*/
32+
private function __construct($factory)
33+
{
34+
$this->factory = $factory;
35+
}
36+
37+
public static function getInstance(): SystemWideConnectionFactory
38+
{
39+
if (self::$instance === null) {
40+
$factory = new StreamConnectionFactory();
41+
if (extension_loaded('sockets')) {
42+
self::$instance = new self(new SocketConnectionFactory($factory));
43+
} else {
44+
self::$instance = new self($factory);
45+
}
46+
}
47+
48+
return self::$instance;
49+
}
50+
51+
public function create(UriConfiguration $config): Connection
52+
{
53+
return $this->factory->create($config);
54+
}
55+
}

0 commit comments

Comments
 (0)