Skip to content

Commit

Permalink
Merge pull request #61 from timirey/hotfix/remove-is-connected-method…
Browse files Browse the repository at this point in the history
…-from-sc

Hotfix/remove is connected method from sc
  • Loading branch information
timirey authored Jul 30, 2024
2 parents ce904ec + 1da8283 commit 3953098
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## [6.0.3](https://github.com/timirey/xapi-php/compare/6.0.2..6.0.3) - 2024-07-30

- Remove `isConnected()` from `SocketConnection::class`.

## [6.0.2](https://github.com/timirey/xapi-php/compare/6.0.1..6.0.2) - 2024-07-30

- Catch exceptions from `stream_socket_client()`.
Expand Down
32 changes: 11 additions & 21 deletions src/Connections/SocketConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public function __destruct()
* Opens the socket connection.
*
* @return boolean True on success, false on failure.
* @throws SocketException If socket is not initialized.
* @throws SocketException If socket is not created.
*/
public function open(): bool
{
$this->socket = @stream_socket_client($this->address, $errorCode, $errorMessage);
$this->socket = stream_socket_client($this->address, $errorCode, $errorMessage);

if (!$this->isConnected()) {
if ($this->socket === false) {
throw new SocketException("$errorCode: $errorMessage");
}

Expand All @@ -65,11 +65,11 @@ public function open(): bool
* @param string $payload The data to send.
*
* @return false|integer The number of bytes written, or false on failure.
* @throws SocketException If socket is not initialized.
* @throws SocketException If socket is not accepting message.
*/
public function send(string $payload): false|int
{
if (!$this->isConnected()) {
if ($this->socket === false) {
throw new SocketException('The socket is not accepting messages.');
}

Expand All @@ -80,11 +80,11 @@ public function send(string $payload): false|int
* Receives data from the socket until the delimiter "\n\n" is encountered.
*
* @return string The read data, or false on failure.
* @throws SocketException If socket is not initialized.
* @throws SocketException If socket is not sending messages.
*/
public function receive(): string
{
if (!$this->isConnected()) {
if ($this->socket === false) {
throw new SocketException('The socket is not sending messages.');
}

Expand All @@ -107,11 +107,11 @@ public function receive(): string
* Listen to the stream socket and yield data as it is received.
*
* @return Generator Yields data received from the socket.
* @throws SocketException If socket is empty or not initialized.
* @throws SocketException If socket is not able to subscribe the client.
*/
public function listen(): Generator
{
if (!$this->isConnected()) {
if ($this->socket === false) {
throw new SocketException('The socket is not subscribable.');
}

Expand All @@ -130,24 +130,14 @@ public function listen(): Generator
* Closes the socket connection.
*
* @return boolean True on success, false on failure.
* @throws SocketException If socket is not initialized.
* @throws SocketException If socket is already closed.
*/
public function close(): bool
{
if (!$this->isConnected()) {
if ($this->socket === false) {
throw new SocketException('The socket is already closed.');
}

return fclose($this->socket);
}

/**
* Checks if the socket is connected and running.
*
* @return boolean True if the socket is connected, false otherwise.
*/
public function isConnected(): bool
{
return is_resource($this->socket) && !feof($this->socket);
}
}
8 changes: 0 additions & 8 deletions tests/Commands/Traits/ClientMockeryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ public function setStream(SocketConnection $stream): void
*/
public function mockResponse(AbstractPayload $payload, array $response): void
{
$this->request->shouldReceive('isConnected')
->once()
->andReturn(true);

$this->request->shouldReceive('send')
->once()
->with($payload->toJson());
Expand All @@ -124,10 +120,6 @@ public function mockResponse(AbstractPayload $payload, array $response): void
*/
public function mockStreamResponse(AbstractStreamPayload $payload, array $response): void
{
$this->request->shouldReceive('isConnected')
->once()
->andReturn(true);

$this->stream->shouldReceive('send')
->once()
->with($payload->toJson());
Expand Down

0 comments on commit 3953098

Please sign in to comment.