Lines 100.00% 18 / 18
Functions and Methods 100.00% 9 / 9
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
WorkerExposure 100.00% 18 / 18 100.00% 9 / 9 11 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 getKey 100.00% 1 / 1 100.00% 1 / 1 1
 remove 100.00% 4 / 4 100.00% 1 / 1 1
 markReleased 100.00% 1 / 1 100.00% 1 / 1 1
 findConfig 100.00% 1 / 1 100.00% 1 / 1 1
 watchOwnCommands 100.00% 1 / 1 100.00% 1 / 1 1
 sendReconfiguration 100.00% 5 / 5 100.00% 1 / 1 2
 sendUpdate 100.00% 2 / 2 100.00% 1 / 1 1
 assertNotReleased 100.00% 2 / 2 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Worker\Exposure;
6
7use Stewart\Contracts\EventStream;
8use Stewart\Contracts\Exception\ExposureException;
9use Stewart\Contracts\Exposure\Command\ExposedCommand;
10use Stewart\Contracts\Exposure\ExposedEntityKey;
11use Stewart\Contracts\Exposure\ExposedStateChange;
12use Stewart\Contracts\Exposure\ExposureConfig;
13use Stewart\Runtime\Model\ResourceScope;
14
15/** @template TConfig of ExposureConfig */
16abstract class WorkerExposure implements ExposedHandle
17{
18    private bool $released = false;
19
20    /** @param TConfig $config */
21    public function __construct(
22        private readonly ExposureChannel $channel,
23        private readonly ResourceScope $scope,
24        private readonly ExposedEntityKey $key,
25        private ExposureConfig $config,
26    ) {}
27
28    public function getKey(): ExposedEntityKey
29    {
30        return $this->key;
31    }
32
33    public function remove(): void
34    {
35        $this->assertNotReleased();
36        $this->channel->requester->requestRemoval($this->scope, $this->key);
37        $this->channel->handles->forgetHandle($this->scope, $this->key);
38        $this->released = true;
39    }
40
41    public function markReleased(): void
42    {
43        $this->released = true;
44    }
45
46    /** @return TConfig */
47    protected function findConfig(): ExposureConfig
48    {
49        return $this->config;
50    }
51
52    /** @return EventStream<ExposedCommand> */
53    protected function watchOwnCommands(): EventStream
54    {
55        return $this->channel->commandStreams->watchCommands($this->scope, $this->key);
56    }
57
58    /**
59     * @param TConfig $config
60     * @throws ExposureException
61     */
62    protected function sendReconfiguration(ExposureConfig $config): void
63    {
64        $this->assertNotReleased();
65        $snapshot = $this->channel->requester->requestReconfiguration($this->scope, $this->key, $config);
66        $this->config = $config;
67
68        if ($snapshot !== null) {
69            $this->applySnapshot($snapshot);
70        }
71    }
72
73    /** @throws ExposureException */
74    protected function sendUpdate(ExposedStateChange $change): void
75    {
76        $this->assertNotReleased();
77        $this->channel->requester->requestUpdate($this->scope, $this->key, $change);
78    }
79
80    /** @throws ExposureException */
81    private function assertNotReleased(): void
82    {
83        if ($this->released) {
84            throw ExposureException::removed($this->key);
85        }
86    }
87}