Skip to content

Commit

Permalink
Replace ClientStateEnum with a native Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed May 23, 2024
1 parent eabea9d commit f827bab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
25 changes: 12 additions & 13 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ class Client implements ClientInterface
private readonly array $options;

private readonly Connector $connector;

/** @var int */
private int $state = ClientStateEnum::NOT_CONNECTED;

private ClientState $state = ClientState::NotConnected;

private ?Connection $connection = null;

Expand Down Expand Up @@ -134,7 +133,7 @@ public function __construct(array $options = [])
$this->connector = new Connector($this->options);


$this->state = ClientStateEnum::NOT_CONNECTED;
$this->state = ClientState::NotConnected;
$this->channels = new Channels();
}

Expand Down Expand Up @@ -163,7 +162,7 @@ public function channel(): ChannelInterface
return $channel;
}

$this->state = ClientStateEnum::ERROR;
$this->state = ClientState::Error;

throw new ClientException(
'channel.open unexpected response of type ' . gettype($response) . '.'
Expand All @@ -177,11 +176,11 @@ public function channel(): ChannelInterface
*/
public function connect(): self
{
if ($this->state !== ClientStateEnum::NOT_CONNECTED) {
if ($this->state !== ClientState::NotConnected) {
throw new ClientException('Client already connected/connecting.');
}

$this->state = ClientStateEnum::CONNECTING;
$this->state = ClientState::Connecting;

$streamScheme = 'tcp';
if (isset($this->options['tls']) && is_array($this->options['tls'])) {
Expand Down Expand Up @@ -213,7 +212,7 @@ public function connect(): self
$this->connection->connectionOpen($this->options['vhost']);
$this->connection->startHeathbeatTimer();

$this->state = ClientStateEnum::CONNECTED;
$this->state = ClientState::Connected;
} catch (\Throwable $thrown) {
throw new ClientException('Could not connect to ' . $uri . ': ' . $thrown->getMessage(), $thrown->getCode(), $thrown);
}
Expand Down Expand Up @@ -247,15 +246,15 @@ protected function authResponse(MethodConnectionStartFrame $start): void
*/
public function disconnect(int $replyCode = 0, string $replyText = ''): void
{
if ($this->state === ClientStateEnum::DISCONNECTING) {
if ($this->state === ClientState::Disconnecting) {
return;
}

if ($this->state !== ClientStateEnum::CONNECTED) {
if ($this->state !== ClientState::Connected) {
throw new ClientException('Client is not connected.');
}

$this->state = ClientStateEnum::DISCONNECTING;
$this->state = ClientState::Disconnecting;

$promises = [];
foreach ($this->channels->all() as $channelId => $channel) {
Expand All @@ -267,15 +266,15 @@ public function disconnect(int $replyCode = 0, string $replyText = ''): void

$this->connection->disconnect($replyCode, $replyText);

$this->state = ClientStateEnum::NOT_CONNECTED;
$this->state = ClientState::NotConnected;
}

/**
* Returns true if client is connected to server.
*/
public function isConnected(): bool
{
return $this->state !== ClientStateEnum::NOT_CONNECTED && $this->state !== ClientStateEnum::ERROR;
return $this->state !== ClientState::NotConnected && $this->state !== ClientState::Error;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/ClientStateEnum.php → src/ClientState.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,32 @@
*
* @author Jakub Kulhan <[email protected]>
*/
final class ClientStateEnum
enum ClientState
{

/**
* Client is not connected. Method connect() hasn't been called yet.
*/
const NOT_CONNECTED = 0;
case NotConnected;

/**
* Client is currently connecting to AMQP server.
*/
const CONNECTING = 1;
case Connecting;

/**
* Client is connected and ready to communicate.
*/
const CONNECTED = 2;
case Connected;

/**
* Client is currently disconnecting from AMQP server.
*/
const DISCONNECTING = 3;
case Disconnecting;

/**
* An error has occurred.
*/
const ERROR = 4;
case Error;

}

0 comments on commit f827bab

Please sign in to comment.