Skip to content

Commit 7cdc5bf

Browse files
committed
Clean up Factory interface
1 parent 76f840f commit 7cdc5bf

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

Datagram/Factory.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@
88

99
class Factory
1010
{
11-
public function createClient(LoopInterface $loop, Resolver $resolver, $host, $port)
11+
protected $loop;
12+
protected $resolver;
13+
14+
public function __construct(LoopInterface $loop, Resolver $resolver = null)
15+
{
16+
$this->loop = $loop;
17+
$this->resolver = $resolver;
18+
}
19+
20+
public function createClient($host, $port)
1221
{
1322
$factory = $this;
14-
return $this->resolve($resolver, $host)->then(function ($ip) use ($loop, $port, $factory) {
23+
$loop = $this->loop;
24+
return $this->resolve($host)->then(function ($ip) use ($loop, $port, $factory) {
1525
$address = $factory->createAddress($ip, $port);
1626
$socket = stream_socket_client('udp://' . $address, $errno, $errstr);
1727
if (!$socket) {
@@ -23,18 +33,18 @@ public function createClient(LoopInterface $loop, Resolver $resolver, $host, $po
2333
});
2434
}
2535

26-
public function createServer(LoopInterface $loop, $port, $host = '127.0.0.1')
36+
public function createServer($port, $host = '127.0.0.1')
2737
{
2838
$address = $this->createAddress($host, $port);
2939

3040
$socket = stream_socket_server("udp://" . $address, $errno, $errstr, STREAM_SERVER_BIND);
3141
if (!$socket) {
3242
die("$errstr ($errno)");
3343
}
34-
return When::resolve(new Server($loop, $socket, $address));
44+
return When::resolve(new Server($this->loop, $socket, $address));
3545
}
3646

37-
protected function resolve($resolver, $host)
47+
protected function resolve($host)
3848
{
3949
// there's no need to resolve if the host is already given as an IP address
4050
if (false !== filter_var($host, FILTER_VALIDATE_IP)) {
@@ -44,7 +54,11 @@ protected function resolve($resolver, $host)
4454
if ($host === 'localhost') {
4555
return When::resolve('127.0.0.1');
4656
}
47-
return $resolver->resolve($host);
57+
58+
if ($resolver === null) {
59+
return When::reject(\Exception('No resolver given in order to get IP address for given hostname'));
60+
}
61+
return $this->resolver->resolve($host);
4862
}
4963

5064
public function createAddress($host, $port)

0 commit comments

Comments
 (0)