Passed
Push — master ( 55a312...57bd4c )
by Zoilo
01:55
created

Transaction::assertResult()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3.3332

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
crap 3.3332
rs 10
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Events\Transaction;
4
5
use ZoiloMora\ElasticAPM\Events\Common\Context;
6
use ZoiloMora\ElasticAPM\Events\Common\TimestampEpoch;
7
use ZoiloMora\ElasticAPM\Events\Common\TransactionName;
8
use ZoiloMora\ElasticAPM\Events\Common\TransactionType;
9
use ZoiloMora\ElasticAPM\Events\Duration;
10
use ZoiloMora\ElasticAPM\Events\TraceableEvent;
11
use ZoiloMora\ElasticAPM\Helper\Encoding;
12
use ZoiloMora\ElasticAPM\Helper\Stopwatch\Stopwatch;
13
14
/**
15
 * An event corresponding to an incoming request or similar task occurring in a monitored service
16
 */
17
final class Transaction extends TraceableEvent
18
{
19
    use TimestampEpoch;
20
    use TransactionName;
21
    use TransactionType;
22
    use Duration;
23
24
    /**
25
     * @var SpanCount
26
     */
27
    private $spanCount;
28
29
    /**
30
     * Any arbitrary contextual information regarding the event, captured by the agent, optionally provided by the user
31
     *
32
     * @var Context
33
     */
34
    private $context;
35
36
    /**
37
     * The result of the transaction.
38
     * For HTTP-related transactions, this should be the status code formatted like 'HTTP 2xx'.
39
     *
40
     * @var string|null
41
     */
42
    private $result;
43
44
    /**
45
     * @param string $name
46
     * @param string $type
47
     * @param Context $context
48
     * @param string|null $traceId
49
     * @param string|null $parentId
50
     *
51
     * @throws \Exception
52
     */
53 10
    public function __construct(
54
        $name,
55
        $type,
56
        Context $context,
57
        $traceId = null,
58
        $parentId = null
59
    ) {
60 10
        parent::__construct($traceId, $parentId);
61
62 10
        $this->timestamp = $this->generateTimestamp();
63 10
        $this->name = $name;
64 10
        $this->type = $type;
65 10
        $this->context = $context;
66
67 10
        $this->spanCount = new SpanCount(0, 0);
68 10
        $this->stopwatch = new Stopwatch();
69 10
        $this->stopwatch->start($this->timestamp);
70 10
    }
71
72
    /**
73
     * @param string|null $result
74
     *
75
     * @return void
76
     *
77
     * @throws \ZoiloMora\ElasticAPM\Helper\Stopwatch\Exception\NotStartedException
78
     * @throws \ZoiloMora\ElasticAPM\Helper\Stopwatch\Exception\NotStoppedException
79
     */
80 3
    public function stop($result = null)
81
    {
82 3
        $this->stopClock();
83
84 3
        $this->assertResult($result);
85 3
        $this->result = $result;
86 3
    }
87
88
    /**
89
     * @param mixed $result
90
     *
91
     * @return void
92
     *
93
     * @throws \InvalidArgumentException
94
     */
95 3
    private function assertResult($result)
96
    {
97 3
        if (null !== $result && false === is_string($result)) {
98
            throw new \InvalidArgumentException('The [result] must be of type string or null.');
99
        }
100 3
    }
101
102
    /**
103
     * @return Context
104
     */
105 1
    public function context()
106
    {
107 1
        return $this->context;
108
    }
109
110
    /**
111
     * @return array
112
     */
113 1
    public function jsonSerialize()
114
    {
115
        return [
116 1
            'transaction' => array_merge(
117 1
                parent::jsonSerialize(),
118
                [
119 1
                    'timestamp' => $this->timestamp,
120 1
                    'name' => $this->getEncodingName(),
121 1
                    'type' => $this->getEncodingType(),
122 1
                    'span_count' => $this->spanCount,
123 1
                    'context' => $this->context,
124 1
                    'duration' => $this->duration,
125 1
                    'result' => Encoding::keywordField($this->result),
126
                ]
127 1
            )
128 1
        ];
129
    }
130
}
131