Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix/remove is connected method from sc #61

Merged
merged 3 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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