Passed
Push — master ( d81171...ddb4d4 )
by Frank
02:35
created

Context::withUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TechDeCo\ElasticApmAgent\Message;
5
6
use JsonSerializable;
7
use TechDeCo\ElasticApmAgent\Serialization;
8
9
final class Context implements JsonSerializable
10
{
11
    /**
12
     * @var mixed[]|null
13
     */
14
    private $custom;
15
16
    /**
17
     * @var Response|null
18
     */
19
    private $response;
20
21
    /**
22
     * @var Request|null
23
     */
24
    private $request;
25
26
    /**
27
     * @var string[]
28
     */
29
    private $tagList = [];
30
31
    /**
32
     * @var User|null
33
     */
34
    private $user;
35
36
    /**
37
     * @param mixed $value
38
     */
39 1
    public function withCustomVariable(string $name, $value): self
40
    {
41 1
        $me                = clone $this;
42 1
        $me->custom[$name] = $value;
43
44 1
        return $me;
45
    }
46
47 1
    public function withResponse(Response $response): self
48
    {
49 1
        $me           = clone $this;
50 1
        $me->response = $response;
51
52 1
        return $me;
53
    }
54
55 13
    public function withRequest(Request $request): self
56
    {
57 13
        $me          = clone $this;
58 13
        $me->request = $request;
59
60 13
        return $me;
61
    }
62
63 37
    public function withTag(string $tag, string $value): self
64
    {
65 37
        $me                = clone $this;
66 37
        $me->tagList[$tag] = $value;
67
68 37
        return $me;
69
    }
70
71 1
    public function withUser(User $user): self
72
    {
73 1
        $me       = clone $this;
74 1
        $me->user = $user;
75
76 1
        return $me;
77
    }
78
79
    /**
80
     * @return mixed[]
81
     */
82 46
    public function jsonSerialize(): array
83
    {
84 46
        return Serialization::filterUnset([
85 46
            'custom' => $this->custom,
86 46
            'response' => Serialization::serializeOr($this->response),
87 46
            'request' => Serialization::serializeOr($this->request),
88 46
            'tags' => $this->tagList,
89 46
            'user' => Serialization::serializeOr($this->user),
90
        ]);
91
    }
92
}
93