OpenTransaction   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 127
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A getId() 0 4 1
A addSpan() 0 4 1
A addMark() 0 8 2
A getContext() 0 4 1
A setContext() 0 4 1
A toTransaction() 0 20 3
A getStartOffset() 0 4 1
A getCorrelationId() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TechDeCo\ElasticApmAgent\Convenience;
5
6
use Ramsey\Uuid\Uuid;
7
use Ramsey\Uuid\UuidInterface;
8
use TechDeCo\ElasticApmAgent\Message\Context;
9
use TechDeCo\ElasticApmAgent\Message\Span;
10
use TechDeCo\ElasticApmAgent\Message\Timestamp;
11
use TechDeCo\ElasticApmAgent\Message\Transaction;
12
use function microtime;
13
14
final class OpenTransaction
15
{
16
    /**
17
     * @var float
18
     */
19
    private $startOfTransaction;
20
21
    /**
22
     * @var UuidInterface
23
     */
24
    private $id;
25
26
    /**
27
     * @var string
28
     */
29
    private $name;
30
31
    /**
32
     * @var Timestamp
33
     */
34
    private $timestamp;
35
36
    /**
37
     * @var Span[]
38
     */
39
    private $spanList = [];
40
41
    /**
42
     * @var string
43
     */
44
    private $type;
45
46
    /**
47
     * @var mixed[]
48
     */
49
    private $markList = [];
50
51
    /**
52
     * @var UuidInterface
53
     */
54
    private $correlationId;
55
56
    /**
57
     * @var Context
58
     */
59
    private $context;
60
61 80
    public function __construct(
62
        UuidInterface $id,
63
        string $name,
64
        Timestamp $timestamp,
65
        string $type,
66
        ?UuidInterface $correlationId = null
67
    ) {
68 80
        $this->id                 = $id;
69 80
        $this->name               = $name;
70 80
        $this->timestamp          = $timestamp;
71 80
        $this->type               = $type;
72 80
        $this->startOfTransaction = microtime(true);
73 80
        $this->correlationId      = $correlationId ?? Uuid::uuid4();
74 80
        $this->context            = (new Context())
75 80
            ->withTag(ContextTags::CORRELATION_ID, $this->correlationId->toString());
76 80
    }
77
78 13
    public function getId(): UuidInterface
79
    {
80 13
        return $this->id;
81
    }
82
83 58
    public function addSpan(Span $span): void
84
    {
85 58
        $this->spanList[] = $span;
86 58
    }
87
88 29
    public function addMark(string $group, string $event, float $timestamp): void
89
    {
90 29
        if (! isset($this->markList[$group])) {
91 29
            $this->markList[$group] = [];
92
        }
93
94 29
        $this->markList[$group][$event] = $timestamp;
95 29
    }
96
97 8
    public function getContext(): Context
98
    {
99 8
        return $this->context;
100
    }
101
102 8
    public function setContext(Context $context): void
103
    {
104 8
        $this->context = $context;
105 8
    }
106
107 34
    public function toTransaction(): Transaction
108
    {
109 34
        $transaction = new Transaction(
110 34
            (microtime(true) - $this->startOfTransaction) * 1000,
111 34
            $this->id,
112 34
            $this->name,
113 34
            $this->timestamp,
114 34
            $this->type
115
        );
116 34
        $transaction = $transaction->withSpan(...$this->spanList)
117 34
                                   ->inContext($this->context);
118
119 34
        foreach ($this->markList as $group => $eventList) {
120 15
            foreach ($eventList as $event => $timestamp) {
121 15
                $transaction = $transaction->marking($group, $event, $timestamp);
122
            }
123
        }
124
125 34
        return $transaction;
126
    }
127
128
    /**
129
     * @return float The offset in microseconds with microsecond precision
130
     */
131 30
    public function getStartOffset(): float
132
    {
133 30
        return (microtime(true) - $this->startOfTransaction) * 1000;
134
    }
135
136 7
    public function getCorrelationId(): UuidInterface
137
    {
138 7
        return $this->correlationId;
139
    }
140
}
141