Consul::attach()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 1
nop 3
dl 0
loc 27
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Discovering via consul
4
 * User: moyo
5
 * Date: 16/11/2017
6
 * Time: 11:32 AM
7
 */
8
9
namespace Carno\Cluster\Discovery\Adaptors;
10
11
use Carno\Channel\Chan;
12
use Carno\Channel\Channel;
13
use Carno\Channel\Worker;
14
use Carno\Cluster\Discovery\Discovered;
15
use Carno\Cluster\Managed;
16
use Carno\Consul\Discovery;
0 ignored issues
show
Bug introduced by
The type Carno\Consul\Discovery 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\Consul\Types\Agent;
0 ignored issues
show
Bug introduced by
The type Carno\Consul\Types\Agent 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...
18
use Carno\Consul\Types\Router;
0 ignored issues
show
Bug introduced by
The type Carno\Consul\Types\Router 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...
19
use function Carno\Coroutine\all;
20
use Carno\Promise\Promise;
21
use Carno\Promise\Promised;
22
23
class Consul implements Discovered
24
{
25
    /**
26
     * @var Agent
27
     */
28
    private $agent = null;
29
30
    /**
31
     * @var Managed[]
32
     */
33
    private $instances = [];
34
35
    /**
36
     * @var Chan[]
37
     */
38
    private $channels = [];
39
40
    /**
41
     * Connector constructor.
42
     * @param Agent $agent
43
     */
44
    public function __construct(Agent $agent)
45
    {
46
        $this->agent = $agent;
47
    }
48
49
    /**
50
     * @param string $group
51
     * @param string $server
52
     * @param Managed $managed
53
     * @return Promised
54
     */
55
    public function attach(string $group, string $server, Managed $managed) : Promised
56
    {
57
        (new Discovery($this->agent))
58
            ->watching(
59
                $named = $this->serviced($group, $server),
60
                $this->channels[$named] = $notify = new Channel
61
            )
62
        ;
63
64
        new Worker($notify, function (array $routes) use ($named, $managed) {
0 ignored issues
show
Unused Code introduced by
The import $named is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
65
            /**
66
             * @var Router[] $routes
67
             */
68
69
            foreach ($routes as $route) {
70
                if ($route->joined()) {
71
                    $managed->routing()->join($route->target());
72
                } elseif ($route->leaved()) {
73
                    $managed->routing()->leave($route->target());
74
                }
75
            }
76
77
            $managed->ready(true);
78
        });
79
80
        return $managed->ready(function () use ($named, $managed) {
81
            $this->instances[$named] = $managed;
82
        });
83
    }
84
85
86
    /**
87
     * @param string $group
88
     * @param string $server
89
     * @return Promised
90
     */
91
    public function detach(string $group, string $server) : Promised
92
    {
93
        $named = $this->serviced($group, $server);
94
95
        if (!isset($this->instances[$named])) {
96
            return Promise::rejected();
97
        }
98
99
        $managed = $this->instances[$named];
100
        unset($this->instances[$named]);
101
102
        $channel = $this->channels[$named];
103
        unset($this->channels[$named]);
104
105
        logger('cluster')->info('Service watcher has been closed', ['name' => $named]);
106
107
        $channel->close();
108
109
        return all($channel->closed(), $managed->shutdown());
110
    }
111
112
    /**
113
     * @param string $group
114
     * @param string $server
115
     * @return string
116
     */
117
    private function serviced(string $group, string $server) : string
118
    {
119
        return $group ? sprintf('%s:%s', $group, $server) : $server;
120
    }
121
}
122