Przeglądaj źródła

Implement separate Predis\Client::getConnectionById().

This method is not part of Predis\ClientInterface because it is being
considered mostly an helper / utility method.
Daniele Alessandri 12 lat temu
rodzic
commit
a6e1ac11d0
3 zmienionych plików z 25 dodań i 20 usunięć
  1. 17 10
      lib/Predis/Client.php
  2. 2 4
      lib/Predis/ClientInterface.php
  3. 6 6
      tests/Predis/ClientTest.php

+ 17 - 10
lib/Predis/Client.php

@@ -136,10 +136,10 @@ class Client implements ClientInterface
      *
      * @return Client
      */
-    public function getClientFor($connectionAlias)
+    public function getClientFor($connectionID)
     {
-        if (($connection = $this->getConnection($connectionAlias)) === null) {
-            throw new \InvalidArgumentException("Invalid connection alias: '$connectionAlias'");
+        if (($connection = $this->getConnectionById($connectionID)) === null) {
+            throw new \InvalidArgumentException("Invalid connection ID: '$connectionID'");
         }
 
         return new Client($connection, $this->options);
@@ -185,17 +185,24 @@ class Client implements ClientInterface
     /**
      * {@inheritdoc}
      */
-    public function getConnection($id = null)
+    public function getConnection()
     {
-        if (isset($id)) {
-            if (!$this->connection instanceof AggregatedConnectionInterface) {
-                throw new NotSupportedException('Retrieving connections by alias is supported only with aggregated connections');
-            }
+        return $this->connection;
+    }
 
-            return $this->connection->getConnectionById($id);
+    /**
+     * Retrieves a single connection out of an aggregated connections instance.
+     *
+     * @param string $connectionId Index or alias of the connection.
+     * @return SingleConnectionInterface
+     */
+    public function getConnectionById($connectionId)
+    {
+        if (!$this->connection instanceof AggregatedConnectionInterface) {
+            throw new NotSupportedException('Retrieving connections by ID is supported only when using aggregated connections');
         }
 
-        return $this->connection;
+        return $this->connection->getConnectionById($connectionId);
     }
 
     /**

+ 2 - 4
lib/Predis/ClientInterface.php

@@ -49,13 +49,11 @@ interface ClientInterface extends BasicClientInterface
     public function disconnect();
 
     /**
-     * Returns the underlying connection instance or, when connected to a cluster,
-     * one of the connection instances identified by its alias.
+     * Returns the underlying connection instance.
      *
-     * @param string $id The alias of a connection when connected to a cluster.
      * @return ConnectionInterface
      */
-    public function getConnection($id = null);
+    public function getConnection();
 
     /**
      * Creates a new instance of the specified Redis command.

+ 6 - 6
tests/Predis/ClientTest.php

@@ -417,13 +417,13 @@ class ClientTest extends StandardTestCase
     /**
      * @group disconnected
      */
-    public function testGetConnectionFromClusterWithAlias()
+    public function testGetConnectionFromAggregatedConnectionWithAlias()
     {
         $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'));
 
         $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster = $client->getConnection());
-        $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node01 = $client->getConnection('node01'));
-        $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node02 = $client->getConnection('node02'));
+        $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node01 = $client->getConnectionById('node01'));
+        $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node02 = $client->getConnectionById('node02'));
 
         $this->assertSame('host1', $node01->getParameters()->host);
         $this->assertSame('host2', $node02->getParameters()->host);
@@ -432,13 +432,13 @@ class ClientTest extends StandardTestCase
     /**
      * @group disconnected
      * @expectedException Predis\NotSupportedException
-     * @expectedExceptionMessage Retrieving connections by alias is supported only with aggregated connections
+     * @expectedExceptionMessage Retrieving connections by ID is supported only when using aggregated connections
      */
-    public function testGetConnectionWithAliasWorksOnlyWithCluster()
+    public function testGetConnectionByIdWorksOnlyWithAggregatedConnections()
     {
         $client = new Client();
 
-        $client->getConnection('node01');
+        $client->getConnectionById('node01');
     }
 
     /**