Lines 93.33% 14 / 15
Functions and Methods 83.33% 5 / 6
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
UnixControlAddress 93.33% 14 / 15 83.33% 5 / 6 8.02 0.00% 0 / 1
 __construct 50.00% 1 / 2 0.00% 0 / 1 2.50
 resolveSocketPath 100.00% 9 / 9 100.00% 1 / 1 2
 isLoopback 100.00% 1 / 1 100.00% 1 / 1 1
 toConnectableAddress 100.00% 1 / 1 100.00% 1 / 1 1
 resolveSocketAddress 100.00% 1 / 1 100.00% 1 / 1 1
 __toString 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Config;
6
7use Amp\Socket\UnixAddress;
8use Stewart\Runtime\Exception\ConfigurationException;
9
10final readonly class UnixControlAddress extends ControlAddress
11{
12    // Linux sun_path is 108 bytes, less a terminating NUL and a temp suffix.
13    private const int MAX_SOCKET_PATH_BYTES = 100;
14
15    /** @throws ConfigurationException */
16    public function __construct(public string $path)
17    {
18        if ($path === '') {
19            throw ConfigurationException::valueInvalid('unix://', 'a unix control address with a path');
20        }
21    }
22
23    /** @throws ConfigurationException */
24    public function resolveSocketPath(ProjectRoot $projectRoot): string
25    {
26        $absolutePath = $projectRoot->resolvePath($this->path);
27
28        if (\strlen($absolutePath) > self::MAX_SOCKET_PATH_BYTES) {
29            throw ConfigurationException::valueInvalid((string) $this, \sprintf(
30                'a unix control address; the resolved path %s is %d bytes, the limit is %d',
31                $absolutePath,
32                \strlen($absolutePath),
33                self::MAX_SOCKET_PATH_BYTES,
34            ));
35        }
36
37        return $absolutePath;
38    }
39
40    public function isLoopback(): bool
41    {
42        return true;
43    }
44
45    public function toConnectableAddress(): self
46    {
47        return $this;
48    }
49
50    /** @throws ConfigurationException */
51    public function resolveSocketAddress(ProjectRoot $projectRoot): UnixAddress
52    {
53        return new UnixAddress($this->resolveSocketPath($projectRoot));
54    }
55
56    public function __toString(): string
57    {
58        return 'unix://' . $this->path;
59    }
60}