Skip to content

Commit c6c2b64

Browse files
committed
Require URL when creating client
1 parent eba2026 commit c6c2b64

3 files changed

Lines changed: 9 additions & 41 deletions

File tree

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@ The `$amiUrl` contains the host and optional port to connect to:
124124
$factory->createClient('127.0.0.1:5038');
125125
```
126126

127-
> If the `$amiUrl` is `null` (or omitted) this method defaults to connecting
128-
to your local host (`127.0.0.1:5038`).
129-
130127
The above examples to not pass any authentication details, so you may have to
131128
call `ActionSender::login()` after connecting or use the recommended shortcut
132129
to pass a username and secret for your AMI login details like this:

src/Factory.php

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ public function __construct(LoopInterface $loop, ConnectorInterface $connector =
2424
$this->connector = $connector;
2525
}
2626

27-
public function createClient($address = null)
27+
public function createClient($url)
2828
{
29-
try {
30-
$parts = $this->parseUrl($address);
31-
} catch (\Exception $e) {
32-
return Promise\reject($e);
29+
$parts = parse_url((strpos($url, '://') === false ? 'tcp://' : '') . $url);
30+
if (!$parts || !isset($parts['scheme'], $parts['host'])) {
31+
return Promise\reject(new InvalidArgumentException('Given URL "' . $url . '" can not be parsed'));
3332
}
3433

35-
if (isset($parts['scheme']) && $parts['scheme'] !== 'tcp') {
36-
$parts['host'] = 'tls://' . $parts['host'];
34+
// use default port 5038
35+
if (!isset($parts['port'])) {
36+
$parts['port'] = 5038;
3737
}
3838

39-
$promise = $this->connector->connect($parts['host'] . ':' . $parts['port'])->then(function (ConnectionInterface $stream) {
39+
$promise = $this->connector->connect($parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port'])->then(function (ConnectionInterface $stream) {
4040
return new Client($stream);
4141
});
4242

@@ -58,25 +58,4 @@ function ($error) use ($client) {
5858

5959
return $promise;
6060
}
61-
62-
private function parseUrl($target)
63-
{
64-
if ($target === null) {
65-
$target = 'tcp://127.0.0.1';
66-
}
67-
if (strpos($target, '://') === false) {
68-
$target = 'tcp://' . $target;
69-
}
70-
71-
$parts = parse_url($target);
72-
if ($parts === false || !isset($parts['host'])) {
73-
throw new InvalidArgumentException('Given URL can not be parsed');
74-
}
75-
76-
if (!isset($parts['port'])) {
77-
$parts['port'] = '5038';
78-
}
79-
80-
return $parts;
81-
}
8261
}

tests/FactoryTest.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,10 @@ public function testDefaultCtor()
2222
$this->factory = new Factory($this->loop);
2323
}
2424

25-
public function testCreateClientUsesTcpConnectorWithDefaultLocation()
26-
{
27-
$promise = new Promise(function () { });
28-
$this->tcp->expects($this->once())->method('connect')->with('127.0.0.1:5038')->willReturn($promise);
29-
30-
$this->factory->createClient();
31-
}
32-
3325
public function testCreateClientUsesDefaultPortForTcpConnection()
3426
{
3527
$promise = new Promise(function () { });
36-
$this->tcp->expects($this->once())->method('connect')->with('localhost:5038')->willReturn($promise);
28+
$this->tcp->expects($this->once())->method('connect')->with('tcp://localhost:5038')->willReturn($promise);
3729

3830
$this->factory->createClient('localhost');
3931
}

0 commit comments

Comments
 (0)