From a348e32144e46a579e99befe6b1f8310524cc556 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Mon, 11 Apr 2011 18:53:02 +0200 Subject: [PATCH] Improve Predis\Client::getConnection() internals. --- lib/Predis/Client.php | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/Predis/Client.php b/lib/Predis/Client.php index a2b9f520..438321f2 100644 --- a/lib/Predis/Client.php +++ b/lib/Predis/Client.php @@ -95,14 +95,7 @@ class Client { } public function getClientFor($connectionAlias) { - if (!Helpers::isCluster($this->_connection)) { - throw new ClientException( - 'This method is supported only when the client is connected to a cluster of connections' - ); - } - - $connection = $this->_connection->getConnectionById($connectionAlias); - if ($connection === null) { + if (($connection = $this->getConnection($connectionAlias)) === null) { throw new \InvalidArgumentException( "Invalid connection alias: '$connectionAlias'" ); @@ -127,12 +120,16 @@ class Client { } public function getConnection($id = null) { - if ($id === null) { - return $this->_connection; + if (isset($id)) { + if (!Helpers::isCluster($this->_connection)) { + throw new ClientException( + 'Retrieving connections by alias is supported '. + 'only with clustered connections' + ); + } + return $this->_connection->getConnectionById($id); } - $connection = $this->_connection; - $isCluster = Helpers::isCluster($connection); - return $isCluster ? $connection->getConnectionById($id) : $connection; + return $this->_connection; } public function __call($method, $arguments) {