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

ErrorContext   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 64
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A jsonSerialize() 0 7 1
A createExceptionId() 0 6 1
A create() 0 6 1
A __construct() 0 25 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource\SemanticLog\Profile\Verbose;
6
7
use JsonSerializable;
8
use Koriym\SemanticLogger\AbstractContext;
9
use Koriym\SemanticLogger\Profiler\PhpProfile;
10
use Koriym\SemanticLogger\Profiler\Profile;
11
use Koriym\SemanticLogger\Profiler\XdebugTrace;
12
use Koriym\SemanticLogger\Profiler\XHProfResult;
13
use Override;
14
use Throwable;
15
16
use function crc32;
17
use function dechex;
18
19
final class ErrorContext extends AbstractContext implements JsonSerializable
20
{
21
    /** @psalm-suppress InvalidClassConstantType */
22
    public const TYPE = 'bear_resource_error';
23
24
    /** @psalm-suppress InvalidClassConstantType */
25
    public const SCHEMA_URL = 'https://bearsunday.github.io/BEAR.Resource/schemas/error-context.json';
26
27
    public readonly string $exceptionId;
28
    public readonly string $exceptionAsString;
29
    public readonly Profile $profile;
30
31
    public function __construct(
32
        Throwable $exception,
33
        string $exceptionId = '',
34
        ?OpenContext $openContext = null,
35
    ) {
36
        $this->exceptionAsString = (string) $exception;
0 ignored issues
show
Bug introduced by
The property exceptionAsString is declared read-only in BEAR\Resource\SemanticLo...le\Verbose\ErrorContext.
Loading history...
37
        $this->exceptionId = $exceptionId !== '' ? $exceptionId : $this->createExceptionId();
0 ignored issues
show
Bug introduced by
The property exceptionId is declared read-only in BEAR\Resource\SemanticLo...le\Verbose\ErrorContext.
Loading history...
38
39
        if ($openContext === null) {
40
            // Create empty profile if no open context provided
41
            $this->profile = new Profile();
0 ignored issues
show
Bug introduced by
The property profile is declared read-only in BEAR\Resource\SemanticLo...le\Verbose\ErrorContext.
Loading history...
42
43
            return;
44
        }
45
46
        // Stop profiling and capture final profile data
47
        // Note: start() was called in OpenContext, now we stop and capture data
48
        $xhprofResult = (new XHProfResult())->stop($openContext->uri);
49
        $xdebugTrace = (new XdebugTrace())->stop();
50
        $phpProfile = PhpProfile::capture();
51
52
        $this->profile = new Profile(
53
            xhprof: $xhprofResult,
54
            xdebug: $xdebugTrace,
55
            php: $phpProfile,
56
        );
57
    }
58
59
    public static function create(
60
        Throwable $exception,
61
        string $exceptionId = '',
62
        ?OpenContext $openContext = null,
63
    ): self {
64
        return new self($exception, $exceptionId, $openContext);
65
    }
66
67
    private function createExceptionId(): string
68
    {
69
        $crc = crc32($this->exceptionAsString);
70
        $crcHex = dechex($crc & 0xFFFFFFFF); // Ensure positive hex value
71
72
        return 'e-bear-resource-' . $crcHex;
73
    }
74
75
    /** @return array<string, mixed> */
76
    #[Override]
77
    public function jsonSerialize(): array
78
    {
79
        return [
80
            'exceptionId' => $this->exceptionId,
81
            'exceptionAsString' => $this->exceptionAsString,
82
            'profile' => $this->profile,
83
        ];
84
    }
85
}
86