SemanticInvoker   A
last analyzed

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
/**
16
 * SemanticInvoker for BEAR.Resource with structured logging
17
 *
18
 * Wraps resource requests with semantic logging using open/close lifecycle.
19
 * Currently accumulates logs via SemanticLogger, retrieved only via flush() in tests.
20
 *
21
 * TODO: Consider adding immediate log persistence when close() is called
22
 * for MCP server integration and AI-assisted debugging.
23
 */
24
final class SemanticInvoker implements InvokerInterface
25
{
26
    public function __construct(
27
        #[Named('original')]
28
        private InvokerInterface $invoker,
29
        private SemanticLoggerInterface $logger,
30
        private ContextFactoryInterface $factory,
31
    ) {
32
    }
33
34
    #[Override]
35
    public function invoke(AbstractRequest $request): ResourceObject
36
    {
37
        $openContext = $this->factory->createOpenContext($request);
38
39
        $openId = $this->logger->open($openContext);
40
41
        try {
42
            $result = $this->invoker->invoke($request);
43
44
            $closeContext = $this->factory->createCompleteContext($result, $openContext);
45
46
            $this->logger->close($closeContext, $openId);
47
48
            return $result;
49
        } catch (Throwable $e) {
50
            $errorContext = $this->factory->createErrorContext($e);
51
52
            $this->logger->close($errorContext, $openId);
53
54
            throw $e;
55
        }
56
    }
57
}
58