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

SemanticResourceInvokerAdapter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
eloc 28
c 1
b 0
f 1
dl 0
loc 58
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A invoke() 0 49 4
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource\SemanticLog;
6
7
use BEAR\Resource\AbstractRequest;
8
use BEAR\Resource\InvokerInterface;
9
use BEAR\Resource\ResourceObject;
10
use Koriym\SemanticLogger\SemanticLoggerInterface;
0 ignored issues
show
Bug introduced by
The type Koriym\SemanticLogger\SemanticLoggerInterface 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...
11
use Override;
12
use Ray\Di\Di\Named;
13
use Throwable;
14
15
use function array_merge;
16
use function get_class;
17
use function is_array;
18
use function property_exists;
19
use function strtolower;
20
use function ucfirst;
21
22
final class SemanticResourceInvokerAdapter implements InvokerInterface
23
{
24
    public function __construct(
25
        #[Named('Original')]
26
        private InvokerInterface $invoker,
27
        private SemanticLoggerInterface $semanticLogger,
28
    ) {
29
    }
30
31
    #[Override]
32
    public function invoke(AbstractRequest $request): ResourceObject
33
    {
34
        // Use resource class name for better traceability
35
        $resourceClass = get_class($request->resourceObject);
36
37
        // Convert HTTP method to resource method (GET -> onGet)
38
        $resourceMethod = 'on' . ucfirst(strtolower($request->method));
39
40
        // Combine query and body parameters for complete parameter context
41
        $parameters = array_merge(
42
            $request->query,
43
            property_exists($request, 'body') && is_array($request->body) ? $request->body : [],
44
        );
45
46
        /** @var array<string, mixed> $parameters */
47
        $context = new ResourceOpenContext(
48
            $resourceClass,
49
            $resourceMethod,
50
            $parameters,
51
        );
52
53
        $openId = $this->semanticLogger->open($context);
54
55
        try {
56
            $result = $this->invoker->invoke($request);
57
58
            $closeContext = new ResourceCompleteContext(
59
                $resourceClass,
60
                $resourceMethod,
61
                $result->code,
62
                /** @var array<string, mixed> */
63
                (array) $result->body,
64
            );
65
66
            $this->semanticLogger->close($closeContext, $openId);
67
68
            return $result;
69
        } catch (Throwable $e) {
70
            $errorContext = new ResourceErrorContext(
71
                $resourceClass,
72
                $resourceMethod,
73
                $e::class,
74
                $e->getMessage(),
75
            );
76
77
            $this->semanticLogger->close($errorContext, $openId);
78
79
            throw $e;
80
        }
81
    }
82
}
83