DNS::attach()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 1
nop 3
dl 0
loc 23
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Discovering via dns
4
 * User: moyo
5
 * Date: 16/11/2017
6
 * Time: 11:35 AM
7
 */
8
9
namespace Carno\Cluster\Discovery\Adaptors;
10
11
use Carno\Cluster\Discovery\Discovered;
12
use Carno\Cluster\Managed;
13
use function Carno\Coroutine\go;
14
use Carno\DNS\DNS as NSR;
0 ignored issues
show
Bug introduced by
The type Carno\DNS\DNS was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Carno\DNS\Exception\ResolvingException;
0 ignored issues
show
Bug introduced by
The type Carno\DNS\Exception\ResolvingException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Carno\DNS\Result;
0 ignored issues
show
Bug introduced by
The type Carno\DNS\Result was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Carno\Net\Address;
18
use Carno\Net\Endpoint;
19
use Carno\Promise\Promised;
20
21
class DNS implements Discovered
22
{
23
    /**
24
     * @var string
25
     */
26
    private $suffix = '';
27
28
    /**
29
     * @var Managed[]
30
     */
31
    private $managed = [];
32
33
    /**
34
     * DNS constructor.
35
     * @param string $suffix
36
     */
37
    public function __construct(string $suffix = '')
38
    {
39
        $this->suffix = $suffix;
40
    }
41
42
    /**
43
     * @param string $group
44
     * @param string $server
45
     * @param Managed $managed
46
     * @return Promised
47
     */
48
    public function attach(string $group, string $server, Managed $managed) : Promised
49
    {
50
        $this->managed[$domain = $this->domain($server)] = $managed;
51
52
        go(static function () use ($domain, $managed) {
53
            try {
54
                /**
55
                 * @var Result $resolved
56
                 */
57
                $resolved = yield NSR::resolve($domain);
58
                foreach ($resolved->iterator() as $host) {
59
                    $managed->routing()->join(
60
                        (new Endpoint(new Address($host, $managed->port())))
61
                            ->relatedService($domain)
62
                            ->assignID(ip2long($host))
63
                    );
64
                }
65
            } catch (ResolvingException $e) {
66
                logger('cluster')->warning('DNS resolve failed', ['domain' => $domain, 'ec' => get_class($e)]);
67
            }
68
        });
69
70
        return $managed->joined();
71
    }
72
73
    /**
74
     * @param string $group
75
     * @param string $server
76
     * @return Promised
77
     */
78
    public function detach(string $group, string $server) : Promised
79
    {
80
        return $this->managed[$this->domain($server)]->shutdown();
81
    }
82
83
    /**
84
     * @param string $server
85
     * @return string
86
     */
87
    private function domain(string $server) : string
88
    {
89
        return $server . $this->suffix;
90
    }
91
}
92