RunCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 67
rs 10
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B runCommand() 0 62 8
1
<?php
2
3
namespace Startwind\Forrest\CliCommand;
4
5
use Startwind\Forrest\Command\Command;
6
use Startwind\Forrest\Output\PromptHelper;
7
use Startwind\Forrest\Output\RunHelper;
8
use Startwind\Forrest\Repository\RepositoryCollection;
9
use Startwind\Forrest\Repository\StatusAwareRepository;
10
use Startwind\Forrest\Runner\Exception\ToolNotFoundException;
11
use Symfony\Component\Console\Command\Command as SymfonyCommand;
12
use Symfony\Component\Console\Helper\QuestionHelper;
13
14
abstract class RunCommand extends ForrestCommand
15
{
16
    /**
17
     * Run the actual command and ask the user for the details.
18
     */
19
    protected function runCommand(Command|string $command, array $userParameters = []): int
20
    {
21
        if (is_string($command)) {
22
            $commandIdentifier = $command;
23
            $command = $this->getCommand($command);
24
        } else {
25
            $commandIdentifier = $command->getFullyQualifiedIdentifier();
26
        }
27
28
        $repositoryIdentifier = RepositoryCollection::getRepositoryIdentifier($commandIdentifier);
29
30
        /** @var QuestionHelper $questionHelper */
31
        $questionHelper = $this->getHelper('question');
32
33
        $promptHelper = new PromptHelper($this->getInput(), $this->getOutput(), $questionHelper, $this->getRecentParameterMemory());
34
35
        $prompt = $promptHelper->askForPrompt($command, $userParameters);
36
37
        $promptHelper->showFinalPrompt($prompt);
38
39
        $runHelper = new RunHelper($this->getInput(), $this->getOutput(), $questionHelper, $this->getConfigHandler(), $this->getHistoryHandler());
40
41
        $force = $this->getInput()->getOption('force');
42
43
        if (!$runHelper->handleRunnable($command, $prompt->getFinalPrompt())) {
44
            return SymfonyCommand::SUCCESS;
45
        }
46
47
        if (!$runHelper->handleForceOption($force, $command, $repositoryIdentifier)) {
48
            return SymfonyCommand::FAILURE;
49
        }
50
51
        if (!$runHelper->confirmRun($force)) {
52
            return SymfonyCommand::FAILURE;
53
        }
54
55
        $this->getOutput()->writeln('');
56
57
        try {
58
            $exitCode = $runHelper->executeCommand($this->getOutput(), $command, $prompt);
59
        } catch (ToolNotFoundException $exception) {
60
            if ($command->getFullyQualifiedIdentifier()) {
61
                $this->getRepositoryCollection()->pushStatus($command->getFullyQualifiedIdentifier(), StatusAwareRepository::STATUS_FAILURE);
62
            }
63
            $this->renderErrorBox($exception->getMessage());
64
            return SymfonyCommand::FAILURE;
65
        }
66
67
        $this->getOutput()->writeln('');
68
69
        if ($exitCode == SymfonyCommand::SUCCESS) {
70
            $this->getOutput()->writeln('<info>Command ran successfully.');
71
        } else {
72
            $this->getOutput()->writeln('<error>' . $exitCode . ' Command did not run successfully.');
73
        }
74
75
        $this->getConfigHandler()->persistChecksum($command, $repositoryIdentifier);
76
        $this->getRecentParameterMemory()->dump();
77
78
        $this->getRepositoryCollection()->pushStatus($command->getFullyQualifiedIdentifier(), StatusAwareRepository::STATUS_SUCCESS);
79
80
        return SymfonyCommand::SUCCESS;
81
    }
82
}
83