Lines 84.21% 16 / 19
Functions and Methods 75.00% 6 / 8
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ExposedHandleRegistry 84.21% 16 / 19 75.00% 6 / 8 13.67 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 isKeyTaken 100.00% 1 / 1 100.00% 1 / 1 1
 recordHandle 100.00% 1 / 1 100.00% 1 / 1 1
 forgetHandle 0.00% 0 / 1 0.00% 0 / 1 2
 findHandle 100.00% 1 / 1 100.00% 1 / 1 1
 applySnapshot 100.00% 1 / 1 100.00% 1 / 1 1
 releaseHandlesOf 77.77% 7 / 9 0.00% 0 / 1 4.18
 releaseAll 100.00% 4 / 4 100.00% 1 / 1 3
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Worker\Exposure;
6
7use Psr\Log\LoggerInterface;
8use Stewart\Contracts\Exposure\ExposedEntityKey;
9use Stewart\Contracts\Exposure\ExposedEntitySnapshot;
10use Stewart\Runtime\Exception\TransportException;
11use Stewart\Runtime\Ipc\Message\ExposuresReleased;
12use Stewart\Runtime\Ipc\Transport;
13use Stewart\Runtime\Model\ResourceScope;
14
15final class ExposedHandleRegistry
16{
17    /** @var array<string, array<string, ExposedHandle>> */
18    private array $handlesByScope = [];
19
20    public function __construct(
21        private readonly Transport $transport,
22        private readonly LoggerInterface $logger,
23    ) {}
24
25    public function isKeyTaken(ResourceScope $scope, ExposedEntityKey $key): bool
26    {
27        return isset($this->handlesByScope[$scope->wireValue()][$key->value]);
28    }
29
30    public function recordHandle(ResourceScope $scope, ExposedEntityKey $key, ExposedHandle $handle): void
31    {
32        $this->handlesByScope[$scope->wireValue()][$key->value] = $handle;
33    }
34
35    public function forgetHandle(ResourceScope $scope, ExposedEntityKey $key): void
36    {
37        unset($this->handlesByScope[$scope->wireValue()][$key->value]);
38    }
39
40    public function findHandle(ResourceScope $scope, ExposedEntityKey $key): ?ExposedHandle
41    {
42        return $this->handlesByScope[$scope->wireValue()][$key->value] ?? null;
43    }
44
45    public function applySnapshot(ResourceScope $scope, ExposedEntityKey $key, ExposedEntitySnapshot $snapshot): void
46    {
47        $this->findHandle($scope, $key)?->applySnapshot($snapshot);
48    }
49
50    public function releaseHandlesOf(ResourceScope $scope): void
51    {
52        $handles = $this->handlesByScope[$scope->wireValue()] ?? [];
53        unset($this->handlesByScope[$scope->wireValue()]);
54
55        if ($handles === []) {
56            return;
57        }
58
59        foreach ($handles as $handle) {
60            $handle->markReleased();
61        }
62
63        try {
64            $this->transport->send(new ExposuresReleased($scope));
65        } catch (TransportException $e) {
66            $this->logger->debug('Could not release exposed entities; the broker channel is closed', ['exception' => $e, 'scope' => (string) $scope]);
67        }
68    }
69
70    public function releaseAll(): void
71    {
72        foreach ($this->handlesByScope as $handles) {
73            foreach ($handles as $handle) {
74                $handle->markReleased();
75            }
76        }
77
78        $this->handlesByScope = [];
79    }
80}