Passed
Pull Request — 1.x (#334)
by Akihito
12:50 queued 10:32
created

SemanticInvoker   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A invoke() 0 21 2
A __construct() 0 6 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
final class SemanticInvoker implements InvokerInterface
16
{
17
    public function __construct(
18
        #[Named('original')]
19
        private InvokerInterface $invoker,
20
        private SemanticLoggerInterface $logger,
21
        private ContextFactoryInterface $factory,
22
    ) {
23
    }
24
25
    #[Override]
26
    public function invoke(AbstractRequest $request): ResourceObject
27
    {
28
        $openContext = $this->factory->createOpenContext($request);
29
30
        $openId = $this->logger->open($openContext);
31
32
        try {
33
            $result = $this->invoker->invoke($request);
34
35
            $closeContext = $this->factory->createCompleteContext($result, $openContext);
36
37
            $this->logger->close($closeContext, $openId);
38
39
            return $result;
40
        } catch (Throwable $e) {
41
            $errorContext = $this->factory->createErrorContext($e);
42
43
            $this->logger->close($errorContext, $openId);
44
45
            throw $e;
46
        }
47
    }
48
}
49