-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathClientBuilder.php
More file actions
165 lines (138 loc) · 4.52 KB
/
Copy pathClientBuilder.php
File metadata and controls
165 lines (138 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
/*
* This file is part of the Neo4j PHP Client and Driver package.
*
* (c) Nagels <https://nagels.tech>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Laudis\Neo4j;
use function in_array;
use Laudis\Neo4j\Authentication\Authenticate;
use Laudis\Neo4j\Common\DriverSetupManager;
use Laudis\Neo4j\Common\Uri;
use Laudis\Neo4j\Contracts\AuthenticateInterface;
use Laudis\Neo4j\Contracts\ClientInterface;
use Laudis\Neo4j\Databags\DriverConfiguration;
use Laudis\Neo4j\Databags\DriverSetup;
use Laudis\Neo4j\Databags\SessionConfiguration;
use Laudis\Neo4j\Databags\TransactionConfiguration;
use Laudis\Neo4j\Exception\UnsupportedScheme;
use Laudis\Neo4j\Formatter\SummarizedResultFormatter;
use Psr\Log\LoggerInterface;
/**
* Immutable factory for creating a client.
*
* @psalm-import-type OGMTypes from \Laudis\Neo4j\Types\OGMTypesAlias
*/
final class ClientBuilder
{
public const SUPPORTED_SCHEMES = ['', 'bolt', 'bolt+s', 'bolt+ssc', 'neo4j', 'neo4j+s', 'neo4j+ssc'];
/**
* @psalm-mutation-free
*/
public function __construct(
/** @psalm-readonly */
private SessionConfiguration $defaultSessionConfig,
/** @psalm-readonly */
private TransactionConfiguration $defaultTransactionConfig,
private DriverSetupManager $driverSetups,
) {
}
/**
* Creates a client builder with default configurations and an OGMFormatter.
*/
public static function create(?string $logLevel = null, ?LoggerInterface $logger = null): ClientBuilder
{
$configuration = DriverConfiguration::default();
if ($logLevel !== null && $logger !== null) {
$configuration = $configuration->withLogger($logLevel, $logger);
}
return new self(
SessionConfiguration::default(),
TransactionConfiguration::default(),
new DriverSetupManager(SummarizedResultFormatter::create(), $configuration)
);
}
public function withDriver(string $alias, string $url, ?AuthenticateInterface $authentication = null, ?int $priority = 0): self
{
$uri = Uri::create($url);
$authentication ??= Authenticate::fromUrl($uri, $this->driverSetups->getLogger());
return $this->withParsedUrl($alias, $uri, $authentication, $priority ?? 0);
}
/**
* @psalm-external-mutation-free
*/
private function withParsedUrl(string $alias, Uri $uri, AuthenticateInterface $authentication, int $priority): self
{
$scheme = $uri->getScheme();
if (!in_array($scheme, self::SUPPORTED_SCHEMES, true)) {
throw UnsupportedScheme::make($scheme, self::SUPPORTED_SCHEMES);
}
$tbr = clone $this;
$tbr->driverSetups = $this->driverSetups->withSetup(new DriverSetup($uri, $authentication), $alias, $priority);
return $tbr;
}
/**
* Sets the default connection to the given alias.
*
* @psalm-mutation-free
*/
public function withDefaultDriver(string $alias): self
{
$tbr = clone $this;
$tbr->driverSetups = $this->driverSetups->withDefault($alias);
return $tbr;
}
/**
* @psalm-mutation-free
*/
public function withFormatter(SummarizedResultFormatter $formatter): self
{
return new self(
$this->defaultSessionConfig,
$this->defaultTransactionConfig,
$this->driverSetups->withFormatter($formatter)
);
}
/**
* @psalm-mutation-free
*/
public function build(): ClientInterface
{
return new Client(
$this->driverSetups,
$this->defaultSessionConfig,
$this->defaultTransactionConfig,
);
}
/**
* @psalm-mutation-free
*/
public function withDefaultDriverConfiguration(DriverConfiguration $config): self
{
$tbr = clone $this;
$tbr->driverSetups = $tbr->driverSetups->withDriverConfiguration($config);
return $tbr;
}
/**
* @psalm-mutation-free
*/
public function withDefaultSessionConfiguration(SessionConfiguration $config): self
{
$tbr = clone $this;
$tbr->defaultSessionConfig = $config;
return $tbr;
}
/**
* @psalm-mutation-free
*/
public function withDefaultTransactionConfiguration(TransactionConfiguration $config): self
{
$tbr = clone $this;
$tbr->defaultTransactionConfig = $config;
return $tbr;
}
}