Exception   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 120
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1
ccs 40
cts 40
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A withCode() 0 7 1
A inModule() 0 7 1
A withAttribute() 0 7 1
A withStackTraceFrame() 0 7 1
A asType() 0 7 1
A thatIsHandled() 0 7 1
A thatIsNotHandled() 0 7 1
A jsonSerialize() 0 12 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TechDeCo\ElasticApmAgent\Message;
5
6
use JsonSerializable;
7
use TechDeCo\ElasticApmAgent\Serialization;
8
use function array_merge;
9
10
final class Exception implements JsonSerializable
11
{
12
    /**
13
     * @var string|int|null
14
     */
15
    private $code;
16
17
    /**
18
     * @var string
19
     */
20
    private $message;
21
22
    /**
23
     * @var string
24
     */
25
    private $module;
26
27
    /**
28
     * @var mixed[]
29
     */
30
    private $attributeList = [];
31
32
    /**
33
     * @var StackTraceFrame[]
34
     */
35
    private $stackTraceFrameList = [];
36
37
    /**
38
     * @var string|null
39
     */
40
    private $type;
41
42
    /**
43
     * @var bool|null
44
     */
45
    private $isHandled;
46
47 18
    public function __construct(string $message)
48
    {
49 18
        $this->message = $message;
50 18
    }
51
52
    /**
53
     * @param string|int $code
54
     */
55 14
    public function withCode($code): self
56
    {
57 14
        $me       = clone $this;
58 14
        $me->code = $code;
59
60 14
        return $me;
61
    }
62
63 1
    public function inModule(string $module): self
64
    {
65 1
        $me         = clone $this;
66 1
        $me->module = $module;
67
68 1
        return $me;
69
    }
70
71
    /**
72
     * @param mixed $value
73
     */
74 1
    public function withAttribute(string $name, $value): self
75
    {
76 1
        $me                       = clone $this;
77 1
        $me->attributeList[$name] = $value;
78
79 1
        return $me;
80
    }
81
82 14
    public function withStackTraceFrame(StackTraceFrame ...$frame): self
83
    {
84 14
        $me                      = clone $this;
85 14
        $me->stackTraceFrameList = array_merge($me->stackTraceFrameList, $frame);
86
87 14
        return $me;
88
    }
89
90 14
    public function asType(string $type): self
91
    {
92 14
        $me       = clone $this;
93 14
        $me->type = $type;
94
95 14
        return $me;
96
    }
97
98 1
    public function thatIsHandled(): self
99
    {
100 1
        $me            = clone $this;
101 1
        $me->isHandled = true;
102
103 1
        return $me;
104
    }
105
106 1
    public function thatIsNotHandled(): self
107
    {
108 1
        $me            = clone $this;
109 1
        $me->isHandled = false;
110
111 1
        return $me;
112
    }
113
114
    /**
115
     * @return mixed[]
116
     */
117 17
    public function jsonSerialize(): array
118
    {
119 17
        return Serialization::filterUnset([
120 17
            'code' => $this->code,
121 17
            'message' => $this->message,
122 17
            'module' => $this->module,
123 17
            'attributes' => $this->attributeList,
124 17
            'stacktrace' => Serialization::serialize(...$this->stackTraceFrameList),
125 17
            'type' => $this->type,
126 17
            'handled' => $this->isHandled,
127
        ]);
128
    }
129
}
130