Passed
Push — semantic-logger ( 66d24d...4aff0d )
by Akihito
02:33
created

DevSemanticInvoker   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A invoke() 0 23 2
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
 * Development semantic invoker with log persistence for MCP integration
17
 *
18
 * Orchestrates semantic logging with immediate file persistence for AI-assisted debugging.
19
 * Follows single responsibility principle by delegating persistence to DevLogPersister.
20
 */
21
final class DevSemanticInvoker implements InvokerInterface
22
{
23
    public function __construct(
24
        #[Named('original')]
25
        private InvokerInterface $invoker,
26
        private SemanticLoggerInterface $logger,
27
        private ContextFactoryInterface $factory,
28
        private DevLogPersister $persister,
29
    ) {
30
    }
31
32
    #[Override]
33
    public function invoke(AbstractRequest $request): ResourceObject
34
    {
35
        $openContext = $this->factory->createOpenContext($request);
36
        $openId = $this->logger->open($openContext);
37
38
        try {
39
            $result = $this->invoker->invoke($request);
40
            $closeContext = $this->factory->createCompleteContext($result, $openContext);
41
            $this->logger->close($closeContext, $openId);
42
43
            // Persist logs after successful completion
44
            $this->persister->persistLogs($this->logger);
45
46
            return $result;
47
        } catch (Throwable $e) {
48
            $errorContext = $this->factory->createErrorContext($e);
49
            $this->logger->close($errorContext, $openId);
50
51
            // Persist logs after error handling
52
            $this->persister->persistLogs($this->logger);
53
54
            throw $e;
55
        }
56
    }
57
}
58