Passed
Push — master ( a7d12b...46dbe8 )
by Dmitry
03:07
created

Span::setInterval()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 2
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 6
    public function end(Status $status = null) : self
41
    {
42 6
        $this->end = microtime(true);
43 6
        if (!$this->status && !$status) {
44 5
            $status = new Status(Status::OK);
45
        }
46 6
        if ($status) {
47 6
            $this->setStatus($status);
48
        }
49 6
        return $this;
50
    }
51
52 3
    public function getStart()
53
    {
54 3
        return $this->start;
55
    }
56
57 5
    public function getEnd()
58
    {
59 5
        return $this->end;
60
    }
61
62 6
    public function setStatus(Status $status) : self
63
    {
64 6
        $this->status = $status;
65 6
        return $this;
66
    }
67
68 2
    public function getStatus() : ?Status
69
    {
70 2
        return $this->status;
71
    }
72
73 5
    public function isRecordingEvents() : bool
74
    {
75 5
        return is_null($this->end);
76
    }
77
78 1
    public function getDuration() : ?float
79
    {
80 1
        if (!$this->end) {
81 1
            return null;
82
        }
83 1
        return $this->end - $this->start;
84
    }
85
86 3
    public function getName() : string
87
    {
88 3
        return $this->name;
89
    }
90
91
    public function setInterval(float $start, float $end) : self
92
    {
93
        $this->start = $start;
94
        $this->end = $end;
95
        return $this;
96
    }
97
98 1
    public function setName(string $name) : self
99
    {
100 1
        $this->name = $name;
101 1
        return $this;
102
    }
103
104 3
    public function getAttribute(string $key)
105
    {
106 3
        if (!array_key_exists($key, $this->attributes)) {
107 1
            return null;
108
        }
109 3
        return $this->attributes[$key];
110
    }
111
112 3
    public function setAttribute(string $key, $value) : self
113
    {
114 3
        $this->throwExceptionIfReadonly();
115
116 3
        $this->attributes[$key] = $value;
117 3
        return $this;
118
    }
119
120 2
    public function getAttributes() : array
121
    {
122 2
        return $this->attributes;
123
    }
124
125 1
    public function setAttributes(array $attributes) : self
126
    {
127 1
        $this->throwExceptionIfReadonly();
128
129 1
        $this->attributes = [];
130 1
        foreach ($attributes as $k => $v) {
131 1
            $this->setAttribute($k, $v);
132
        }
133 1
        return $this;
134
    }
135
136 3
    public function addEvent(string $name, array $attributes = [], float $timestamp = null) : Event
137
    {
138 3
        $this->throwExceptionIfReadonly();
139
140 3
        $event = new Event($name, $attributes, $timestamp);
141 3
        $this->events[] = $event;
142 3
        return $event;
143
    }
144
145 3
    public function getEvents()
146
    {
147 3
        return $this->events;
148
    }
149
150 4
    private function throwExceptionIfReadonly()
151
    {
152 4
        if (!$this->isRecordingEvents()) {
153 2
            throw new Exception("Span is readonly");
154
        }
155
    }
156
}