diff --git a/src/Client.php b/src/Client.php index 791258b..8193927 100644 --- a/src/Client.php +++ b/src/Client.php @@ -9,9 +9,11 @@ use Clue\Redis\Protocol\Model\ErrorReplyException; use Clue\Redis\Protocol\Serializer\SerializerInterface; use Clue\Redis\Protocol\Factory as ProtocolFactory; -use Clue\React\Redis\Request; use UnderflowException; use RuntimeException; +use React\Promise\Deferred; +use Clue\Redis\Protocol\Model\ErrorReply; +use Clue\Redis\Protocol\Model\ModelInterface; class Client extends EventEmitter { @@ -46,7 +48,7 @@ public function __construct(Stream $stream, ParserInterface $parser = null, Seri foreach ($models as $data) { try { - $that->handleReply($data); + $that->handleMessage($data); } catch (UnderflowException $error) { $that->emit('error', array($error)); @@ -67,36 +69,34 @@ public function __construct(Stream $stream, ParserInterface $parser = null, Seri public function __call($name, $args) { - if ($this->ending) { - $e = new RuntimeException('Connection closed'); + $request = new Deferred(); - if (class_exists('React\Promise\When')) { - return \React\Promise\When::reject($e); - } else { - return \React\Promise\reject($e); - } + if ($this->ending) { + $request->reject(new RuntimeException('Connection closed')); + } else { + $this->stream->write($this->serializer->getRequestMessage($name, $args)); + $this->requests []= $request; } - $this->stream->write($this->serializer->getRequestMessage($name, $args)); - - $request = new Request($name); - $this->requests []= $request; - return $request->promise(); } - public function handleReply($data) + public function handleMessage(ModelInterface $message) { - $this->emit('message', array($data, $this)); + $this->emit('message', array($message, $this)); if (!$this->requests) { throw new UnderflowException('Unexpected reply received, no matching request found'); } $request = array_shift($this->requests); - /* @var $request Request */ + /* @var $request Deferred */ - $request->handleReply($data); + if ($message instanceof ErrorReply) { + $request->reject($message); + } else { + $request->resolve($message->getValueNative()); + } if ($this->ending && !$this->isBusy()) { $this->close(); diff --git a/src/Request.php b/src/Request.php deleted file mode 100644 index 38b3f78..0000000 --- a/src/Request.php +++ /dev/null @@ -1,19 +0,0 @@ -reject($data); - } else { - $this->resolve($data->getValueNative()); - } - } -} diff --git a/tests/ClientTest.php b/tests/FunctionalTest.php similarity index 99% rename from tests/ClientTest.php rename to tests/FunctionalTest.php index cc5ebf9..aa71767 100644 --- a/tests/ClientTest.php +++ b/tests/FunctionalTest.php @@ -8,7 +8,7 @@ use Clue\React\Redis\Client; -class ClientTest extends TestCase +class FunctionalTest extends TestCase { protected static $loop; protected static $factory;