public function createClient($target = null)
{
try {
$parts = $this->parseUrl($target);
} catch (InvalidArgumentException $e) {
return Promise\reject($e);
}
$protocol = $this->protocol;
$promise = $this->connector->create($parts['host'], $parts['port'])->then(function (Stream $stream) use($protocol) {
return new StreamingClient($stream, $protocol->createResponseParser(), $protocol->createSerializer());
});
if (isset($parts['auth'])) {
$promise = $promise->then(function (StreamingClient $client) use($parts) {
return $client->auth($parts['auth'])->then(function () use($client) {
return $client;
}, function ($error) use($client) {
$client->close();
throw $error;
});
});
}
if (isset($parts['db'])) {
$promise = $promise->then(function (StreamingClient $client) use($parts) {
return $client->select($parts['db'])->then(function () use($client) {
return $client;
}, function ($error) use($client) {
$client->close();
throw $error;
});
});
}
return $promise;
}