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

DevSemanticInvoker::invoke()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 14
c 1
b 0
f 1
nc 5
nop 1
dl 0
loc 23
rs 9.7998
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\DevLogger;
11
use Koriym\SemanticLogger\SemanticLoggerInterface;
12
use Override;
13
use Ray\Di\Di\Named;
14
use Throwable;
15
16
/**
17
 * Development semantic invoker with log persistence for MCP integration
18
 *
19
 * Orchestrates semantic logging with immediate file persistence for AI-assisted debugging.
20
 * Follows single responsibility principle by delegating persistence to vendor DevLogger.
21
 */
22
final class DevSemanticInvoker implements InvokerInterface
23
{
24
    public function __construct(
25
        #[Named('original')]
26
        private InvokerInterface $invoker,
27
        private SemanticLoggerInterface $logger,
28
        private ContextFactoryInterface $factory,
29
        private DevLogger $devLogger,
30
    ) {
31
    }
32
33
    #[Override]
34
    public function invoke(AbstractRequest $request): ResourceObject
35
    {
36
        $openContext = $this->factory->createOpenContext($request);
37
        $openId = $this->logger->open($openContext);
38
39
        try {
40
            $result = $this->invoker->invoke($request);
41
            $closeContext = $this->factory->createCompleteContext($result, $openContext);
42
            $this->logger->close($closeContext, $openId);
43
44
            // Persist logs after successful completion
45
            $this->devLogger->log($this->logger);
46
47
            return $result;
48
        } catch (Throwable $e) {
49
            $errorContext = $this->factory->createErrorContext($e);
50
            $this->logger->close($errorContext, $openId);
51
52
            // Persist logs after error handling
53
            $this->devLogger->log($this->logger);
54
55
            throw $e;
56
        }
57
    }
58
}
59