Discovery::endpointsLister()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
/**
3
 * Service watcher
4
 * User: moyo
5
 * Date: 22/09/2017
6
 * Time: 4:38 PM
7
 */
8
9
namespace Carno\Consul;
10
11
use Carno\Channel\Chan;
12
use Carno\Consul\APIs\AbstractEndpointsLister;
13
use Carno\Consul\APIs\CatalogServiceEndpoints;
14
use Carno\Consul\APIs\HealthServiceEndpoints;
15
use Carno\Consul\Chips\AgentRequired;
16
use Carno\Consul\Chips\GWatcher;
17
use Carno\Consul\Chips\WRouter;
18
use Carno\Consul\Exception\UnknownOptionsException;
19
20
class Discovery
21
{
22
    use AgentRequired, WRouter, GWatcher;
23
24
    /**
25
     * [catalog,health]
26
     */
27
    private const ENDPOINTS_API = 'health';
28
29
    /**
30
     * @param string $service
31
     * @param Chan $notify
32
     */
33
    public function watching(string $service, Chan $notify) : void
34
    {
35
        $ig = function () use ($service) {
36
            return $this->endpointsLister($service);
37
        };
38
39
        $do = function (AbstractEndpointsLister $lister) use ($notify) {
40
            yield $notify->send($this->routing(yield $lister->result()));
41
        };
42
43
        $this->nwProcess($notify->closed(), $ig, $do, 'Service watcher interrupted', ['svc' => $service]);
44
    }
45
46
    /**
47
     * @param string $service
48
     * @return AbstractEndpointsLister
49
     */
50
    private function endpointsLister(string $service) : AbstractEndpointsLister
51
    {
52
        switch (self::ENDPOINTS_API) {
53
            case 'catalog':
54
                return (new CatalogServiceEndpoints($this->agent))->service($service);
55
            case 'health':
56
                return (new HealthServiceEndpoints($this->agent))->service($service);
57
            default:
58
                throw new UnknownOptionsException;
59
        }
60
    }
61
}
62