Skip to content

Commit 3e120b9

Browse files
committed
Accept optional rejection callback for ConnectionManagerReject
1 parent 439141c commit 3e120b9

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,15 @@ $delayed = new ConnectionManagerDelayed($connector, 0.5, $loop);
9696

9797
### Reject
9898

99-
The `ConnectionManagerReject(?string $reason)` simply rejects every single connection attempt.
99+
The `ConnectionManagerReject(null|string|callable $reason)` simply rejects every single connection attempt.
100100
This is particularly useful for the below [`ConnectionManagerSelective`](#selective) to reject connection attempts
101101
to only certain destinations (for example blocking advertisements or harmful sites).
102102

103-
The constructor accepts an optional rejection reason which will be used for the
104-
`Exception` instance that is used to reject the resulting promise.
103+
The constructor accepts an optional rejection reason which will be used for
104+
rejecting the resulting promise.
105+
106+
You can explicitly pass a `string` value which will be used as the message for
107+
the `Exception` instance:
105108

106109
```php
107110
$connector = new ConnectionManagerReject('Blocked');
@@ -111,6 +114,19 @@ $connector->connect('www.google.com:80')->then(null, function ($e) {
111114
});
112115
```
113116

117+
You can explicitly pass a `callable` value which will be used to either
118+
`throw` or `return` a custom `Exception` instance:
119+
120+
```php
121+
$connector = new ConnectionManagerReject(function ($uri) {
122+
throw new RuntimeException($uri . ' blocked');
123+
});
124+
$connector->connect('www.google.com:80')->then(null, function ($e) {
125+
assert($e instanceof \RuntimeException);
126+
assert($e->getMessage() === 'www.google.com:80 blocked');
127+
});
128+
```
129+
114130
### Swappable
115131

116132
The `ConnectionManagerSwappable($connector)` is a simple decorator for other `ConnectionManager`s to

src/ConnectionManagerReject.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ class ConnectionManagerReject implements ConnectorInterface
1212
private $reason = 'Connection rejected';
1313

1414
/**
15-
*
16-
* @param ?string $reason
15+
* @param null|string|callable $reason
1716
*/
1817
public function __construct($reason = null)
1918
{
@@ -22,8 +21,21 @@ public function __construct($reason = null)
2221
}
2322
}
2423

25-
public function connect($_)
24+
public function connect($uri)
2625
{
27-
return Promise\reject(new Exception($this->reason));
26+
$reason = $this->reason;
27+
if (!is_string($reason)) {
28+
try {
29+
$reason = $reason($uri);
30+
} catch (\Exception $e) {
31+
$reason = $e;
32+
}
33+
}
34+
35+
if (!$reason instanceof \Exception) {
36+
$reason = new Exception($reason);
37+
}
38+
39+
return Promise\reject($reason);
2840
}
2941
}

tests/ConnectionManagerRejectTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,38 @@ public function testRejectWithCustomMessage()
2626

2727
$this->assertEquals('Blocked', $text);
2828
}
29+
30+
public function testRejectThrowsCustomException()
31+
{
32+
$cm = new ConnectionManagerReject(function ($uri) {
33+
throw new RuntimeException('Blocked ' . $uri);
34+
});
35+
36+
$promise = $cm->connect('www.google.com:80');
37+
38+
$exception = null;
39+
$promise->then($this->expectCallableNever(), function ($e) use (&$exception) {
40+
$exception = $e;
41+
});
42+
43+
$this->assertInstanceOf('RuntimeException', $exception);
44+
$this->assertEquals('Blocked www.google.com:80', $exception->getMessage());
45+
}
46+
47+
public function testRejectReturnsCustomException()
48+
{
49+
$cm = new ConnectionManagerReject(function ($uri) {
50+
return new RuntimeException('Blocked ' . $uri);
51+
});
52+
53+
$promise = $cm->connect('www.google.com:80');
54+
55+
$exception = null;
56+
$promise->then($this->expectCallableNever(), function ($e) use (&$exception) {
57+
$exception = $e;
58+
});
59+
60+
$this->assertInstanceOf('RuntimeException', $exception);
61+
$this->assertEquals('Blocked www.google.com:80', $exception->getMessage());
62+
}
2963
}

0 commit comments

Comments
 (0)