Transaction::ignoreApdex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
namespace ZERO2TEN\Observability\APM;
4
5
use Throwable;
6
7
/**
8
 * Transaction
9
 *
10
 * This class exposes specific methods of the AgentInterface that allows
11
 * customization of the (current) transaction.
12
 *
13
 * @copyright Copyright (c) 2023 0TO10 B.V. <https://0to10.nl>
14
 * @package ZERO2TEN\Observability\APM
15
 */
16
final class Transaction implements TransactionInterface
17
{
18
    /** @var AgentInterface */
19
    private $agent;
20
21
    /**
22
     * Indicates if the current transaction was ignored.
23
     *
24
     * @var bool
25
     */
26
    private $ignored = false;
27
28
    /**
29
     * List of custom parameters that were added to the current transaction.
30
     *
31
     * Note that this will only contain parameters that were added via this
32
     * class - manually added transactions will not be included.
33
     *
34
     * @var array<string, mixed>
35
     */
36
    private $parameters = [];
37
38
    /**
39
     * @param AgentInterface $agent
40
     * @constructor
41
     */
42 1
    public function __construct(AgentInterface $agent)
43
    {
44 1
        $this->agent = $agent;
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50 4
    public function start(bool $ignorePrevious = false): TransactionInterface
51
    {
52 4
        if ($ignorePrevious) {
53 2
            $this->ignore();
54
        }
55
56 4
        $this->end();
57
58 4
        return $this->agent->startTransaction();
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64 1
    public function changeName(string $name): TransactionInterface
65
    {
66 1
        $this->agent->changeTransactionName($name);
67 1
        return $this;
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73 1
    public function addParameter(string $name, $value): TransactionInterface
74
    {
75 1
        $this->agent->addTransactionParameter($name, $value);
76 1
        $this->parameters[$name] = $value;
77
78 1
        return $this;
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84 2
    public function addParameters(array $parameters): TransactionInterface
85
    {
86 2
        foreach ($parameters as $name => $value) {
87 1
            $this->addParameter($name, $value);
88
        }
89
90 2
        return $this;
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96 4
    public function getParameters(): array
97
    {
98 4
        return $this->parameters;
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104 2
    public function markAsBackground(bool $background): TransactionInterface
105
    {
106 2
        $this->agent->markTransactionAsBackground($background);
107 2
        return $this;
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113 1
    public function recordException(Throwable $e): TransactionInterface
114
    {
115 1
        $this->agent->recordTransactionException($e->getMessage(), $e);
116 1
        return $this;
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122 6
    public function addDatastoreSegment(
123
        Datastore $datastore,
124
        callable $callable,
125
        string $query = null,
126
        string $inputQueryLabel = null,
127
        string $inputQuery = null
128
    )
129
    {
130 6
        return $this->agent->addTransactionDatastoreSegment(
131 6
            $datastore,
132 6
            $callable,
133 6
            $query,
134 6
            $inputQueryLabel,
135 6
            $inputQuery
136 6
        );
137
    }
138
139
    /**
140
     * @inheritDoc
141
     */
142 1
    public function stopTiming(): TransactionInterface
143
    {
144 1
        $this->agent->stopTransactionTiming();
145 1
        return $this;
146
    }
147
148
    /**
149
     * @inheritDoc
150
     */
151 1
    public function ignoreApdex(): TransactionInterface
152
    {
153 1
        $this->agent->ignoreTransactionApdex();
154 1
        return $this;
155
    }
156
157
    /**
158
     * @inheritDoc
159
     */
160 1
    public function ignore(): TransactionInterface
161
    {
162 1
        $this->agent->ignoreTransaction();
163 1
        $this->ignored = true;
164
165 1
        return $this;
166
    }
167
168
    /**
169
     * @inheritDoc
170
     */
171 1
    public function isIgnored(): bool
172
    {
173 1
        return $this->ignored;
174
    }
175
176
    /**
177
     * @inheritDoc
178
     */
179 2
    public function isSampled(): bool
180
    {
181 2
        return $this->agent->isTransactionSampled();
182
    }
183
184
    /**
185
     * @inheritDoc
186
     */
187 1
    public function end(): TransactionInterface
188
    {
189 1
        $this->agent->endTransaction();
190 1
        return $this;
191
    }
192
193
    /**
194
     * @inheritDoc
195
     */
196 1
    public function isEnded(): bool
197
    {
198 1
        return $this->agent->isTransactionEnded();
199
    }
200
}
201