13
13
14
14
namespace Laudis \Neo4j ;
15
15
16
- use InvalidArgumentException ;
17
16
use Laudis \Neo4j \Contracts \ClientInterface ;
18
17
use Laudis \Neo4j \Contracts \DriverInterface ;
19
18
use Laudis \Neo4j \Contracts \FormatterInterface ;
27
26
use Laudis \Neo4j \Enum \AccessMode ;
28
27
use Laudis \Neo4j \Types \CypherList ;
29
28
use Laudis \Neo4j \Types \CypherMap ;
30
- use function sprintf ;
31
29
32
30
/**
33
- * @template T
31
+ * A collection of drivers with methods to run queries though them.
34
32
*
35
- * @implements ClientInterface<T>
33
+ * @template ResultFormat
34
+ *
35
+ * @implements ClientInterface<ResultFormat>
36
36
*/
37
37
final class Client implements ClientInterface
38
38
{
39
39
private const DEFAULT_DRIVER_CONFIG = 'bolt://localhost:7687 ' ;
40
-
41
- /** @var CypherMap<DriverSetup> */
42
- private CypherMap $ driverSetups ;
43
- /** @var array<string, DriverInterface<T>> */
40
+ /** @var non-empty-array<string, DriverInterface<ResultFormat>> */
44
41
private array $ drivers ;
45
- /** @var FormatterInterface<T> */
46
- private FormatterInterface $ formatter ;
47
- private DriverConfiguration $ configuration ;
42
+ /** @psalm-readonly */
48
43
private ?string $ default ;
49
44
50
45
/**
51
- * @param CypherMap<DriverSetup> $driverConfigurations
52
- * @param FormatterInterface<T> $formatter
46
+ * @psalm-mutation-free
47
+ *
48
+ * @param CypherMap<DriverSetup> $driverSetups
49
+ * @param FormatterInterface<ResultFormat> $formatter
53
50
*/
54
- public function __construct (CypherMap $ driverConfigurations , DriverConfiguration $ configuration , FormatterInterface $ formatter , ?string $ default )
51
+ public function __construct (CypherMap $ driverSetups , DriverConfiguration $ configuration , FormatterInterface $ formatter , ?string $ default )
55
52
{
56
- $ this ->driverSetups = $ driverConfigurations ;
57
- $ this ->drivers = [];
58
- $ this ->formatter = $ formatter ;
59
- $ this ->configuration = $ configuration ;
60
53
$ this ->default = $ default ;
54
+ $ this ->drivers = $ this ->createDrivers ($ driverSetups , $ formatter , $ configuration );
61
55
}
62
56
63
57
public function run (string $ query , iterable $ parameters = [], ?string $ alias = null )
@@ -80,31 +74,20 @@ public function beginTransaction(?iterable $statements = null, ?string $alias =
80
74
return $ this ->startSession ($ alias , SessionConfiguration::default ())->beginTransaction ($ statements , $ config );
81
75
}
82
76
77
+ /**
78
+ * @psalm-mutation-free
79
+ */
83
80
public function getDriver (?string $ alias ): DriverInterface
84
81
{
85
- $ this ->createDefaultDriverIfNeeded ();
86
-
87
82
$ alias = $ this ->decideAlias ($ alias );
88
83
89
- if (!isset ($ this ->drivers [$ alias ])) {
90
- if (!$ this ->driverSetups ->hasKey ($ alias )) {
91
- $ key = sprintf ('The provided alias: "%s" was not found in the connection pool ' , $ alias );
92
- throw new InvalidArgumentException ($ key );
93
- }
94
-
95
- $ setup = $ this ->driverSetups ->get ($ alias );
96
- $ uri = $ setup ->getUri ();
97
- $ timeout = $ setup ->getSocketTimeout ();
98
- $ driver = DriverFactory::create ($ uri , $ this ->configuration , $ setup ->getAuth (), $ timeout , $ this ->formatter );
99
-
100
- $ this ->drivers [$ alias ] = $ driver ;
101
- }
102
-
103
84
return $ this ->drivers [$ alias ];
104
85
}
105
86
106
87
/**
107
- * @return SessionInterface<T>
88
+ * @psalm-mutation-free
89
+ *
90
+ * @return SessionInterface<ResultFormat>
108
91
*/
109
92
private function startSession (?string $ alias = null , SessionConfiguration $ configuration = null ): SessionInterface
110
93
{
@@ -126,14 +109,9 @@ public function transaction(callable $tsxHandler, ?string $alias = null, ?Transa
126
109
return $ this ->writeTransaction ($ tsxHandler , $ alias , $ config );
127
110
}
128
111
129
- private function createDefaultDriverIfNeeded (): void
130
- {
131
- if ($ this ->driverSetups ->isEmpty () && count ($ this ->drivers ) === 0 ) {
132
- $ driver = DriverFactory::create (self ::DEFAULT_DRIVER_CONFIG , null , null , null , $ this ->formatter );
133
- $ this ->drivers ['default ' ] = $ driver ;
134
- }
135
- }
136
-
112
+ /**
113
+ * @psalm-mutation-free
114
+ */
137
115
private function decideAlias (?string $ alias ): string
138
116
{
139
117
if ($ alias !== null ) {
@@ -144,10 +122,33 @@ private function decideAlias(?string $alias): string
144
122
return $ this ->default ;
145
123
}
146
124
147
- if ($ this ->driverSetups ->count () > 0 ) {
148
- return $ this ->driverSetups ->first ()->getKey ();
125
+ return array_key_first ($ this ->drivers );
126
+ }
127
+
128
+ /**
129
+ * @psalm-mutation-free
130
+ *
131
+ * @param CypherMap<DriverSetup> $driverSetups
132
+ * @param FormatterInterface<ResultFormat> $formatter
133
+ *
134
+ * @return non-empty-array<string, DriverInterface<ResultFormat>>
135
+ */
136
+ private function createDrivers (CypherMap $ driverSetups , FormatterInterface $ formatter , DriverConfiguration $ configuration ): array
137
+ {
138
+ if (count ($ driverSetups ) === 0 ) {
139
+ $ drivers = ['default ' => DriverFactory::create (self ::DEFAULT_DRIVER_CONFIG , null , null , null , $ formatter )];
140
+ } else {
141
+ $ drivers = [];
142
+ foreach ($ driverSetups as $ alias => $ setup ) {
143
+ $ uri = $ setup ->getUri ();
144
+ $ timeout = $ setup ->getSocketTimeout ();
145
+ $ auth = $ setup ->getAuth ();
146
+
147
+ $ drivers [$ alias ] = DriverFactory::create ($ uri , $ configuration , $ auth , $ timeout , $ formatter );
148
+ }
149
149
}
150
150
151
- return 'default ' ;
151
+ /** @var non-empty-array<string, DriverInterface<ResultFormat>> */
152
+ return $ drivers ;
152
153
}
153
154
}
0 commit comments