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:
13: namespace Clansuite\Capture;
14:
15: use Clansuite\Capture\Extractor\VersionNormalizer;
16: use Clansuite\Capture\Protocol\ProtocolResolver;
17: use Clansuite\Capture\Storage\FixtureStorageInterface;
18: use Clansuite\Capture\Strategy\CaptureStrategyInterface;
19:
20: /**
21: * Service class for capturing game server information using configurable strategies and protocols.
22: */
23: final readonly class CaptureService
24: {
25: /**
26: * Initializes the capture service with required dependencies.
27: *
28: * @param ProtocolResolver $protocolResolver resolves protocol implementations
29: * @param CaptureStrategyInterface $captureStrategy strategy for performing captures
30: * @param FixtureStorageInterface $storage storage for captured data
31: * @param VersionNormalizer $normalizer normalizes version information
32: */
33: public function __construct(
34: private ProtocolResolver $protocolResolver,
35: private CaptureStrategyInterface $captureStrategy,
36: private FixtureStorageInterface $storage,
37: private VersionNormalizer $normalizer
38: ) {
39: }
40:
41: /**
42: * Captures server information for the given IP, port, and protocol, and stores the result.
43: *
44: * @param string $ip server IP address
45: * @param int $port server port
46: * @param string $protocol protocol name or 'auto' for automatic detection
47: * @param array<mixed> $options additional capture options
48: *
49: * @return string path to the stored capture result
50: */
51: public function capture(string $ip, int $port, string $protocol = 'auto', array $options = []): string
52: {
53: $protocolInstance = $this->protocolResolver->resolve($protocol, $ip, $port);
54: $addr = new ServerAddress($ip, $port);
55:
56: $result = $this->captureStrategy->capture($protocolInstance, $addr, $options + ['protocol_name' => $protocol]);
57:
58: $version = $this->normalizer->normalize(
59: $protocolInstance->getVersion($result->serverInfo),
60: );
61:
62: return $this->storage->save(
63: $protocolInstance->getProtocolName(),
64: $version,
65: $ip,
66: $port,
67: $result,
68: );
69: }
70: }
71: