Lines 100.00% 8 / 8
Methods 100.00% 6 / 6
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 2 / 2 100.00% 1 / 1 1
 describePeer 100.00% 1 / 1 100.00% 1 / 1 1
 hasExpired 100.00% 1 / 1 100.00% 1 / 1 1
 readLine 100.00% 1 / 1 100.00% 1 / 1 1
 send 100.00% 1 / 1 100.00% 1 / 1 1
 close 100.00% 2 / 2 100.00% 1 / 1 1
16final readonly class ClientSession
17{
18    private BufferedReader $reader;
19
20    private string $deadlineSubscription;
21
22    // Closing on the deadline aborts a pending write, which takes no cancellation.
23    public function __construct(
24        public int $id,
25        private Socket $socket,
26        private FrameCodec $codec,
27        private Cancellation $deadline,
28    ) {
29        $this->reader = new BufferedReader($socket);
30        $this->deadlineSubscription = $deadline->subscribe($socket->close(...));
31    }
32
33    public function describePeer(): string
34    {
35        return $this->socket->getRemoteAddress()->toString();
36    }
37
38    public function hasExpired(): bool
39    {
40        return $this->deadline->isRequested();
41    }
42
43    /**
44     * @param positive-int $limit
45     * @throws Throwable
46     */
47    public function readLine(int $limit): string
48    {
49        return rtrim($this->reader->readUntil("\n", $this->deadline, $limit), "\r\n");
50    }
51
52    /** @throws ControlException|StreamException */
53    public function send(ServerFrame $frame): void
54    {
55        $this->socket->write($this->codec->encodeFrame($frame));
56    }
57
58    public function close(): void
59    {
60        $this->deadline->unsubscribe($this->deadlineSubscription);
61        $this->socket->close();
62    }
63}