DnsConnector::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Thruster\Component\SocketClient;
4
5
use Thruster\Component\Dns\ResolverInterface;
6
use Thruster\Component\Promise\PromiseInterface;
7
use function Thruster\Component\Promise\resolve;
8
9
/**
10
 * Class DnsConnector
11
 *
12
 * @package Thruster\Component\SocketClient
13
 * @author  Aurimas Niekis <[email protected]>
14
 */
15
class DnsConnector implements ConnectorInterface
16
{
17
    /**
18
     * @var ConnectorInterface
19
     */
20
    private $connector;
21
22
    /**
23
     * @var ResolverInterface
24
     */
25
    private $resolver;
26
27 5
    public function __construct(ConnectorInterface $connector, ResolverInterface $resolver)
28
    {
29 5
        $this->connector = $connector;
30 5
        $this->resolver = $resolver;
31 5
    }
32
33 5
    public function create(string $host, int $port) : PromiseInterface
34
    {
35
        return $this
36 5
            ->resolveHostname($host)
37 5
            ->then(function ($address) use ($port) {
38 4
                return $this->connector->create($address, $port);
39 5
            });
40
    }
41
42 5
    private function resolveHostname(string $host)
43
    {
44 5
        if (false !== filter_var($host, FILTER_VALIDATE_IP)) {
45 1
            return resolve($host);
46
        }
47
48 4
        return $this->resolver->resolve($host);
49
    }
50
}
51