Lines 0.00% 0 / 32
Functions and Methods 0.00% 0 / 5
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ActionCaller 0.00% 0 / 32 0.00% 0 / 5 110 0.00% 0 / 1
 __construct 0.00% 0 / 1 0.00% 0 / 1 2
 callAction 0.00% 0 / 1 0.00% 0 / 1 2
 callActionForResponse 0.00% 0 / 1 0.00% 0 / 1 2
 sendRequestAndAwait 0.00% 0 / 24 0.00% 0 / 1 12
 sendRequest 0.00% 0 / 5 0.00% 0 / 1 20
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Worker\Context;
6
7use Amp\CancelledException;
8use Stewart\Contracts\Action\ActionName;
9use Stewart\Contracts\Action\ActionResponse;
10use Stewart\Contracts\Action\ActionTarget;
11use Stewart\Contracts\Exception\ActionCallException;
12use Stewart\Contracts\State\EventContext;
13use Stewart\Runtime\Exception\TransportError;
14use Stewart\Runtime\Exception\TransportException;
15use Stewart\Runtime\Ipc\Message\ActionCallRequest;
16use Stewart\Runtime\Ipc\Transport;
17use Stewart\Runtime\Ipc\WorkerSettings;
18use Stewart\Runtime\Model\ResourceScope;
19use Stewart\Runtime\Worker\ConnectionStatus;
20use Stewart\Runtime\Worker\PendingRequests;
21use Stewart\Support\Time\Deadlines;
22use Stewart\Support\Wire\ActionFields;
23use Throwable;
24
25final readonly class ActionCaller
26{
27    public function __construct(
28        private Transport $transport,
29        private PendingRequests $pending,
30        private ConnectionStatus $connection,
31        private Deadlines $deadlines,
32        private WorkerSettings $workerSettings,
33    ) {}
34
35    /** @throws ActionCallException */
36    public function callAction(ResourceScope $scope, ActionName $action, ActionFields $data, ?ActionTarget $target): EventContext
37    {
38        return $this->sendRequestAndAwait($scope, $action, $data, $target, returnResponse: false)->context ?? EventContext::unknown();
39    }
40
41    /** @throws ActionCallException */
42    public function callActionForResponse(ResourceScope $scope, ActionName $action, ActionFields $data, ?ActionTarget $target): ActionResponse
43    {
44        return $this->sendRequestAndAwait($scope, $action, $data, $target, returnResponse: true);
45    }
46
47    /** @throws ActionCallException */
48    private function sendRequestAndAwait(
49        ResourceScope $scope,
50        ActionName $action,
51        ActionFields $data,
52        ?ActionTarget $target,
53        bool $returnResponse,
54    ): ActionResponse {
55        if (!$this->connection->connected) {
56            throw ActionCallException::unreachable($action->domain, $action->action, 'Home Assistant is disconnected');
57        }
58
59        $call = $this->pending->open(
60            ActionResponse::class,
61            static fn(string $detail): ActionCallException => ActionCallException::unreachable($action->domain, $action->action, $detail),
62        );
63
64        try {
65            $this->sendRequest(new ActionCallRequest(
66                correlationId: $call->correlationId,
67                scope: $scope,
68                domain: $action->domain,
69                action: $action->action,
70                data: $data->fields,
71                target: $target,
72                returnResponse: $returnResponse,
73            ));
74
75            return $call->getFuture()->await($this->deadlines->timeout($this->workerSettings->callTimeout));
76        } catch (CancelledException $e) {
77            throw ActionCallException::timedOut(
78                $action->domain,
79                $action->action,
80                \sprintf('no answer from the broker within %s', $this->workerSettings->callTimeout),
81                $e,
82            );
83        } finally {
84            $this->pending->forget($call->correlationId);
85        }
86    }
87
88    /** @throws ActionCallException */
89    private function sendRequest(ActionCallRequest $request): void
90    {
91        try {
92            $this->transport->send($request);
93        } catch (Throwable $e) {
94            throw $e instanceof TransportException && $e->reason === TransportError::Unencodable
95                ? ActionCallException::rejected($request->domain, $request->action, $e->getMessage(), null, $e)
96                : ActionCallException::unreachable($request->domain, $request->action, 'the broker is gone: ' . $e->getMessage(), $e);
97        }
98    }
99}