• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

prooph / event-store-client / 9555551525

17 Jun 2024 10:16PM UTC coverage: 70.262% (-1.1%) from 71.395%
9555551525

push

github

prolic
update coveralls repo token

3466 of 4933 relevant lines covered (70.26%)

67.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

31.47
/src/ConnectionSettingsBuilder.php
1
<?php
2

3
/**
4
 * This file is part of `prooph/event-store-client`.
5
 * (c) 2018-2024 Alexander Miertsch <kontakt@codeliner.ws>
6
 * (c) 2018-2024 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace Prooph\EventStoreClient;
15

16
use Amp\ByteStream\WritableResourceStream;
17
use Amp\File\File;
18
use Amp\Log\ConsoleFormatter;
19
use Amp\Log\StreamHandler;
20
use Monolog\Formatter\LineFormatter;
21
use Monolog\Logger as MonoLog;
22
use Prooph\EventStore\EndPoint;
23
use Prooph\EventStore\Exception\InvalidArgumentException;
24
use Prooph\EventStore\Exception\RuntimeException;
25
use Prooph\EventStore\Internal\Consts;
26
use Prooph\EventStore\UserCredentials;
27
use Psr\Log\LoggerInterface as Logger;
28
use Psr\Log\NullLogger;
29

30
/**
31
 * All times are milliseconds
32
 */
