Passed
Pull Request — 1.x (#334)
by Akihito
11:25
created

SemanticResourceInvokerAdapter::invoke()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 24
c 1
b 0
f 1
dl 0
loc 39
rs 9.536
cc 2
nc 4
nop 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;
11
use Override;
12
use Ray\Di\Di\Named;
13
use Throwable;
14
15
use function strtoupper;
16
17
final class SemanticResourceInvokerAdapter implements InvokerInterface
18
{
19
    public function __construct(
20
        #[Named('Original')]
21
        private InvokerInterface $invoker,
22
        private SemanticLoggerInterface $semanticLogger,
23
    ) {
24
    }
25
26
    #[Override]
27
    public function invoke(AbstractRequest $request): ResourceObject
28
    {
29
        // Use URI and method for better user understanding
30
        $uri = $request->toUri();
31
        $method = strtoupper($request->method);
32
33
        $query = $request->query;
0 ignored issues
show
Documentation Bug introduced by
It seems like $request->query of type array is incompatible with the declared type BEAR\Resource\Query of property $query.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
35
        $context = new ResourceOpenContext(
36
            $uri,
37
            $method,
38
            $query,
39
        );
40
41
        $openId = $this->semanticLogger->open($context);
42
43
        try {
44
            $result = $this->invoker->invoke($request);
45
46
            $closeContext = new ResourceCompleteContext(
47
                $result,
48
                $method,
49
            );
50
51
            $this->semanticLogger->close($closeContext, $openId);
52
53
            return $result;
54
        } catch (Throwable $e) {
55
            $errorContext = new ResourceErrorContext(
56
                $uri,
57
                $method,
58
                $e::class,
59
                $e->getMessage(),
0 ignored issues
show
Unused Code introduced by
The call to BEAR\Resource\SemanticLo...rContext::__construct() has too many arguments starting with $e->getMessage(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
            $errorContext = /** @scrutinizer ignore-call */ new ResourceErrorContext(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
60
            );
61
62
            $this->semanticLogger->close($errorContext, $openId);
63
64
            throw $e;
65
        }
66
    }
67
}
68