Skip to content

Commit

Permalink
chore!: bump php version to 8.1 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
EJTJ3 authored Oct 21, 2022
1 parent ac0c464 commit 994ba2a
Show file tree
Hide file tree
Showing 17 changed files with 260 additions and 296 deletions.
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
}
],
"require": {
"php" : "^7.4 | ^8.0",
"php" : "^8.1",
"nyholm/dsn": "^2.0",
"psr/log": "^1.1",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
},
"suggest": {
"clue/socket-raw": "Allows using the new socket client"
},
"scripts": {
"test": "vendor/bin/phpunit"
}
Expand Down
111 changes: 51 additions & 60 deletions src/Connection/ClientConnectionOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,57 @@
*/
final class ClientConnectionOptions
{
/**
* Turns on +OK protocol acknowledgements.
*/
private bool $verbose;

/**
* Turns on additional strict format checking, e.g. for properly formed subjects.
*/
private bool $pedantic;

/**
* Indicates whether the client requires an SSL connection.
*/
private bool $tlsRequired;

/**
* Client authorization token (if auth_required is set).
*/
private ?string $authToken;

/**
* Connection username (if auth_required is set).
*/
private ?string $user;

/**
* Connection password (if auth_required is set).
*/
private ?string $password;

/**
* Optional client name.
*/
private ?string $name;

/**
* Sending 0 (or absent) indicates client supports original protocol.
* Sending 1 indicates that the client supports dynamic reconfiguration of
* cluster topology changes by asynchronously receiving INFO messages with known servers it can reconnect to.
*/
private ?int $protocol;

/**
* If set to true, the server (version 1.2.0+) will not send originating messages from this
* connection to its own subscriptions. Clients should set this to true only for server supporting
* this feature, which is when proto in the INFO protocol is set to at least 1.
*/
private bool $echo;

public function __construct()
public function __construct(
/**
* Turns on +OK protocol acknowledgements.
*/
private bool $verbose = true,

/**
* Turns on additional strict format checking, e.g. for properly formed subjects.
*/
private bool $pedantic = true,

/**
* Indicates whether the client requires an SSL connection.
*/
private bool $tlsRequired = false,

/**
* Client authorization token (if auth_required is set).
*/
private ?string $authToken = null,

/**
* Connection username (if auth_required is set).
*/
private ?string $user = null,

/**
* Connection password (if auth_required is set).
*/
private ?string $password = null,

/**
* Optional client name.
*/
private ?string $name = null,

/**
* Sending 0 (or absent) indicates client supports original protocol.
* Sending 1 indicates that the client supports dynamic reconfiguration of
* cluster topology changes by asynchronously receiving INFO messages with known servers it can reconnect to.
*/
private ?int $protocol = 0,

/**
* If set to true, the server (version 1.2.0+) will not send originating messages from this
* connection to its own subscriptions. Clients should set this to true only for server supporting
* this feature, which is when proto in the INFO protocol is set to at least 1.
*/
private bool $echo = false,
)
{
$this->verbose = true;
$this->pedantic = true;
$this->tlsRequired = false;
$this->authToken = null;
$this->user = null;
$this->password = null;
$this->name = null;
$this->protocol = 0;
$this->echo = false;
}

public function isVerbose(): bool
Expand Down Expand Up @@ -164,7 +155,7 @@ public function setEcho(bool $echo): void
}

