Skip to content

Commit

Permalink
refactor configuration options
Browse files Browse the repository at this point in the history
  • Loading branch information
nekufa committed Dec 24, 2024
1 parent c3de3f1 commit 28bc413
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 98 deletions.
56 changes: 17 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,14 @@ The NKeys functionality requires Ed25519, which is provided in `libsodium` exten
use Basis\Nats\Client;
use Basis\Nats\Configuration;

// this is default options, you can override anyone
$configuration = new Configuration([
'host' => 'localhost',
'jwt' => null,
'lang' => 'php',
'pass' => null,
'pedantic' => false,
'port' => 4222,
'reconnect' => true,
'timeout' => 1,
'token' => null,
'user' => null,
'nkey' => null,
'verbose' => false,
'version' => 'dev',
]);
// you can override any default configuraiton key using constructor
$configuration = new Configuration(
host: 'nats-host',
user: 'basis',
pass: 'secret',
);

// delaya configuration options are changed via setters

// default delay mode is constant - first retry be in 1ms, second in 1ms, third in 1ms
$configuration->setDelay(0.001);
Expand Down Expand Up @@ -79,25 +71,13 @@ Connection settings when connecting to a nats server that has TLS and TLS Client
use Basis\Nats\Client;
use Basis\Nats\Configuration;

// this is default options, you can override anyone
$configuration = new Configuration([
'host' => 'localhost',
'jwt' => null,
'lang' => 'php',
'pass' => null,
'pedantic' => false,
'port' => 4222,
'reconnect' => true,
'timeout' => 1,
'token' => null,
'user' => null,
'nkey' => null,
'verbose' => false,
'version' => 'dev',
'tlsCertFile' => "./certs/client-cert.pem",
'tlsKeyFile' => "./certs/client-key.pem",
'tlsCaFile' => "./certs/client-key.pem",
]);
// you can override any default configuraiton key using constructor
$configuration = new Configuration(
host: 'tls-service-endpoint',
tlsCertFile: "./certs/client-cert.pem",
tlsKeyFile': "./certs/client-key.pem",
tlsCaFile': "./certs/client-key.pem",
);

$configuration->setDelay(0.001);

Expand Down Expand Up @@ -392,11 +372,9 @@ use Basis\Nats\Configuration;
use Basis\Nats\NKeys\CredentialsParser;

$configuration = new Configuration(
[
'host' => 'localhost',
'port' => 4222
],
CredentialsParser::fromFile($credentialPath)
host: 'localhost',
port: 4222,
);

$client = new Client($configuration);
Expand Down
90 changes: 39 additions & 51 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,55 @@

class Configuration
{
public readonly bool $pedantic;
public readonly bool $reconnect;
public readonly bool $verbose;
public readonly int $port;
public readonly string $host;
public readonly string $lang;
public readonly string $version;
public readonly float $timeout;
public readonly int $pingInterval;

public readonly string $inboxPrefix;

public readonly ?string $jwt;
public readonly ?string $pass;
public readonly ?string $token;
public readonly ?string $user;
public readonly ?string $nkey;

public readonly ?string $tlsKeyFile;
public readonly ?string $tlsCertFile;
public readonly ?string $tlsCaFile;

public const DELAY_CONSTANT = 'constant';
public const DELAY_LINEAR = 'linear';
public const DELAY_EXPONENTIAL = 'exponential';

protected float $delay = 0.001;
protected string $delayMode = self::DELAY_CONSTANT;

protected array $defaults = [
'host' => 'localhost',
'jwt' => null,
'lang' => 'php',
'pass' => null,
'pedantic' => false,
'port' => 4222,
'reconnect' => true,
'timeout' => 1,
'token' => null,
'user' => null,
'nkey' => null,
'verbose' => false,
'version' => 'dev',
'pingInterval' => 2,
'inboxPrefix' => '_INBOX',
'tlsKeyFile' => null,
'tlsCertFile' => null,
'tlsCaFile' => null,
];
protected float $delay;
protected string $delayMode;

/**
* @param array<string, string|int|bool|null> ...$options
* @param array<string, string|int|bool|null> $options
*/
public function __construct(array ...$options)
{
$config = array_merge($this->defaults, ...$options);
foreach ($config as $k => $v) {
public function __construct(
array $options = [], // deprecated
array $options2 = [], // deprecated multi option array support
array $options3 = [], // deprecated multi option array support
public string $host = 'localhost',
public int $port = 4222,
public ?string $user = null,
public ?string $jwt = null,
public ?string $pass = null,
public ?string $token = null,
public ?string $nkey = null,
public ?string $tlsKeyFile = null,
public ?string $tlsCertFile = null,
public ?string $tlsCaFile = null,
public bool $pedantic = false,
public bool $reconnect = true,
public bool $verbose = false,
public float $timeout = 1,
public int $pingInterval = 2,
float $delay = 0.001,
string $delayMode = self::DELAY_CONSTANT,
public string $lang = 'php',
public string $version = 'dev',
public string $inboxPrefix = '_INBOX',
) {

$this->setDelay($delay, $delayMode);

foreach (array_merge($options, $options2, $options3) as $k => $v) {
if (!property_exists($this, $k)) {
throw new InvalidArgumentException("Invalid config option $k");
}
$this->$k = $v;
if ($k == 'delayMode') {
$this->setDelay($this->delay, $v);
} elseif ($k == 'delay') {
$this->setDelay($k, $this->delayMode);
} else {
$this->$k = $v;
}
}
}

Expand Down
17 changes: 9 additions & 8 deletions tests/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ public function getClient(): Client

public function getConfiguration(array ...$options): Configuration
{
return new Configuration([
'host' => getenv('NATS_HOST'),
'port' => +getenv('NATS_PORT'),
'delay' => 0.05,
'delayMode' => Configuration::DELAY_LINEAR,
'timeout' => 0.5,
'verbose' => getenv('NATS_CLIENT_VERBOSE') == '1',
], ...$options);
return new Configuration(
array_merge(...$options),
host: getenv('NATS_HOST'),
port: +getenv('NATS_PORT'),
timeout: 0.5,
verbose: getenv('NATS_CLIENT_VERBOSE') == '1',
delay: 0.05,
delayMode: Configuration::DELAY_LINEAR,
);
}

public function setup(): void
Expand Down

0 comments on commit 28bc413

Please sign in to comment.