Skip to content

Commit 1e719a0

Browse files
authored
Merge pull request #23 from clue-labs/reject-reason
Support custom rejection reason for ConnectionManagerReject
2 parents 8b73b8a + 3e120b9 commit 1e719a0

File tree

3 files changed

+102
-3
lines changed

3 files changed

+102
-3
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,37 @@ $delayed = new ConnectionManagerDelayed($connector, 0.5, $loop);
9696

9797
### Reject
9898

99-
The `ConnectionManagerReject()` 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
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:
108+
109+
```php
110+
$connector = new ConnectionManagerReject('Blocked');
111+
$connector->connect('www.google.com:80')->then(null, function ($e) {
112+
assert($e instanceof \Exception);
113+
assert($e->getMessage() === 'Blocked');
114+
});
115+
```
116+
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+
103130
### Swappable
104131

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

src/ConnectionManagerReject.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,33 @@
99
// a simple connection manager that rejects every single connection attempt
1010
class ConnectionManagerReject implements ConnectorInterface
1111
{
12-
public function connect($_)
12+
private $reason = 'Connection rejected';
13+
14+
/**
15+
* @param null|string|callable $reason
16+
*/
17+
public function __construct($reason = null)
18+
{
19+
if ($reason !== null) {
20+
$this->reason = $reason;
21+
}
22+
}
23+
24+
public function connect($uri)
1325
{
14-
return Promise\reject(new Exception('Connection rejected'));
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);
1540
}
1641
}

tests/ConnectionManagerRejectTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,51 @@ public function testReject()
1313

1414
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
1515
}
16+
17+
public function testRejectWithCustomMessage()
18+
{
19+
$cm = new ConnectionManagerReject('Blocked');
20+
$promise = $cm->connect('www.google.com:80');
21+
22+
$text = null;
23+
$promise->then($this->expectCallableNever(), function (Exception $e) use (&$text) {
24+
$text = $e->getMessage();
25+
});
26+
27+
$this->assertEquals('Blocked', $text);
28+
}
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+
}
1663
}

0 commit comments

Comments
 (0)