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

OpenContext   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 22
c 3
b 1
f 1
dl 0
loc 52
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A getXdebugId() 0 3 1
A __construct() 0 16 1
A jsonSerialize() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource\SemanticLog\Profile\Verbose;
6
7
use BEAR\Resource\AbstractRequest;
8
use BEAR\Resource\SemanticLog\Profile\XdebugTrace;
9
use BEAR\Resource\SemanticLog\Profile\XHProfResult;
10
use JsonSerializable;
11
use Koriym\SemanticLogger\AbstractContext;
12
use Override;
13
14
use function spl_object_hash;
15
use function strtolower;
16
use function strtoupper;
17
use function ucfirst;
18
use function uniqid;
19
20
final class OpenContext extends AbstractContext implements JsonSerializable
21
{
22
    /** @psalm-suppress InvalidClassConstantType */
23
    public const TYPE = 'bear_resource_request';
24
25
    /** @psalm-suppress InvalidClassConstantType */
26
    public const SCHEMA_URL = 'https://bearsunday.github.io/BEAR.Resource/schemas/open-context.json';
27
28
    public readonly string $method;
29
    public readonly string $uri;
30
    /** @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...
31
    public readonly array $call;
32
33
    /** @var array<string, string|null> */
34
    private static array $xdebugIdMap = [];
35
36
    public function __construct(AbstractRequest $request)
37
    {
38
        $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...
39
        $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...
40
        $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...
41
            $request->resourceObject::class,
42
            'on' . ucfirst(strtolower($request->method))
43
        ];
44
45
        // Start profiling (but don't capture data yet - that's for close)
46
        XHProfResult::start();
47
        XdebugTrace::start();
48
49
        // Generate an ID for profiling context
50
        $xdebugId = uniqid('profile_', true);
51
        self::$xdebugIdMap[spl_object_hash($this)] = $xdebugId;
52
    }
53
54
    public static function create(AbstractRequest $request): self
55
    {
56
        return new self($request);
57
    }
58
59
    public function getXdebugId(): ?string
60
    {
61
        return self::$xdebugIdMap[spl_object_hash($this)] ?? null;
62
    }
63
64
    /** @return array<string, mixed> */
65
    #[Override]
66
    public function jsonSerialize(): array
67
    {
68
        return [
69
            'method' => $this->method,
70
            'uri' => $this->uri,
71
            'call' => $this->call,
72
        ];
73
    }
74
}
75