18
18
use function function_exists ;
19
19
use function is_callable ;
20
20
use Laudis \Neo4j \Common \Cache ;
21
+ use Laudis \Neo4j \Common \SemaphoreFactory ;
22
+ use Laudis \Neo4j \Contracts \SemaphoreFactoryInterface ;
21
23
use Psr \SimpleCache \CacheInterface ;
22
24
use function sprintf ;
23
25
24
26
/**
25
27
* Configuration object for the driver.
26
- *
27
- * @psalm-immutable
28
28
*/
29
29
final class DriverConfiguration
30
30
{
@@ -34,49 +34,54 @@ final class DriverConfiguration
34
34
public const DEFAULT_ACQUIRE_CONNECTION_TIMEOUT = 2.0 ;
35
35
36
36
private ?string $ userAgent ;
37
- /** @var pure- callable():(HttpPsrBindings|null)|HttpPsrBindings|null */
37
+ /** @var callable():(HttpPsrBindings|null)|HttpPsrBindings|null */
38
38
private $ httpPsrBindings ;
39
39
private SslConfiguration $ sslConfig ;
40
40
private ?int $ maxPoolSize ;
41
- /** @var pure- callable():(CacheInterface|null)|CacheInterface|null */
41
+ /** @var callable():(CacheInterface|null)|CacheInterface|null */
42
42
private $ cache ;
43
+ /** @var callable():(SemaphoreFactoryInterface|null)|SemaphoreFactoryInterface|null */
44
+ private $ semaphoreFactory ;
43
45
/** @var ?float */
44
46
private ?float $ acquireConnectionTimeout ;
45
47
46
48
/**
47
- * @param pure-callable():(HttpPsrBindings|null)|HttpPsrBindings|null $httpPsrBindings
48
- * @param pure-callable():(CacheInterface|null)|CacheInterface|null $cache
49
+ * @param callable():(HttpPsrBindings|null)|HttpPsrBindings|null $httpPsrBindings
50
+ * @param callable():(CacheInterface|null)|CacheInterface|null $cache
51
+ * @param callable():(SemaphoreFactoryInterface|null)|SemaphoreFactoryInterface|null $semaphore
52
+ *
53
+ * @psalm-immutable
49
54
*/
50
- public function __construct (?string $ userAgent , $ httpPsrBindings , SslConfiguration $ sslConfig , ?int $ maxPoolSize , $ cache , ?float $ acquireConnectionTimeout )
55
+ public function __construct (?string $ userAgent , $ httpPsrBindings , SslConfiguration $ sslConfig , ?int $ maxPoolSize , $ cache , ?float $ acquireConnectionTimeout, $ semaphore )
51
56
{
52
57
$ this ->userAgent = $ userAgent ;
53
58
$ this ->httpPsrBindings = $ httpPsrBindings ;
54
59
$ this ->sslConfig = $ sslConfig ;
55
60
$ this ->maxPoolSize = $ maxPoolSize ;
56
61
$ this ->cache = $ cache ;
57
62
$ this ->acquireConnectionTimeout = $ acquireConnectionTimeout ;
63
+ $ this ->semaphoreFactory = $ semaphore ;
58
64
}
59
65
60
66
/**
61
67
* @param pure-callable():(HttpPsrBindings|null)|HttpPsrBindings|null $httpPsrBindings
62
68
*
63
- * @pure
69
+ * @psalm-immutable
64
70
*/
65
- public static function create (?string $ userAgent , $ httpPsrBindings , SslConfiguration $ sslConfig , int $ maxPoolSize , CacheInterface $ cache , float $ acquireConnectionTimeout ): self
71
+ public static function create (?string $ userAgent , $ httpPsrBindings , SslConfiguration $ sslConfig , int $ maxPoolSize , CacheInterface $ cache , float $ acquireConnectionTimeout, SemaphoreFactoryInterface $ semaphore ): self
66
72
{
67
- return new self ($ userAgent , $ httpPsrBindings , $ sslConfig , $ maxPoolSize , $ cache , $ acquireConnectionTimeout );
73
+ return new self ($ userAgent , $ httpPsrBindings , $ sslConfig , $ maxPoolSize , $ cache , $ acquireConnectionTimeout, $ semaphore );
68
74
}
69
75
70
76
/**
71
77
* Creates a default configuration with a user agent based on the driver version
72
78
* and HTTP PSR implementation auto detected from the environment.
73
79
*
74
- * @pure
80
+ * @psalm-immutable
75
81
*/
76
82
public static function default (): self
77
83
{
78
- /** @psalm-suppress ImpureMethodCall */
79
- return new self (null , HttpPsrBindings::default (), SslConfiguration::default (), null , null , null );
84
+ return new self (null , HttpPsrBindings::default (), SslConfiguration::default (), null , null , null , null );
80
85
}
81
86
82
87
public function getUserAgent (): string
@@ -89,7 +94,7 @@ public function getUserAgent(): string
89
94
$ version = '2 ' ;
90
95
}
91
96
92
- return sprintf (self ::DEFAULT_USER_AGENT , $ version );
97
+ $ this -> userAgent = sprintf (self ::DEFAULT_USER_AGENT , $ version );
93
98
}
94
99
95
100
return $ this ->userAgent ;
@@ -99,6 +104,8 @@ public function getUserAgent(): string
99
104
* Creates a new configuration with the provided user agent.
100
105
*
101
106
* @param string|null $userAgent
107
+ *
108
+ * @psalm-immutable
102
109
*/
103
110
public function withUserAgent ($ userAgent ): self
104
111
{
@@ -111,7 +118,9 @@ public function withUserAgent($userAgent): self
111
118
/**
112
119
* Creates a new configuration with the provided bindings.
113
120
*
114
- * @param pure-callable():(HttpPsrBindings|null)|HttpPsrBindings|null $bindings
121
+ * @param callable():(HttpPsrBindings|null)|HttpPsrBindings|null $bindings
122
+ *
123
+ * @psalm-immutable
115
124
*/
116
125
public function withHttpPsrBindings ($ bindings ): self
117
126
{
@@ -121,6 +130,9 @@ public function withHttpPsrBindings($bindings): self
121
130
return $ tbr ;
122
131
}
123
132
133
+ /**
134
+ * @psalm-immutable
135
+ */
124
136
public function withSslConfiguration (SslConfiguration $ config ): self
125
137
{
126
138
$ tbr = clone $ this ;
@@ -129,23 +141,29 @@ public function withSslConfiguration(SslConfiguration $config): self
129
141
return $ tbr ;
130
142
}
131
143
144
+ /**
145
+ * @psalm-immutable
146
+ */
132
147
public function getSslConfiguration (): SslConfiguration
133
148
{
134
149
return $ this ->sslConfig ;
135
150
}
136
151
137
152
public function getHttpPsrBindings (): HttpPsrBindings
138
153
{
139
- $ bindings = (is_callable ($ this ->httpPsrBindings )) ? call_user_func ($ this ->httpPsrBindings ) : $ this ->httpPsrBindings ;
154
+ $ this -> httpPsrBindings = (is_callable ($ this ->httpPsrBindings )) ? call_user_func ($ this ->httpPsrBindings ) : $ this ->httpPsrBindings ;
140
155
141
- return $ bindings ?? HttpPsrBindings::default ();
156
+ return $ this -> httpPsrBindings ??= HttpPsrBindings::default ();
142
157
}
143
158
144
159
public function getMaxPoolSize (): int
145
160
{
146
161
return $ this ->maxPoolSize ?? self ::DEFAULT_POOL_SIZE ;
147
162
}
148
163
164
+ /**
165
+ * @psalm-immutable
166
+ */
149
167
public function withMaxPoolSize (?int $ maxPoolSize ): self
150
168
{
151
169
$ tbr = clone $ this ;
@@ -155,7 +173,9 @@ public function withMaxPoolSize(?int $maxPoolSize): self
155
173
}
156
174
157
175
/**
158
- * @param pure-callable():(CacheInterface|null)|CacheInterface|null $cache
176
+ * @param callable():(CacheInterface|null)|CacheInterface|null $cache
177
+ *
178
+ * @psalm-immutable
159
179
*/
160
180
public function withCache ($ cache ): self
161
181
{
@@ -167,17 +187,29 @@ public function withCache($cache): self
167
187
168
188
public function getCache (): CacheInterface
169
189
{
170
- $ cache = (is_callable ($ this ->cache )) ? call_user_func ($ this ->cache ) : $ this ->cache ;
190
+ $ this ->cache = (is_callable ($ this ->cache )) ? call_user_func ($ this ->cache ) : $ this ->cache ;
191
+
192
+ return $ this ->cache ??= Cache::getInstance ();
193
+ }
194
+
195
+ public function getSemaphoreInterface (): SemaphoreFactoryInterface
196
+ {
197
+ $ this ->semaphoreFactory = (is_callable ($ this ->semaphoreFactory )) ? call_user_func ($ this ->semaphoreFactory ) : $ this ->semaphoreFactory ;
171
198
172
- /** @psalm-suppress ImpureMethodCall */
173
- return $ cache ?? Cache::getInstance ();
199
+ return $ this ->semaphoreFactory ??= SemaphoreFactory::getInstance ();
174
200
}
175
201
202
+ /**
203
+ * @psalm-immutable
204
+ */
176
205
public function getAcquireConnectionTimeout (): float
177
206
{
178
- return $ this ->acquireConnectionTimeout ?? self ::DEFAULT_ACQUIRE_CONNECTION_TIMEOUT ;
207
+ return $ this ->acquireConnectionTimeout ??= self ::DEFAULT_ACQUIRE_CONNECTION_TIMEOUT ;
179
208
}
180
209
210
+ /**
211
+ * @psalm-immutable
212
+ */
181
213
public function withAcquireConnectionTimeout (?float $ acquireConnectionTimeout ): self
182
214
{
183
215
$ tbr = clone $ this ;
0 commit comments