Skip to content

Commit

Permalink
The socket library is now responsible for creating the url
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvdvleuten committed Feb 14, 2016
1 parent cee3040 commit 5855e73
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 38 deletions.
45 changes: 11 additions & 34 deletions src/Connection/Connection.php
Expand Up @@ -30,16 +30,6 @@ class Connection implements ConnectionInterface
*/
private $factory;

/**
* @var string
*/
private $host;

/**
* @var int
*/
private $port;

/**
* @var bool
*/
Expand All @@ -55,19 +45,22 @@ class Connection implements ConnectionInterface
*/
private $timeout;

/**
* @var string
*/
private $url;

/**
* Constructor.
*
* @param string $host The hostname of the NNTP server.
* @param int $port The port of the NNTP server.
* @param string $url The url of the NNTP server.
* @param bool $secure A bool indicating if a secure connection should be established.
* @param int $timeout The socket timeout in seconds.
* @param Factory $factory The socket client factory.
*/
public function __construct($host, $port, $secure = false, $timeout = 15, Factory $factory = null)
public function __construct($url, $secure = false, $timeout = 15, Factory $factory = null)
{
$this->host = $host;
$this->port = $port;
$this->url = $url;
$this->secure = $secure;
$this->timeout = $timeout;
$this->factory = $factory ?: new Factory();
Expand All @@ -78,14 +71,11 @@ public function __construct($host, $port, $secure = false, $timeout = 15, Factor
*/
public function connect()
{
$address = gethostbyname($this->host);
$url = $this->getSocketUrl($address);

try {
$this->socket = $this->factory->createFromString($url, $scheme)
->connectTimeout($url, $this->timeout);
$this->socket = $this->factory->createFromString($this->url, $scheme)
->connectTimeout($this->url, $this->timeout);
} catch (Exception $e) {
throw new RuntimeException(sprintf('Connection to %s:%d failed: %s', $address, $this->port, $e->getMessage()), 0, $e);
throw new RuntimeException(sprintf('Connection to %s://%s failed: %s', $scheme, $this->url, $e->getMessage()), 0, $e);
}

if ($this->secure) {
Expand Down Expand Up @@ -273,17 +263,4 @@ public function getCompressedResponse(Response $response)

return new MultiLineResponse($response, $lines);
}

/**
* @param string $address
*/
protected function getSocketUrl($address)
{
if (strpos($address, ':') !== false) {
// enclose IPv6 addresses in square brackets before appending port
$address = '['.$address.']';
}

return sprintf('tcp://%s:%s', $address, $this->port);
}
}
8 changes: 4 additions & 4 deletions tests/Connection/ConnectionTest.php
Expand Up @@ -47,17 +47,17 @@ public function setup()

$this->socket->expects($this->once())
->method('connectTimeout')
->with('tcp://127.0.0.1:5000', 15)
->with('localhost:5000', 15)
->willReturnSelf();

$this->factory = $this->getMock('Socket\Raw\Factory');

$this->factory->expects($this->once())
->method('createFromString')
->with('tcp://127.0.0.1:5000')
->with('localhost:5000')
->willReturn($this->socket);

$this->connection = new Connection('localhost', 5000, false, 15, $this->factory);
$this->connection = new Connection('localhost:5000', false, 15, $this->factory);
}

public function testConnectionCanBeEstablishedThroughSocket()
Expand Down Expand Up @@ -86,7 +86,7 @@ public function testErrorIsThrownWhenConnectionCannotBeEstablished()
{
$this->socket->expects($this->once())
->method('connectTimeout')
->with('tcp://127.0.0.1:5000', 15)
->with('localhost:5000', 15)
->willThrowException(new Exception());

$this->connection->connect();
Expand Down

0 comments on commit 5855e73

Please sign in to comment.