Passed
Pull Request — 1.x (#334)
by Akihito
02:32
created

OpenContext::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 1
eloc 5
c 3
b 1
f 1
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource\SemanticLog\Profile\Verbose;
6
7
use BEAR\Resource\AbstractRequest;
8
use JsonSerializable;
9
use Koriym\SemanticLogger\AbstractContext;
10
use Override;
11
12
use function strtolower;
13
use function strtoupper;
14
use function ucfirst;
15
use function uniqid;
16
17
final class OpenContext extends AbstractContext implements JsonSerializable
18
{
19
    /** @psalm-suppress InvalidClassConstantType */
20
    public const TYPE = 'bear_resource_request';
21
22
    /** @psalm-suppress InvalidClassConstantType */
23
    public const SCHEMA_URL = 'https://bearsunday.github.io/BEAR.Resource/schemas/open-context.json';
24
25
    public readonly string $method;
26
    public readonly string $uri;
27
28
    /** @var array{string, string} */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{string, string} at position 2 could not be parsed: Expected ':' at position 2, but found 'string'.
Loading history...
29
    public readonly array $call;
30
31
    public function __construct(AbstractRequest $request)
32
    {
33
        $this->method = strtoupper($request->method);
0 ignored issues
show
Bug introduced by
The property method is declared read-only in BEAR\Resource\SemanticLo...ile\Verbose\OpenContext.
Loading history...
34
        $this->uri = $request->toUri();
0 ignored issues
show
Bug introduced by
The property uri is declared read-only in BEAR\Resource\SemanticLo...ile\Verbose\OpenContext.
Loading history...
35
        $this->call = [
0 ignored issues
show
Bug introduced by
The property call is declared read-only in BEAR\Resource\SemanticLo...ile\Verbose\OpenContext.
Loading history...
36
            $request->resourceObject::class,
37
            'on' . ucfirst(strtolower($request->method)),
38
        ];
39
40
        // Start XdebugTrace for profiling (handled internally)
41
    }
42
43
    public static function create(AbstractRequest $request): self
44
    {
45
        return new self($request);
46
    }
47
48
    public function getXdebugId(): string
49
    {
50
        return uniqid('profile_', true);
51
    }
52
53
    /** @return array<string, mixed> */
54
    #[Override]
55
    public function jsonSerialize(): array
56
    {
57
        return [
58
            'method' => $this->method,
59
            'uri' => $this->uri,
60
            'call' => $this->call,
61
        ];
62
    }
63
}
64