33
class ConnectionSettingsBuilder
34
{
35
    private Logger $log;
36

37
    private bool $verboseLogging = false;
38

39
    private int $maxQueueSize = Consts::DefaultMaxQueueSize;
40

41
    private int $maxConcurrentItems = Consts::DefaultMaxConcurrentItems;
42

43
    private int $maxRetries = Consts::DefaultMaxOperationsRetry;
44

45
    private int $maxReconnections = Consts::DefaultMaxReconnections;
46

47
    private bool $requireMaster = Consts::DefaultRequireMaster;
48

49
    private float $reconnectionDelay = Consts::DefaultReconnectionDelay;
50

51
    private float $operationTimeout = Consts::DefaultOperationTimeout;
52

53
    private float $operationTimeoutCheckPeriod = Consts::DefaultOperationTimeoutPeriod;
54

55
    private ?UserCredentials $defaultUserCredentials = null;
56

57
    private bool $useSslConnection = false;
58

59
    private string $targetHost = '';
60

61
    private bool $validateServer = true;
62

63
    private bool $failOnNoServerResponse = true;
64

65
    private float $heartbeatInterval = 0.75;
66

67
    private float $heartbeatTimeout = 1.5;
68

69
    private float $clientConnectionTimeout = 1;
70

71
    private string $clusterDns = '';
72

73
    private int $maxDiscoverAttempts = Consts::DefaultMaxClusterDiscoverAttempts;
74

75
    private int $gossipExternalHttpPort = Consts::DefaultClusterManagerExternalHttpPort;
76

77
    private float $gossipTimeout = 1;
78

79
    /** @var list<GossipSeed> */
80
    private array $gossipSeeds = [];
81

82
    private bool $preferRandomNode = false;
83

84
    /** @internal */
85
    public function __construct()
86
    {
87
        $this->log = new NullLogger();
433✔
88
    }
89

90
    public function useConsoleLogger(): self
91
    {
92
        if (! \class_exists(StreamHandler::class)) {
×
93
            throw new RuntimeException(\sprintf(
×
94
                '%s not found, install amphp/log ^1.0 via composer to use console logger',
×
95
                StreamHandler::class
×
96
            ));
×
97
        }
98

99
        $logHandler = new StreamHandler(new WritableResourceStream(\STDOUT));
×
100
        $logHandler->setFormatter(new ConsoleFormatter("[%datetime%] %channel%.%level_name%: %message%\r\n"));
×
101

102
        $this->log = new MonoLog('event-store-client');
×
103
        $this->log->pushHandler($logHandler);
×
104

105
        return $this;
×
106
    }
107

108
    public function useFileLogger(File $file): self
109
    {
110
        if (! \class_exists(StreamHandler::class)) {
×
111
            throw new RuntimeException(\sprintf(
×
112
                '%s not found, install amphp/log ^1.0 via composer to use file logger',
×
113
                StreamHandler::class
×
114
            ));
×
115
        }
116

117
        $logHandler = new StreamHandler($file);
×
118
        $logHandler->setFormatter(new LineFormatter());
×
119

120
        $this->log = new MonoLog('event-store-client');
×
121
        $this->log->pushHandler($logHandler);
×
122

123
        return $this;
×
124
    }
125

126
    public function useCustomLogger(Logger $logger): self
127
    {
128
        $this->log = $logger;
×
129

130
        return $this;
×
131
    }
132

133
    public function enableVerboseLogging(): self
134
    {
135
        $this->verboseLogging = true;
×
136

137
        return $this;
×
138
    }
139

140
    public function limitOperationsQueueTo(int $limit): self
141
    {
142
        if ($limit < 0) {
×
143
            throw new InvalidArgumentException('Limit must be positive');
×
144
        }
145

146
        $this->maxQueueSize = $limit;
×
147

148
        return $this;
×
149
    }
150

151
    public function limitConcurrentOperationsTo(int $limit): self
152
    {
153
        if ($limit < 0) {
×
154
            throw new InvalidArgumentException('Limit must be positive');
×
155
        }
156

157
        $this->maxConcurrentItems = $limit;
×
158

159
        return $this;
×
160
    }
161

162
    public function limitAttemptsForOperationTo(int $limit): self
163
    {
164
        if ($limit < 0) {
×
165
            throw new InvalidArgumentException('Limit must be positive');
×
166
        }
167

168
        $this->maxRetries = $limit - 1;
×
169

170
        return $this;
×
171
    }
172

173
    public function limitRetriesForOperationTo(int $limit): self
174
    {
175
        if ($limit < 0) {
×
176
            throw new InvalidArgumentException('Limit must be positive');
×
177
        }
178

179
        $this->maxRetries = $limit;
×
180

181
        return $this;
×
182
    }
183

184
    public function keepRetrying(): self
185
    {
186
        $this->maxRetries = -1;
×
187

188
        return $this;
×
189
    }
190

191
    public function limitReconnectionsTo(int $limit): self
192
    {
193
        if ($limit < 0) {
×
194
            throw new InvalidArgumentException('Limit must be positive');
×
195
        }
196

197
        $this->maxReconnections = $limit;
×
198

199
        return $this;
×
200
    }
201

202
    public function keepReconnecting(): self
203
    {
204
        $this->maxReconnections = -1;
×
205

206
        return $this;
×
207
    }
208

209
    public function performOnMasterOnly(): self
210
    {
211
        $this->requireMaster = true;
×
212

213
        return $this;
×
214
    }
215

216
    public function performOnAnyNode(): self
217
    {
218
        $this->requireMaster = false;
1✔
219

220
        return $this;
1✔
221
    }
222

223
    public function setReconnectionDelayTo(float $reconnectionDelay): self
224
    {
225
        if ($reconnectionDelay < 0) {
×
226
            throw new InvalidArgumentException('Delay must be positive');
×
227
        }
228

229
        $this->reconnectionDelay = $reconnectionDelay;
×
230

231
        return $this;
×
232
    }
233

234
    public function setOperationTimeoutTo(float $operationTimeout): self
235
    {
236
        if ($operationTimeout < 0) {
×
237
            throw new InvalidArgumentException('Timeout must be positive');
×
238
        }
239

240
        $this->operationTimeout = $operationTimeout;
×
241

242
        return $this;
×
243
    }
244

245
    public function setTimeoutCheckPeriodTo(float $timeoutCheckPeriod): self
246
    {
247
        if ($timeoutCheckPeriod < 0) {
×
248
            throw new InvalidArgumentException('Timeout must be positive');
×
249
        }
250

251
        $this->operationTimeoutCheckPeriod = $timeoutCheckPeriod;
×
252

253
        return $this;
×
254
    }
255

256
    public function setDefaultUserCredentials(UserCredentials $userCredentials): self
257
    {
258
        $this->defaultUserCredentials = $userCredentials;
137✔
259

260
        return $this;
137✔
261
    }
262

263
    public function useSslConnection(string $targetHost, bool $validateServer): self
264
    {
265
        if (empty($targetHost)) {
×
266
            throw new InvalidArgumentException('Target host required');
×
267
        }
268

269
        $this->useSslConnection = true;
×
270
        $this->targetHost = $targetHost;
×
271
        $this->validateServer = $validateServer;
×
272

273
        return $this;
×
274
    }
275

276
    public function failOnNoServerResponse(): self
277
    {
278
        $this->failOnNoServerResponse = true;
×
279

280
        return $this;
×
281
    }
282

283
    public function setHeartbeatInterval(float $interval): self
284
    {
285
        if ($interval < 0) {
×
286
            throw new InvalidArgumentException('Interval must be positive');
×
287
        }
288

289
        $this->heartbeatInterval = $interval;
×
290

291
        return $this;
×
292
    }
293

294
    public function setHeartbeatTimeout(float $timeout): self
295
    {
296
        if ($timeout < 0) {
2✔
297
            throw new InvalidArgumentException('Timeout must be positive');
×
298
        }
299

300
        $this->heartbeatTimeout = $timeout;
2✔
301

302
        return $this;
2✔
303
    }
304

305
    public function withConnectionTimeoutOf(float $timeout): self
306
    {
307
        if ($timeout < 0) {
×
308
            throw new InvalidArgumentException('Timeout must be positive');
×
309
        }
310

311
        $this->clientConnectionTimeout = $timeout;
×
312

313
        return $this;
×
314
    }
315

316
    public function setClusterDns(string $clusterDns): self
317
    {
318
        if (empty($clusterDns)) {
×
319
            throw new InvalidArgumentException('Cluster DNS required');
×
320
        }
321

322
        $this->clusterDns = $clusterDns;
×
323

324
        return $this;
×
325
    }
326

327
    public function setMaxDiscoverAttempts(int $maxDiscoverAttempts): self
328
    {
329
        if ($maxDiscoverAttempts < 1) {
1✔
330
            throw new InvalidArgumentException(\sprintf(
×
331
                'Max discover attempts is out of range %d. Allowed range: [1, PHP_INT_MAX].',
×
332
                $maxDiscoverAttempts
×
333
            ));
×
334
        }
335

336
        $this->maxDiscoverAttempts = $maxDiscoverAttempts;
1✔
337

338
        return $this;
1✔
339
    }
340

341
    public function setGossipTimeout(float $timeout): self
342
    {
343
        if ($timeout < 0) {
1✔
344
            throw new InvalidArgumentException('Timeout must be positive');
×
345
        }
346

347
        $this->gossipTimeout = $timeout;
1✔
348

349
        return $this;
1✔
350
    }
351

352
    public function preferRandomNode(): self
353
    {
354
        $this->preferRandomNode = true;
1✔
355

356
        return $this;
1✔
357
    }
358

359
    public function setClusterGossipPort(int $clusterGossipPort): self
360
    {
361
        if ($clusterGossipPort < 1) {
×
362
            throw new InvalidArgumentException('Invalid port given');
×
363
        }
364

365
        $this->gossipExternalHttpPort = $clusterGossipPort;
×
366

367
        return $this;
×
368
    }
369

370
    /** @param list<EndPoint> $gossipSeeds */
371
    public function setGossipSeedEndPoints(array $gossipSeeds, bool $seedOverTls = true): self
372
    {
373
        if (empty($gossipSeeds)) {
×
374
            throw new InvalidArgumentException('Empty FakeDnsEntries collection');
×
375
        }
376

377
        foreach ($gossipSeeds as $seed) {
×
378
            $this->gossipSeeds[] = new GossipSeed($seed, '', $seedOverTls);
×
379
        }
380

381
        return $this;
×
382
    }
383

384
    /** @param list<GossipSeed> $gossipSeeds */
385
    public function setGossipSeeds(array $gossipSeeds): self
386
    {
387
        if (empty($gossipSeeds)) {
3✔
388
            throw new InvalidArgumentException('Empty FakeDnsEntries collection');
×
389
        }
390

391
        $this->gossipSeeds = $gossipSeeds;
3✔
392

393
        return $this;
3✔
394
    }
395

396
    public function build(): ConnectionSettings
397
    {
398
        return new ConnectionSettings(
433✔
399
            $this->log,
433✔
400
            $this->verboseLogging,
433✔
401
            $this->maxQueueSize,
433✔
402
            $this->maxConcurrentItems,
433✔
403
            $this->maxRetries,
433✔
404
            $this->maxReconnections,
433✔
405
            $this->requireMaster,
433✔
406
            $this->reconnectionDelay,
433✔
407
            $this->operationTimeout,
433✔
408
            $this->operationTimeoutCheckPeriod,
433✔
409
            $this->defaultUserCredentials,
433✔
410
            $this->useSslConnection,
433✔
411
            $this->targetHost,
433✔
412
            $this->validateServer,
433✔
413
            $this->failOnNoServerResponse,
433✔
414
            $this->heartbeatInterval,
433✔
415
            $this->heartbeatTimeout,
433✔
416
            $this->clusterDns,
433✔
417
            $this->maxDiscoverAttempts,
433✔
418
            $this->gossipExternalHttpPort,
433✔
419
            $this->gossipSeeds,
433✔
420
            $this->gossipTimeout,
433✔
421
            $this->preferRandomNode,
433✔
422
            $this->clientConnectionTimeout
433✔
423
        );
433✔
424
    }
425
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc