Passed
Push — master ( 102c19...95cf87 )
by Shiyu
04:19 queued 51s
created

Zipkin::init()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.9
1
<?php
2
/**
3
 * Zipkin implementer
4
 * User: moyo
5
 * Date: 23/11/2017
6
 * Time: 4:22 PM
7
 */
8
9
namespace Carno\Traced\Platform;
10
11
use Carno\Config\Config;
12
use Carno\Promise\Promise;
13
use Carno\Promise\Promised;
14
use Carno\Traced\Chips\TransportObserve;
15
use Carno\Traced\Chips\TransportSync;
16
use Carno\Traced\Contracts\Observer;
17
use Carno\Traced\Protocol\AlwaysNone;
18
use Carno\Traced\Protocol\ZipkinJFV2;
19
use Carno\Traced\Transport\Blackhole;
20
use Carno\Traced\Transport\UDPRelays;
21
use Carno\Traced\Transport\ZipkinHAV2;
22
use Carno\Traced\Utils\Environment;
23
use Carno\Tracing\Contracts\Carrier;
24
use Carno\Tracing\Contracts\Env;
25
use Carno\Tracing\Contracts\Platform;
26
use Carno\Tracing\Contracts\Protocol;
27
use Carno\Tracing\Contracts\Transport;
28
use Carno\Tracing\Standard\Carriers\HTTP;
29
30
class Zipkin implements Platform, Observer
31
{
32
    use TransportSync;
33
    use TransportObserve;
34
35
    /**
36
     * @var Environment
37
     */
38
    private $env = null;
39
40
    /**
41
     * @var Config
42
     */
43
    private $conf = null;
44
45
    /**
46
     * @var Carrier
47
     */
48
    private $carrier = null;
49
50
    /**
51
     * @var Protocol
52
     */
53
    private $protocol = null;
54
55
    /**
56
     * @var Transport
57
     */
58
    private $transport = null;
59
60
    /**
61
     * Zipkin constructor.
62
     * @param Config $config
63
     */
64
    public function __construct(Config $config)
65
    {
66
        $this->conf = $config;
67
68
        $this->env = new Environment();
69
        $this->carrier = new HTTP();
70
    }
71
72
    /**
73
     * @return Promised
74
     */
75
    public function init() : Promised
76
    {
77
        $this->syncing(
78
            $this->conf,
79
            'tracing.addr',
80
            [
81
                'zk-udp' => [UDPRelays::class, ZipkinJFV2::class],
82
                'zipkin' => [ZipkinHAV2::class, ZipkinJFV2::class],
83
            ],
84
            function (Transport $transport = null, Protocol $protocol = null) {
85
                $this->transport && $this->transport->disconnect();
86
                $this->transport = $transport;
87
                $this->protocol = $protocol;
88
                $this->changed($transport);
89
            }
90
        );
91
92
        return Promise::resolved();
93
    }
94
95
    /**
96
     * @return Environment
97
     */
98
    public function env() : Env
99
    {
100
        return $this->env;
101
    }
102
103
    /**
104
     * @return bool
105
     */
106
    public function joined() : bool
107
    {
108
        return $this->carrier && $this->protocol && $this->transport;
109
    }
110
111
    /**
112
     * @return Promised
113
     */
114
    public function leave() : Promised
115
    {
116
        return $this->transport ? $this->transport->disconnect() : Promise::resolved();
117
    }
118
119
    /**
120
     * @return Carrier
121
     */
122
    public function carrier() : Carrier
123
    {
124
        return $this->carrier;
125
    }
126
127
    /**
128
     * @return Protocol
129
     */
130
    public function serializer() : Protocol
131
    {
132
        return $this->protocol ?? new AlwaysNone();
133
    }
134
135
    /**
136
     * @return Transport
137
     */
138
    public function transporter() : Transport
139
    {
140
        return $this->transport ?? new Blackhole();
141
    }
142
}
143