/**
* @return array<string, bool|int|string|null>
* @return array<string, mixed>
*/
public function toArray(): array
{
Expand Down
63 changes: 32 additions & 31 deletions src/Connection/NatsConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use EJTJ3\PhpNats\Encoder\EncoderInterface;
use EJTJ3\PhpNats\Encoder\JsonEncoder;
use EJTJ3\PhpNats\Exception\NatsConnectionRefusedException;
use EJTJ3\PhpNats\Exception\NatsInvalidOperationException;
use EJTJ3\PhpNats\Exception\NatsInvalidResponseException;
use EJTJ3\PhpNats\Exception\TransportAlreadyConnectedException;
use EJTJ3\PhpNats\Logger\NullLogger;
Expand All @@ -18,6 +17,7 @@
use EJTJ3\PhpNats\Transport\Stream\StreamTransport;
use EJTJ3\PhpNats\Transport\TranssportOption;
use EJTJ3\PhpNats\Util\StringUtil;
use Exception;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;

Expand All @@ -42,22 +42,10 @@ final class NatsConnection implements LoggerAwareInterface

public function __construct(
NatsConnectionOptionInterface $connectionOptions,
NatsTransportInterface $transport = null,
EncoderInterface $encoder = null,
LoggerInterface $logger = null
NatsTransportInterface $transport = new StreamTransport(),
EncoderInterface $encoder = new JsonEncoder(),
LoggerInterface $logger = new NullLogger()
) {
if ($encoder === null) {
$encoder = new JsonEncoder();
}

if ($logger === null) {
$logger = new NullLogger();
}

if ($transport === null) {
$transport = new StreamTransport();
}

$this->transport = $transport;
$this->encoder = $encoder;
$this->connectionOptions = $connectionOptions;
Expand All @@ -67,6 +55,9 @@ public function __construct(
$this->connected = false;
}

/**
* @throws Exception
*/
public function connect(): void
{
if ($this->transport->isConnected()) {
Expand All @@ -82,7 +73,7 @@ public function connect(): void

try {
$this->transport->connect($transportOption);
} catch (\Exception $e) {
} catch (Exception $e) {
$this->logger->error($e->getMessage());

continue;
Expand Down Expand Up @@ -125,6 +116,9 @@ public function close(): void
$this->transport->close();
}

/**
* @throws Exception
*/
public function publish(string $subject, string $payload): void
{
if ($this->isConnected() === false) {
Expand All @@ -133,26 +127,32 @@ public function publish(string $subject, string $payload): void

$message = sprintf('%s %s', $subject, strlen($payload));

$this->doWrite(NatsProtocolOperation::PUB, $message . Nats::CR_LF . $payload);
$this->doWrite(NatsProtocolOperation::Pub, $message . Nats::CR_LF . $payload);
}

private function isErrorResponse(string $response): bool
{
return substr($response, 0, 4) === NatsProtocolOperation::ERR;
return NatsProtocolOperation::Err->isOperation(substr($response, 0, 4));
}

/**
* @throws Exception
*/
public function ping(): void
{
$this->doWrite(NatsProtocolOperation::PING);
$this->doWrite(NatsProtocolOperation::Ping, "ping");
}

/**
* @throws Exception
*/
public function validatePing(): void
{
$this->ping();

$pingResponse = $this->getResponse();

if ($pingResponse !== NatsProtocolOperation::PONG) {
if (!NatsProtocolOperation::Pong->isOperation($pingResponse)) {
throw new NatsInvalidResponseException('Did not receive a pong from the server');
}
}
Expand All @@ -162,6 +162,9 @@ public function getServerInfo(): ?ServerInfo
return $this->serverInfo;
}

/**
* @throws Exception
*/
private function doConnect(): void
{
if ($this->currentServer === null) {
Expand All @@ -179,12 +182,12 @@ private function doConnect(): void
$connectionOptions->setPassword($server->getPassword());
}

$this->doWrite(NatsProtocolOperation::CONNECT, $connectionOptions->toArray());
$this->doWrite(NatsProtocolOperation::Connect, $connectionOptions->toArray());

if ($connectionOptions->isVerbose() === true) {
$connectResponse = $this->getResponse();

if ($connectResponse !== NatsProtocolOperation::ACK) {
if (NatsProtocolOperation::Ack->isOperation($connectResponse) === false) {
throw new NatsInvalidResponseException('Nats did not send a normal response');
}
}
Expand All @@ -196,13 +199,13 @@ private function createServerInfo(): ServerInfo

[$operation, $data] = explode(' ', $rawData);

if ($operation !== NatsProtocolOperation::INFO) {
if (!NatsProtocolOperation::Info->isOperation($operation)) {
throw new NatsInvalidResponseException('Server information is not correct');
}

$data = $this->encoder->decode($data);

$serverInfo = new ServerInfo($data);
$serverInfo = ServerInfo::fromData($data);

$this->serverInfo = $serverInfo;

Expand Down Expand Up @@ -230,18 +233,16 @@ private function getResponse(): string

/**
* @param array<string, mixed>|string|null $payload
*
* @throws Exception
*/
private function doWrite(string $operation, $payload = null): void
private function doWrite(NatsProtocolOperation $operation, array|string $payload): void
{
if (!in_array($operation, NatsProtocolOperation::AVAILABLE_OPERATIONS, true)) {
throw NatsInvalidOperationException::withOperation($operation);
}

if (!is_string($payload)) {
$payload = $this->encoder->encode($payload);
}

$payload = sprintf('%s %s%s', $operation, $payload, Nats::CR_LF);
$payload = sprintf('%s %s%s', $operation->value, $payload, Nats::CR_LF);

$this->transport->write($payload);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Connection/NatsConnectionOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class NatsConnectionOption implements NatsConnectionOptionInterface
* @param array<int, string|Server>|Server|string|ServerCollection $servers
*/
public function __construct(
$servers = [],
array|Server|string|ServerCollection $servers = [],
?string $name = null,
int $timeout = 5,
bool $randomize = false
Expand Down
2 changes: 1 addition & 1 deletion src/Connection/ServerCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class ServerCollection
private array $servers;

/**
* @param array<Server> $servers
* @param Server[] $servers
*/
public function __construct(array $servers, bool $randomize = false)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Constant/Nats.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
/**
* @author Evert Jan Hakvoort <[email protected]>
*/
final class Nats
abstract class Nats
{
public const DEFAULT_PORT = 4222;
final public const DEFAULT_PORT = 4222;

public const CR_LF = "\r\n";
final public const CR_LF = "\r\n";

public const LANG = 'php';
final public const LANG = 'php';

public const VERSION = '0.0.1';
final public const VERSION = '0.0.1';
}
Loading

0 comments on commit 994ba2a

Please sign in to comment.