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

CompleteContext   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 28 1
A jsonSerialize() 0 10 1
A create() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource\SemanticLog\Profile\Verbose;
6
7
use BEAR\Resource\ResourceObject;
8
use BEAR\Resource\Types;
9
use JsonSerializable;
10
use Koriym\SemanticLogger\AbstractContext;
11
use Koriym\SemanticLogger\Profiler\PhpProfile;
12
use Koriym\SemanticLogger\Profiler\Profile;
13
use Koriym\SemanticLogger\Profiler\XdebugTrace;
14
use Koriym\SemanticLogger\Profiler\XHProfResult;
15
use Override;
16
17
/**
18
 * @psalm-import-type Headers from Types
19
 * @psalm-import-type HttpBody from Types
20
 */
21
final class CompleteContext extends AbstractContext implements JsonSerializable
22
{
23
    /** @psalm-suppress InvalidClassConstantType */
24
    public const TYPE = 'bear_resource_complete';
25
26
    /** @psalm-suppress InvalidClassConstantType */
27
    public const SCHEMA_URL = 'https://bearsunday.github.io/BEAR.Resource/schemas/complete-context.json';
28
29
    public readonly string $uri;
30
    public readonly int $code;
31
32
    /** @var Headers */
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\SemanticLog\Profile\Verbose\Headers was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
    public readonly array $headers;
34
35
    /** @var HttpBody  */
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\SemanticLog\Profile\Verbose\HttpBody was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
    public readonly mixed $body;
37
    public readonly string $view;
38
    public readonly Profile $profile;
39
40
    public function __construct(ResourceObject $resource, OpenContext $openContext)
41
    {
42
        // Trigger rendering to get view
43
        $resourceString = (string) $resource;
44
        unset($resourceString);
45
        $this->uri = (string) $resource->uri;
0 ignored issues
show
Bug introduced by
The property uri is declared read-only in BEAR\Resource\SemanticLo...Verbose\CompleteContext.
Loading history...
46
        $this->code = $resource->code;
0 ignored issues
show
Bug introduced by
The property code is declared read-only in BEAR\Resource\SemanticLo...Verbose\CompleteContext.
Loading history...
47
        $this->headers = $resource->headers;
0 ignored issues
show
Bug introduced by
The property headers is declared read-only in BEAR\Resource\SemanticLo...Verbose\CompleteContext.
Loading history...
48
        /** @psalm-suppress MixedAssignment */
49
        /** @phpstan-ignore-next-line */
50
        $this->body = $resource->body;
0 ignored issues
show
Bug introduced by
The property body is declared read-only in BEAR\Resource\SemanticLo...Verbose\CompleteContext.
Loading history...
51
        /** @psalm-suppress PossiblyNullPropertyAssignmentValue */
52
        $this->view = $resource->view ?? '';
0 ignored issues
show
Bug introduced by
The property view is declared read-only in BEAR\Resource\SemanticLo...Verbose\CompleteContext.
Loading history...
53
54
        // Stop profiling and capture final profile data
55
        // Note: start() was called in OpenContext, now we stop and capture data
56
        $xhprofResult = (new XHProfResult())->stop($this->uri);
57
        $xdebugTrace = (new XdebugTrace())->stop();
58
        $phpProfile = PhpProfile::capture();
59
60
        $this->profile = new Profile(
0 ignored issues
show
Bug introduced by
The property profile is declared read-only in BEAR\Resource\SemanticLo...Verbose\CompleteContext.
Loading history...
61
            xhprof: $xhprofResult,
62
            xdebug: $xdebugTrace,
63
            php: $phpProfile,
64
        );
65
66
        // OpenContext is used for profiling coordination but not stored
67
        unset($openContext);
68
    }
69
70
    public static function create(ResourceObject $resource, OpenContext $openContext): self
71
    {
72
        return new self($resource, $openContext);
73
    }
74
75
    /** @return array<string, mixed> */
76
    #[Override]
77
    public function jsonSerialize(): array
78
    {
79
        return [
80
            'uri' => $this->uri,
81
            'code' => $this->code,
82
            'headers' => $this->headers,
83
            'body' => $this->body,
84
            'view' => $this->view,
85
            'profile' => $this->profile,
86
        ];
87
    }
88
}
89