DnsConnector   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%
Metric Value
dl 0
loc 36
wmc 4
lcom 1
cbo 3
ccs 13
cts 13
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 8 1
A resolveHostname() 0 8 2
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