| 1 |
<?php |
| 2 |
|
| 3 |
namespace ConnectionManager\Extra; |
| 4 |
|
| 5 |
use React\Socket\ConnectorInterface; |
| 6 |
use React\Promise; |
| 7 |
use Exception; |
| 8 |
|
| 9 |
// a simple connection manager that rejects every single connection attempt |
| 10 |
class ConnectionManagerReject implements ConnectorInterface |
| 11 |
{ |
| 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) |
| 25 |
{ |
| 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); |
| 40 |
} |
| 41 |
} |