Skip to content

Commit

Permalink
type hints and close the resource after socket shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
ppshobi committed Nov 13, 2022
1 parent 1a74c92 commit ed164e6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
37 changes: 24 additions & 13 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
use Psonic\Contracts\Client as ClientInterface;
use Psonic\Contracts\Command as CommandInterface;
use Psonic\Contracts\Response as ResponseInterface;
use TypeError;

class Client implements ClientInterface
{
/** @var resource $resource */
private $resource;

/** @var string */
private $host;
/** @var int */
private $port;
/** @var int|null */
private $errorNo;
/** @var string */
private $errorMessage;
/** @var int */
private $maxTimeout;

/**
Expand All @@ -39,11 +46,12 @@ public function __construct($host = 'localhost', $port = 1491, $timeout = 30)
*/
public function send(CommandInterface $command): ResponseInterface
{
if (!$this->resource) {
throw new ConnectionException();
try {
fwrite($this->resource, $command);
}catch (TypeError $e) {
throw new ConnectionException("Not connected to sonic. " . $e->getMessage());
}

fwrite($this->resource, $command);
return $this->read();
}

Expand All @@ -53,10 +61,6 @@ public function send(CommandInterface $command): ResponseInterface
*/
public function read(): ResponseInterface
{
if (!$this->resource) {
throw new ConnectionException();
}

$string = fgets($this->resource);

if($string === false) {
Expand All @@ -76,20 +80,27 @@ public function read(): ResponseInterface
* @throws ConnectionException
* connects to the socket
*/
public function connect():void
public function connect(): void
{
if (!$this->resource = stream_socket_client("tcp://{$this->host}:{$this->port}", $this->errorNo, $this->errorMessage, $this->maxTimeout)) {
throw new ConnectionException();
$resource = stream_socket_client("tcp://{$this->host}:{$this->port}", $this->errorNo, $this->errorMessage, $this->maxTimeout);
if (!$resource) {
throw new ConnectionException("Unable to connect to sonic search engine with given host: $this->host and port: $this->port}. Error code $this->errorNo with $this->errorMessage was produced");
}
$this->resource = $resource;
}

/**
* Disconnects from a socket
*/
public function disconnect()
public function disconnect(): void
{
stream_socket_shutdown($this->resource, STREAM_SHUT_WR);
$this->resource = null;
$result = stream_socket_shutdown($this->resource, STREAM_SHUT_WR);

if(!$result) {
throw new \RuntimeException("Unable to shut down stream socket connection");
}

fclose($this->resource);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/Unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

use Psonic\Client;
use Psonic\Commands\Misc\PingCommand;
use Psonic\Commands\Search\QueryCommand;
use Psonic\Contracts\Command;
use Psonic\Contracts\Response;
use Psonic\Exceptions\ConnectionException;
use Psonic\Search;
use Tests\TestCase;

class ClientTest extends TestCase
Expand Down Expand Up @@ -38,6 +40,7 @@ public function the_client_can_disconnect_from_socket_and_throws_exception_if_co
$this->client->connect();
$this->client->disconnect();
$this->expectException(ConnectionException::class);
$this->expectExceptionMessage("Not connected to sonic. fwrite(): supplied resource is not a valid stream resource");
$this->client->send(new PingCommand);
}

Expand Down Expand Up @@ -76,4 +79,4 @@ public function the_client_can_send_a_command_to_sonic_and_returns_a_response_ob
$this->assertInstanceOf(Response::class, $this->client->send(new PingCommand));
}

}
}

0 comments on commit ed164e6

Please sign in to comment.