Transaction   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 151
ccs 50
cts 50
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A inContext() 0 7 1
A resultingIn() 0 7 1
A withSpan() 0 7 1
A marking() 0 11 2
A thatIsSampled() 0 7 1
A thatIsNotSampled() 0 7 1
A withTotalDroppedSpans() 0 7 1
A jsonSerialize() 0 16 2
1
<?php
2
declare(strict_types=1);
3
4
namespace TechDeCo\ElasticApmAgent\Message;
5
6
use JsonSerializable;
7
use Ramsey\Uuid\UuidInterface;
8
use TechDeCo\ElasticApmAgent\Serialization;
9
use function array_merge;
10
11
final class Transaction implements JsonSerializable
12
{
13
    /**
14
     * @var Context|null
15
     */
16
    private $context;
17
18
    /**
19
     * @var float
20
     */
21
    private $duration;
22
23
    /**
24
     * @var UuidInterface
25
     */
26
    private $id;
27
28
    /**
29
     * @var string
30
     */
31
    private $name;
32
33
    /**
34
     * @var string|null
35
     */
36
    private $result;
37
38
    /**
39
     * @var Timestamp
40
     */
41
    private $timestamp;
42
43
    /**
44
     * @var Span[]
45
     */
46
    private $spanList = [];
47
48
    /**
49
     * @var string
50
     */
51
    private $type;
52
53
    /**
54
     * @var mixed[]
55
     */
56
    private $markList = [];
57
58
    /**
59
     * @var bool|null
60
     */
61
    private $isSampled;
62
63
    /**
64
     * @var int
65
     */
66
    private $droppedTotalSpanCount;
67
68 49
    public function __construct(
69
        float $duration,
70
        UuidInterface $id,
71
        string $name,
72
        Timestamp $timestamp,
73
        string $type
74
    ) {
75 49
        $this->duration  = $duration;
76 49
        $this->id        = $id;
77 49
        $this->name      = $name;
78 49
        $this->timestamp = $timestamp;
79 49
        $this->type      = $type;
80 49
    }
81
82 35
    public function inContext(Context $context): self
83
    {
84 35
        $me          = clone $this;
85 35
        $me->context = $context;
86
87 35
        return $me;
88
    }
89
90 1
    public function resultingIn(string $result): self
91
    {
92 1
        $me         = clone $this;
93 1
        $me->result = $result;
94
95 1
        return $me;
96
    }
97
98 35
    public function withSpan(Span ...$span): self
99
    {
100 35
        $me           = clone $this;
101 35
        $me->spanList = array_merge($me->spanList, $span);
102
103 35
        return $me;
104
    }
105
106 16
    public function marking(string $group, string $event, float $timestamp): self
107
    {
108 16
        $me = clone $this;
109
110 16
        if (! isset($me->markList[$group])) {
111 16
            $me->markList[$group] = [];
112
        }
113 16
        $me->markList[$group][$event] = $timestamp;
114
115 16
        return $me;
116
    }
117
118 1
    public function thatIsSampled(): self
119
    {
120 1
        $me            = clone $this;
121 1
        $me->isSampled = true;
122
123 1
        return $me;
124
    }
125
126 1
    public function thatIsNotSampled(): self
127
    {
128 1
        $me            = clone $this;
129 1
        $me->isSampled = false;
130
131 1
        return $me;
132
    }
133
134 1
    public function withTotalDroppedSpans(int $count): self
135
    {
136 1
        $me                        = clone $this;
137 1
        $me->droppedTotalSpanCount = $count;
138
139 1
        return $me;
140
    }
141
142
    /**
143
     * @return mixed[]
144
     */
145 44
    public function jsonSerialize(): array
146
    {
147 44
        return Serialization::filterUnset([
148 44
            'context' => Serialization::serializeOr($this->context),
149 44
            'duration' => $this->duration,
150 44
            'id' => $this->id->toString(),
151 44
            'name' => $this->name,
152 44
            'result' => $this->result,
153 44
            'timestamp' => $this->timestamp->jsonSerialize(),
154 44
            'spans' => Serialization::serialize(...$this->spanList),
155 44
            'type' => $this->type,
156 44
            'marks' => $this->markList,
157 44
            'sampled' => $this->isSampled,
158 44
            'span_count' => $this->droppedTotalSpanCount ? ['dropped' => ['total' => $this->droppedTotalSpanCount]] : null,
159
        ]);
160
    }
161
}
162