Lines 100.00% 9 / 9
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
AppRequestCommand 100.00% 9 / 9 100.00% 3 / 3 4 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 configure 100.00% 2 / 2 100.00% 1 / 1 1
 execute 100.00% 6 / 6 100.00% 1 / 1 2
 sendAppRequest n/a 0 / 0 n/a 0 / 0 0
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Console;
6
7use Amp\ByteStream\StreamException;
8use Amp\CancelledException;
9use Stewart\Contracts\App\AppId;
10use Stewart\Contracts\Exception\StewartException;
11use Stewart\Contracts\Time\Duration;
12use Stewart\Runtime\Config\ConfigLoader;
13use Stewart\Runtime\Control\Client\ControlClient;
14use Stewart\Runtime\Control\Client\ControlTarget;
15use Stewart\Runtime\Control\Client\ControlTargetResolver;
16use Stewart\Runtime\Control\Protocol\Frame\CommandResult;
17use Symfony\Component\Console\Command\Command;
18use Symfony\Component\Console\Input\InputArgument;
19use Symfony\Component\Console\Input\InputInterface;
20use Symfony\Component\Console\Output\OutputInterface;
21use Throwable;
22
23abstract class AppRequestCommand extends ControlCommand
24{
25    public function __construct(
26        ConfigLoader $config,
27        ControlTargetResolver $targets,
28        protected readonly ControlClient $client,
29    ) {
30        parent::__construct($config, $targets);
31    }
32
33    protected function configure(): void
34    {
35        parent::configure();
36        $this->addArgument('app', InputArgument::REQUIRED, 'The automation ID, as shown by stewart status');
37    }
38
39    protected function execute(InputInterface $input, OutputInterface $output): int
40    {
41        try {
42            $result = $this->sendAppRequest($this->resolveControlTarget($input), new AppId($this->readStringArgument($input, 'app')), $this->parseTimeoutOption($input));
43        } catch (StewartException|StreamException|CancelledException $e) {
44            $output->writeln('<error>' . $e->getMessage() . '</error>');
45
46            return Command::FAILURE;
47        }
48
49        $this->writeCommandResult($output, $result);
50
51        return Command::SUCCESS;
52    }
53
54    /** @throws StewartException|Throwable */
55    abstract protected function sendAppRequest(ControlTarget $target, AppId $appId, Duration $timeout): CommandResult;
56}