Skip to content

Commit eba2026

Browse files
committed
Return rejected promise when AMI URL is invalid
1 parent 488e54b commit eba2026

4 files changed

Lines changed: 21 additions & 4 deletions

File tree

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ $factory = new Factory($loop, $connector);
100100
#### createClient()
101101

102102
The `createClient(string $amiUrl): PromiseInterface<Client>` method can be used to create a new [`Client`](#client).
103-
It helps with establishing a plain TCP/IP or secure SSL/TLS connection to the AMI
104-
and issuing an initial `login` action.
103+
It helps with establishing a plain TCP/IP or secure TLS connection to the AMI
104+
and optionally issuing an initial `login` action.
105105

106106
```php
107107
$factory->createClient($amiUrl)->then(
@@ -114,6 +114,10 @@ $factory->createClient($amiUrl)->then(
114114
);
115115
```
116116

117+
The method returns a [Promise](https://github.com/reactphp/promise) that will
118+
resolve with the [`Client`](#client) instance on success or will reject with an
119+
`Exception` if the URL is invalid or the connection or authentication fails.
120+
117121
The `$amiUrl` contains the host and optional port to connect to:
118122

119123
```php

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"php": ">=5.3",
1515
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
1616
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
17-
"react/promise": "^2.0 || ^1.0",
17+
"react/promise": "^2.0 || ^1.1",
1818
"react/socket": "^1.0 || ^0.8.2"
1919
},
2020
"require-dev": {

src/Factory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use React\Socket\Connector;
88
use React\Socket\ConnectorInterface;
99
use InvalidArgumentException;
10+
use React\Promise;
1011

1112
class Factory
1213
{
@@ -25,7 +26,11 @@ public function __construct(LoopInterface $loop, ConnectorInterface $connector =
2526

2627
public function createClient($address = null)
2728
{
28-
$parts = $this->parseUrl($address);
29+
try {
30+
$parts = $this->parseUrl($address);
31+
} catch (\Exception $e) {
32+
return Promise\reject($e);
33+
}
2934

3035
if (isset($parts['scheme']) && $parts['scheme'] !== 'tcp') {
3136
$parts['host'] = 'tls://' . $parts['host'];

tests/FactoryTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Clue\React\Ami\Factory;
44
use React\Promise\Promise;
5+
56
class FactoryTest extends TestCase
67
{
78
private $loop;
@@ -44,4 +45,11 @@ public function testCreateClientUsesTlsConnectorWithTlsLocation()
4445

4546
$this->factory->createClient('tls://ami.local:1234');
4647
}
48+
49+
public function testCreateClientWithInvalidUrlWillRejectPromise()
50+
{
51+
$promise = $this->factory->createClient('///');
52+
53+
$promise->then(null, $this->expectCallableOnce());
54+
}
4755
}

0 commit comments

Comments
 (0)