Passed
Push — master ( 6f2ed7...e5b8b5 )
by Dmitry
01:50
created

Span::setParentSpanContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace OpenTelemetry\Tracing;
6
7
use Exception;
8
9
class Span
10
{
11
    private $name;
12
    private $spanContext;
13
    private $parentSpanContext;
14
15
    private $start;
16
    private $end;
17
    private $status;
18
19
    private $attributes = [];
20
    private $events = [];
21
22 11
    public function __construct(string $name, SpanContext $spanContext, SpanContext $parentSpanContext = null)
23
    {
24 11
        $this->name = $name;
25 11
        $this->spanContext = $spanContext;
26 11
        $this->parentSpanContext = $parentSpanContext;
27 11
        $this->start = microtime(true);
28 11
    }
29
30 10
    public function getSpanContext() : SpanContext
31
    {
32 10
        return $this->spanContext;
33
    }
34
35 4
    public function getParentSpanContext() : ?SpanContext
36
    {
37 4
        return $this->parentSpanContext;
38
    }
39
40
    public function setParentSpanContext(SpanContext $parentSpanContext = null) : self
41
    {
42
        $this->parentSpanContext = $parentSpanContext;
43
        return $this;
44
    }
45
46 6
    public function end(Status $status = null) : self
47
    {
48 6
        $this->end = microtime(true);
49 6
        if (!$this->status && !$status) {
50 5
            $status = new Status(Status::OK);
51
        }
52 6
        if ($status) {
53 6
            $this->setStatus($status);
54
        }
55 6
        return $this;
56
    }
57
58 3
    public function getStart()
59
    {
60 3
        return $this->start;
61
    }
62
63 5
    public function getEnd()
64
    {
65 5
        return $this->end;
66
    }
67
68 6
    public function setStatus(Status $status) : self
69
    {
70 6
        $this->status = $status;
71 6
        return $this;
72
    }
73
74 2
    public function getStatus() : ?Status
75
    {
76 2
        return $this->status;
77
    }
78
79 5
    public function isRecordingEvents() : bool
80
    {
81 5
        return is_null($this->end);
82
    }
83
84 1
    public function getDuration() : ?float
85
    {
86 1
        if (!$this->end) {
87 1
            return null;
88
        }
89 1
        return $this->end - $this->start;
90
    }
91
92 3
    public function getName() : string
93
    {
94 3
        return $this->name;
95
    }
96
97
    public function setInterval(float $start, float $end) : self
98
    {
99
        $this->start = $start;
100
        $this->end = $end;
101
        return $this;
102
    }
103
104 1
    public function setName(string $name) : self
105
    {
106 1
        $this->name = $name;
107 1
        return $this;
108
    }
109
110 3
    public function getAttribute(string $key)
111
    {
112 3
        if (!array_key_exists($key, $this->attributes)) {
113 1
            return null;
114
        }
115 3
        return $this->attributes[$key];
116
    }
117
118 3
    public function setAttribute(string $key, $value) : self
119
    {
120 3
        $this->throwExceptionIfReadonly();
121
122 3
        $this->attributes[$key] = $value;
123 3
        return $this;
124
    }
125
126 2
    public function getAttributes() : array
127
    {
128 2
        return $this->attributes;
129
    }
130
131 1
    public function setAttributes(array $attributes) : self
132
    {
133 1
        $this->throwExceptionIfReadonly();
134
135 1
        $this->attributes = [];
136 1
        foreach ($attributes as $k => $v) {
137 1
            $this->setAttribute($k, $v);
138
        }
139 1
        return $this;
140
    }
141
142 3
    public function addEvent(string $name, array $attributes = [], float $timestamp = null) : Event
143
    {
144 3
        $this->throwExceptionIfReadonly();
145
146 3
        $event = new Event($name, $attributes, $timestamp);
147 3
        $this->events[] = $event;
148 3
        return $event;
149
    }
150
151 3
    public function getEvents()
152
    {
153 3
        return $this->events;
154
    }
155
156 4
    private function throwExceptionIfReadonly()
157
    {
158 4
        if (!$this->isRecordingEvents()) {
159 2
            throw new Exception("Span is readonly");
160
        }
161
    }
162
}