Span::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 2
b 1
f 0
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 12
    public function __construct(string $name, SpanContext $spanContext, SpanContext $parentSpanContext = null)
23
    {
24 12
        $this->name = $name;
25 12
        $this->spanContext = $spanContext;
26 12
        $this->parentSpanContext = $parentSpanContext;
27 12
        $this->start = microtime(true);
28 12
    }
29
30 11
    public function getSpanContext(): SpanContext
31
    {
32 11
        return $this->spanContext;
33
    }
34
35 5
    public function getParentSpanContext(): ?SpanContext
36
    {
37 5
        return $this->parentSpanContext;
38
    }
39
40
    public function setParentSpanContext(SpanContext $parentSpanContext = null): self
41
    {
42
        $this->parentSpanContext = $parentSpanContext;
43
        return $this;
44
    }
45
46 7
    public function end(Status $status = null): self
47
    {
48 7
        $this->end = microtime(true);
49 7
        if (!$this->status && !$status) {
50 6
            $status = new Status(Status::OK);
51
        }
52 7
        if ($status) {
53 7
            $this->setStatus($status);
54
        }
55 7
        return $this;
56
    }
57
58 4
    public function getStart()
59
    {
60 4
        return $this->start;
61
    }
62
63 6
    public function getEnd()
64
    {
65 6
        return $this->end;
66
    }
67
68 7
    public function setStatus(Status $status): self
69
    {
70 7
        $this->status = $status;
71 7
        return $this;
72
    }
73
74 3
    public function getStatus(): ?Status
75
    {
76 3
        return $this->status;
77
    }
78
79 6
    public function isRecordingEvents(): bool
80
    {
81 6
        return $this->end === null;
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 4
    public function getName(): string
93
    {
94 4
        return $this->name;
95
    }
96
97 1
    public function setInterval(float $start, ?float $end): self
98
    {
99 1
        $this->start = $start;
100 1
        $this->end = $end;
101 1
        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 4
    public function setAttribute(string $key, $value): self
119
    {
120 4
        $this->throwExceptionIfReadonly();
121
122 4
        $this->attributes[$key] = $value;
123 4
        return $this;
124
    }
125
126 3
    public function getAttributes(): array
127
    {
128 3
        return $this->attributes;
129
    }
130
131 2
    public function setAttributes(array $attributes): self
132
    {
133 2
        $this->throwExceptionIfReadonly();
134
135 2
        $this->attributes = [];
136 2
        foreach ($attributes as $k => $v) {
137 2
            $this->setAttribute($k, $v);
138
        }
139 2
        return $this;
140
    }
141
142 4
    public function addEvent(string $name, array $attributes = [], float $timestamp = null): Event
143
    {
144 4
        $this->throwExceptionIfReadonly();
145
146 4
        $event = new Event($name, $attributes, $timestamp);
147 4
        $this->events[] = $event;
148 4
        return $event;
149
    }
150
151 4
    public function getEvents()
152
    {
153 4
        return $this->events;
154
    }
155
156 5
    private function throwExceptionIfReadonly()
157
    {
158 5
        if (!$this->isRecordingEvents()) {
159 2
            throw new Exception("Span is readonly");
160
        }
161 5
    }
162
}
163