Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
CaptureCommand
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
4 / 4
12
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
 run
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
5
 hasHelpFlag
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
5
 parseArgs
100.00% covered (success)
100.00%
5 / 5
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\CLI;
14
15use function error_log;
16use Clansuite\Capture\CaptureService;
17use Exception;
18
19/**
20 * Command-line interface for capturing game server information.
21 */
22class CaptureCommand
23{
24    public function __construct(private readonly CaptureService $captureService)
25    {
26    }
27
28    /**
29     * Executes the capture command with the given arguments.
30     *
31     * @param array<string> $argv command-line arguments
32     *
33     * @return int exit code (0 for success, 1 for failure)
34     */
35    public function run(array $argv): int
36    {
37        // Simple help handling: if user passed -h/--help or insufficient args, delegate to HelpCommand
38        if ($this->hasHelpFlag($argv)) {
39            $help = new HelpCommand;
40            $help->run($argv);
41
42            return 0;
43        }
44
45        $args     = $this->parseArgs($argv);
46        $ip       = $args[0];
47        $port     = $args[1];
48        $protocol = $args[2];
49        $options  = $args[3];
50
51        if ($ip === '' || $port <= 0) {
52            $help = new HelpCommand;
53            $help->run(['', "❌ Missing required arguments: ip and port are required.\n"]);
54
55            return 1;
56        }
57
58        try {
59            $savedPath = $this->captureService->capture($ip, $port, $protocol, $options);
60            print $savedPath . "\n";
61
62            return 0;
63        } catch (Exception $e) {
64            error_log('❌ ' . $e->getMessage());
65
66            return 1;
67        }
68    }
69
70    /**
71     * @param array<string> $argv
72     */
73    private function hasHelpFlag(array $argv): bool
74    {
75        foreach ($argv as $arg) {
76            if ($arg === '-h' || $arg === '--help' || $arg === 'help') {
77                return true;
78            }
79        }
80
81        return false;
82    }
83
84    /**
85     * @param array<string> $argv
86     *
87     * @return array{string, int, string, array<mixed>}
88     */
89    private function parseArgs(array $argv): array
90    {
91        $ip       = $argv[1] ?? '';
92        $port     = (int) ($argv[2] ?? 0);
93        $protocol = $argv[3] ?? 'auto';
94        $options  = [];
95
96        // Basic parsing, can be enhanced
97        return [$ip, $port, $protocol, $options];
98    }
99}