Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ProtocolResolver
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 resolve
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 detectProtocol
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php declare(strict_types=1);
2
3/**
4 * Clansuite Server Query
5 *
6 * SPDX-FileCopyrightText: 2003-2025 Jens A. Koch
7 * SPDX-License-Identifier: MIT
8 *
9 * For the full copyright and license information, please view
10 * the LICENSE file that was distributed with this source code.
11 */
12
13namespace Clansuite\Capture\Protocol;
14
15use function is_a;
16use function is_string;
17use Clansuite\Capture\UnknownProtocolException;
18
19/**
20 * Resolves protocol names to their corresponding class implementations for server querying.
21 */
22class ProtocolResolver
23{
24    /**
25     * Constructor.
26     *
27     * @param array<mixed> $protocolMap
28     */
29    public function __construct(private array $protocolMap)
30    {
31    }
32
33    /**
34     * resolve method.
35     */
36    public function resolve(string $protocol, string $ip, int $port): ProtocolInterface
37    {
38        if ($protocol === 'auto') {
39            $protocol = $this->detectProtocol();
40        }
41
42        $class = $this->protocolMap[$protocol] ?? throw new UnknownProtocolException($protocol);
43
44        if (!is_string($class) || !is_a($class, ProtocolInterface::class, true)) {
45            throw new UnknownProtocolException($protocol);
46        }
47
48        /** @var class-string<ProtocolInterface> $class */
49        return new $class;
50    }
51
52    private function detectProtocol(): string
53    {
54        // Stub: default to source
55        return 'source';
56    }
57}