Lines 100.00% 8 / 8
Functions and Methods 100.00% 1 / 1
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ControlAddress 100.00% 8 / 8 100.00% 1 / 1 3 100.00% 1 / 1
 parse 100.00% 8 / 8 100.00% 1 / 1 3
 isLoopback n/a 0 / 0 n/a 0 / 0 0
 toConnectableAddress n/a 0 / 0 n/a 0 / 0 0
 resolveSocketAddress n/a 0 / 0 n/a 0 / 0 0
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Config;
6
7use Amp\Socket\SocketAddress;
8use Stewart\Runtime\Exception\ConfigurationException;
9use Stringable;
10
11abstract readonly class ControlAddress implements Stringable
12{
13    public const string DEFAULT_SOCKET = 'var/run/stewart.sock';
14
15    /** @throws ConfigurationException */
16    public static function parse(string $address): self
17    {
18        if (preg_match('~\Aunix://(.+)\z~', $address, $unix) === 1) {
19            return new UnixControlAddress($unix[1]);
20        }
21
22        if (preg_match('~\Atcp://(\[[0-9a-fA-F:.]+\]|[^:/\[\]]+):([0-9]{1,5})\z~', $address, $tcp) === 1) {
23            return new TcpControlAddress($tcp[1], (int) $tcp[2]);
24        }
25
26        throw ConfigurationException::valueInvalid(
27            $address,
28            'a control address; use unix://path, tcp://host:port or "off"',
29        );
30    }
31
32    abstract public function isLoopback(): bool;
33
34    abstract public function toConnectableAddress(): self;
35
36    /** @throws ConfigurationException */
37    abstract public function resolveSocketAddress(ProjectRoot $projectRoot): SocketAddress|string;
38}