Typeset   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A extend() 0 4 1
A forget() 0 4 1
A picked() 0 8 3
A release() 0 4 2
A classify() 0 4 2
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