Skip to content

Commit a72e1d6

Browse files
authored
Merge pull request #19 from clue-labs/invalid-uri
Reject promise for invalid URI passed to ConnectionManagerSelective
2 parents a2b2ccd + b581fc9 commit a72e1d6

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/Multiple/ConnectionManagerSelective.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,20 @@ public function __construct(array $managers)
6565

6666
public function connect($uri)
6767
{
68-
try {
69-
$parts = parse_url('tcp://' . $uri);
70-
if (!isset($parts) || !isset($parts['scheme'], $parts['host'], $parts['port'])) {
71-
throw new InvalidArgumentException('Invalid URI');
72-
}
68+
$parts = parse_url('tcp://' . $uri);
69+
if (!isset($parts) || !isset($parts['scheme'], $parts['host'], $parts['port'])) {
70+
return Promise\reject(new InvalidArgumentException('Invalid URI'));
71+
}
7372

74-
$connector = $this->getConnectorForTarget(
75-
trim($parts['host'], '[]'),
76-
$parts['port']
77-
);
78-
} catch (UnderflowException $e) {
79-
return Promise\reject($e);
73+
$connector = $this->getConnectorForTarget(
74+
trim($parts['host'], '[]'),
75+
$parts['port']
76+
);
77+
78+
if ($connector === null) {
79+
return Promise\reject(new UnderflowException('No connector for given target found'));
8080
}
81+
8182
return $connector->connect($uri);
8283
}
8384

@@ -105,6 +106,6 @@ private function getConnectorForTarget($targetHost, $targetPort)
105106
}
106107
}
107108

108-
throw new UnderflowException('No connector for given target found');
109+
return null;
109110
}
110111
}

tests/Multiple/ConnectionManagerSelectiveTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ public function testEmptyWillAlwaysReject()
1313
$this->assertPromiseReject($promise);
1414
}
1515

16+
public function testInvalidUriWillAlwaysReject()
17+
{
18+
$cm = new ConnectionManagerSelective(array());
19+
20+
$promise = $cm->connect('////');
21+
$this->assertPromiseReject($promise);
22+
}
23+
1624
/**
1725
* @expectedException InvalidArgumentException
1826
*/

0 commit comments

Comments
 (0)