CommandCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 71
rs 10
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B renderListCommand() 0 66 10
1
<?php
2
3
namespace Startwind\Forrest\CliCommand\Command;
4
5
use Startwind\Forrest\Output\OutputHelper;
6
use Startwind\Forrest\Repository\ListAware;
7
use Startwind\Forrest\Repository\RepositoryCollection;
8
9
class CommandCommand extends \Startwind\Forrest\CliCommand\RunCommand
10
{
11
    /**
12
     * Render the list output
13
     */
14
    protected function renderListCommand(string $repository = ''): void
15
    {
16
        $output = $this->getOutput();
17
18
        OutputHelper::renderHeader($output);
19
20
        $this->enrichRepositories();
21
22
        $maxLength = 0;
23
24
        if ($repository) {
25
            $repositories = [$repository => $this->getRepositoryCollection()->getRepository($repository)];
26
        } else {
27
            $repositories = $this->getRepositoryCollection()->getRepositories();
28
        }
29
30
        foreach ($repositories as $repoIdentifier => $repository) {
31
            if ($repository instanceof ListAware) {
32
                try {
33
                    foreach ($repository->getCommands(false) as $command) {
34
                        $maxLength = max($maxLength, strlen(RepositoryCollection::createUniqueCommandName($repoIdentifier, $command)));
35
                    }
36
                } catch (\Exception $exception) {
37
                    unset($repositories[$repoIdentifier]);
38
                    $this->renderErrorBox([
39
                        'Unable to fetch commands from ' . $repoIdentifier . '. ' . $exception->getMessage(),
40
                    ]);
41
                    $output->writeln('');
42
                }
43
            }
44
        }
45
46
        $output->writeln([
47
            '<fg=yellow>Usage:</>',
48
            '',
49
            '  forrest run [command]',
50
            '',
51
        ]);
52
53
        foreach ($repositories as $repoIdentifier => $repository) {
54
            if (!$repository instanceof ListAware) {
55
                continue;
56
            }
57
58
            if (!$repository->hasCommands()) {
59
                continue;
60
            }
61
62
            if ($repository->isSpecial()) {
63
                $this->renderWarningBox($repository->getName() . ' (' . $repoIdentifier . ')');
64
                $output->writeln(['  ' . $repository->getDescription(), '']);
65
            } else {
66
                $output->writeln([
67
                    '',
68
                    '<fg=yellow>' . $repository->getName() . '</> (' . $repoIdentifier . ')',
69
                    '',
70
                ]);
71
            }
72
73
            /** @var \Symfony\Component\Console\Helper\QuestionHelper $questionHelper */
74
            $questionHelper = $this->getHelper('question');
75
76
            OutputHelper::renderCommands($output, $this->getInput(), $questionHelper, $repository->getCommands(false), $repoIdentifier, $maxLength);
77
        }
78
79
        $output->writeln('');
80
    }
81
}
82