Typeset::picked()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Types manager
4
 * User: moyo
5
 * Date: 2018/7/31
6
 * Time: 6:15 PM
7
 */
8
9
namespace Carno\Cluster\Routing;
10
11
use Carno\Net\Endpoint;
12
13
class Typeset implements Typed
14
{
15
    /**
16
     * @var Typed[]
17
     */
18
    private $table = [];
19
20
    /**
21
     * Typeset constructor.
22
     * @param Typed ...$defaults
23
     */
24
    public function __construct(Typed ...$defaults)
25
    {
26
        foreach ($defaults as $typed) {
27
            $this->extend($typed);
28
        }
29
    }
30
31
    /**
32
     * @param Typed $typing
33
     * @return static
34
     */
35
    public function extend(Typed $typing) : self
36
    {
37
        $this->table[get_class($typing)] = $typing;
38
        return $this;
39
    }
40
41
    /**
42
     * @param string $typing
43
     * @return static
44
     */
45
    public function forget(string $typing) : self
46
    {
47
        unset($this->table[$typing]);
48
        return $this;
49
    }
50
51
    /**
52
     * @param string ...$tags
53
     * @return Endpoint[]
54
     */
55
    public function picked(string ...$tags) : array
56
    {
57
        foreach ($this->table as $typed) {
58
            if ($nodes = $typed->picked(...$tags)) {
59
                return $nodes;
60
            }
61
        }
62
        return [];
63
    }
64
65
    /**
66
     * @param string $tag
67
     * @param Endpoint $node
68
     */
69
    public function classify(string $tag, Endpoint $node) : void
70
    {
71
        foreach ($this->table as $typed) {
72
            $typed->classify($tag, $node);
73
        }
74
    }
75
76
    /**
77
     * @param Endpoint $node
78
     */
79
    public function release(Endpoint $node) : void
80
    {
81
        foreach ($this->table as $typed) {
82
            $typed->release($node);
83
        }
84
    }
85
}
86