Skip to content

Commit 38492f3

Browse files
committed
updated driverconfig to have a semaphore factory interface
1 parent af90794 commit 38492f3

File tree

2 files changed

+58
-23
lines changed

2 files changed

+58
-23
lines changed

src/Common/SemaphoreFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of the Neo4j PHP Client and Driver package.
57
*
@@ -12,11 +14,12 @@
1214
namespace Laudis\Neo4j\Common;
1315

1416
use function extension_loaded;
17+
use Laudis\Neo4j\Contracts\SemaphoreFactoryInterface;
1518
use Laudis\Neo4j\Contracts\SemaphoreInterface;
1619
use Laudis\Neo4j\Databags\DriverConfiguration;
1720
use Psr\Http\Message\UriInterface;
1821

19-
final class SemaphoreFactory
22+
final class SemaphoreFactory implements SemaphoreFactoryInterface
2023
{
2124
private static ?SemaphoreFactory $instance = null;
2225
/** @var callable(string, int):SemaphoreInterface */

src/Databags/DriverConfiguration.php

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
use function function_exists;
1919
use function is_callable;
2020
use Laudis\Neo4j\Common\Cache;
21+
use Laudis\Neo4j\Common\SemaphoreFactory;
22+
use Laudis\Neo4j\Contracts\SemaphoreFactoryInterface;
2123
use Psr\SimpleCache\CacheInterface;
2224
use function sprintf;
2325

2426
/**
2527
* Configuration object for the driver.
26-
*
27-
* @psalm-immutable
2828
*/
2929
final class DriverConfiguration
3030
{
@@ -34,49 +34,54 @@ final class DriverConfiguration
3434
public const DEFAULT_ACQUIRE_CONNECTION_TIMEOUT = 2.0;
3535

3636
private ?string $userAgent;
37-
/** @var pure-callable():(HttpPsrBindings|null)|HttpPsrBindings|null */
37+
/** @var callable():(HttpPsrBindings|null)|HttpPsrBindings|null */
3838
private $httpPsrBindings;
3939
private SslConfiguration $sslConfig;
4040
private ?int $maxPoolSize;
41-
/** @var pure-callable():(CacheInterface|null)|CacheInterface|null */
41+
/** @var callable():(CacheInterface|null)|CacheInterface|null */
4242
private $cache;
43+
/** @var callable():(SemaphoreFactoryInterface|null)|SemaphoreFactoryInterface|null */
44+
private $semaphoreFactory;
4345
/** @var ?float */
4446
private ?float $acquireConnectionTimeout;
4547

4648
/**
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
4954
*/
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)
5156
{
5257
$this->userAgent = $userAgent;
5358
$this->httpPsrBindings = $httpPsrBindings;
5459
$this->sslConfig = $sslConfig;
5560
$this->maxPoolSize = $maxPoolSize;
5661
$this->cache = $cache;
5762
$this->acquireConnectionTimeout = $acquireConnectionTimeout;
63+
$this->semaphoreFactory = $semaphore;
5864
}
5965

6066
/**
6167
* @param pure-callable():(HttpPsrBindings|null)|HttpPsrBindings|null $httpPsrBindings
6268
*
63-
* @pure
69+
* @psalm-immutable
6470
*/
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
6672
{
67-
return new self($userAgent, $httpPsrBindings, $sslConfig, $maxPoolSize, $cache, $acquireConnectionTimeout);
73+
return new self($userAgent, $httpPsrBindings, $sslConfig, $maxPoolSize, $cache, $acquireConnectionTimeout, $semaphore);
6874
}
6975

7076
/**
7177
* Creates a default configuration with a user agent based on the driver version
7278
* and HTTP PSR implementation auto detected from the environment.
7379
*
74-
* @pure
80+
* @psalm-immutable
7581
*/
7682
public static function default(): self
7783
{
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);
8085
}
8186

8287
public function getUserAgent(): string
@@ -89,7 +94,7 @@ public function getUserAgent(): string
8994
$version = '2';
9095
}
9196

92-
return sprintf(self::DEFAULT_USER_AGENT, $version);
97+
$this->userAgent = sprintf(self::DEFAULT_USER_AGENT, $version);
9398
}
9499

95100
return $this->userAgent;
@@ -99,6 +104,8 @@ public function getUserAgent(): string
99104
* Creates a new configuration with the provided user agent.
100105
*
101106
* @param string|null $userAgent
107+
*
108+
* @psalm-immutable
102109
*/
103110
public function withUserAgent($userAgent): self
104111
{
@@ -111,7 +118,9 @@ public function withUserAgent($userAgent): self
111118
/**
112119
* Creates a new configuration with the provided bindings.
113120
*
114-
* @param pure-callable():(HttpPsrBindings|null)|HttpPsrBindings|null $bindings
121+
* @param callable():(HttpPsrBindings|null)|HttpPsrBindings|null $bindings
122+
*
123+
* @psalm-immutable
115124
*/
116125
public function withHttpPsrBindings($bindings): self
117126
{
@@ -121,6 +130,9 @@ public function withHttpPsrBindings($bindings): self
121130
return $tbr;
122131
}
123132

133+
/**
134+
* @psalm-immutable
135+
*/
124136
public function withSslConfiguration(SslConfiguration $config): self
125137
{
126138
$tbr = clone $this;
@@ -129,23 +141,29 @@ public function withSslConfiguration(SslConfiguration $config): self
129141
return $tbr;
130142
}
131143

144+
/**
145+
* @psalm-immutable
146+
*/
132147
public function getSslConfiguration(): SslConfiguration
133148
{
134149
return $this->sslConfig;
135150
}
136151

137152
public function getHttpPsrBindings(): HttpPsrBindings
138153
{
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;
140155

141-
return $bindings ?? HttpPsrBindings::default();
156+
return $this->httpPsrBindings ??= HttpPsrBindings::default();
142157
}
143158

144159
public function getMaxPoolSize(): int
145160
{
146161
return $this->maxPoolSize ?? self::DEFAULT_POOL_SIZE;
147162
}
148163

164+
/**
165+
* @psalm-immutable
166+
*/
149167
public function withMaxPoolSize(?int $maxPoolSize): self
150168
{
151169
$tbr = clone $this;
@@ -155,7 +173,9 @@ public function withMaxPoolSize(?int $maxPoolSize): self
155173
}
156174

157175
/**
158-
* @param pure-callable():(CacheInterface|null)|CacheInterface|null $cache
176+
* @param callable():(CacheInterface|null)|CacheInterface|null $cache
177+
*
178+
* @psalm-immutable
159179
*/
160180
public function withCache($cache): self
161181
{
@@ -167,17 +187,29 @@ public function withCache($cache): self
167187

168188
public function getCache(): CacheInterface
169189
{
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;
171198

172-
/** @psalm-suppress ImpureMethodCall */
173-
return $cache ?? Cache::getInstance();
199+
return $this->semaphoreFactory ??= SemaphoreFactory::getInstance();
174200
}
175201

202+
/**
203+
* @psalm-immutable
204+
*/
176205
public function getAcquireConnectionTimeout(): float
177206
{
178-
return $this->acquireConnectionTimeout ?? self::DEFAULT_ACQUIRE_CONNECTION_TIMEOUT;
207+
return $this->acquireConnectionTimeout ??= self::DEFAULT_ACQUIRE_CONNECTION_TIMEOUT;
179208
}
180209

210+
/**
211+
* @psalm-immutable
212+
*/
181213
public function withAcquireConnectionTimeout(?float $acquireConnectionTimeout): self
182214
{
183215
$tbr = clone $this;

0 commit comments

Comments
 (0)