Passed
Pull Request — master (#19)
by Frank
06:39
created

Request::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 9.8333
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TechDeCo\ElasticApmAgent\Message;
5
6
use JsonSerializable;
7
use Psr\Http\Message\RequestInterface;
8
use TechDeCo\ElasticApmAgent\Serialization;
9
10
final class Request implements JsonSerializable
11
{
12
    /**
13
     * @var mixed[]|string|null
14
     */
15
    private $body;
16
17
    /**
18
     * @var mixed[]
19
     */
20
    private $environment = [];
21
22
    /**
23
     * @var mixed[]
24
     */
25
    private $headerList = [];
26
27
    /**
28
     * @var string|null
29
     */
30
    private $httpVersion;
31
32
    /**
33
     * @var string
34
     */
35
    private $method;
36
37
    /**
38
     * @var Socket|null
39
     */
40
    private $socket;
41
42
    /**
43
     * @var Url
44
     */
45
    private $url;
46
47
    /**
48
     * @var string[]
49
     */
50
    private $cookieList = [];
51
52 21
    public function __construct(string $method, Url $url)
53
    {
54 21
        $this->method = $method;
55 21
        $this->url    = $url;
56 21
    }
57
58 4
    public static function fromHttpRequest(RequestInterface $httpRequest): self
59
    {
60 4
        return new self(
61 4
            $httpRequest->getMethod(),
62 4
            Url::fromUri($httpRequest->getUri())
63
        );
64
    }
65
66
    /**
67
     * @param mixed[]|string $body
68
     */
69 2
    public function withBody($body): self
70
    {
71 2
        $me       = clone $this;
72 2
        $me->body = $body;
73
74 2
        return $me;
75
    }
76
77
    /**
78
     * @param mixed $value
79
     */
80 1
    public function withEnvironmentVariable(string $name, $value): self
81
    {
82 1
        $me                     = clone $this;
83 1
        $me->environment[$name] = $value;
84
85 1
        return $me;
86
    }
87
88
    /**
89
     * @param mixed $value
90
     */
91 5
    public function withHeader(string $name, $value): self
92
    {
93 5
        $me                    = clone $this;
94 5
        $me->headerList[$name] = $value;
95
96 5
        return $me;
97
    }
98
99 14
    public function onHttpVersion(string $httpVersion): self
100
    {
101 14
        $me              = clone $this;
102 14
        $me->httpVersion = $httpVersion;
103
104 14
        return $me;
105
    }
106
107 1
    public function onSocket(Socket $socket): self
108
    {
109 1
        $me         = clone $this;
110 1
        $me->socket = $socket;
111
112 1
        return $me;
113
    }
114
115 1
    public function withCookie(string $key, string $value): self
116
    {
117 1
        $me                   = clone $this;
118 1
        $me->cookieList[$key] = $value;
119
120 1
        return $me;
121
    }
122
123
    /**
124
     * @return mixed[]
125
     */
126 19
    public function jsonSerialize(): array
127
    {
128 19
        return Serialization::filterUnset([
129 19
            'body' => $this->body,
130 19
            'env' => $this->environment,
131 19
            'headers' => $this->headerList,
132 19
            'http_version' => $this->httpVersion,
133 19
            'method' => $this->method,
134 19
            'socket' => Serialization::serializeOr($this->socket),
135 19
            'url' => $this->url->jsonSerialize(),
136 19
            'cookies' => $this->cookieList,
137
        ]);
138
    }
139
}
140