ListCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Startwind\Forrest\CliCommand\Directory;
4
5
use Startwind\Forrest\CliCommand\Directory\Exception\DirectoriesLoadException;
6
use Startwind\Forrest\Util\OutputHelper;
7
use Startwind\Forrest\Output\OutputHelper as TableOutputHelper;
8
use Symfony\Component\Console\Command\Command as SymfonyCommand;
9
use Symfony\Component\Console\Helper\TableSeparator;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class ListCommand extends DirectoryCommand
15
{
16
    protected static $defaultName = 'directory:list';
17
    protected static $defaultDescription = 'List all repositories in the official Forrest directory.';
18
19
    protected function configure()
20
    {
21
        parent::configure();
22
        // @todo as long as there are only a few repositories listed we show all. This mechanism should be activated
23
        //       as soon as there a longer list.
24
        $this->addOption('all', '', InputOption::VALUE_OPTIONAL, 'List all repositories. Default is that only official repositories are shown.', true);
25
    }
26
27
    protected function doExecute(InputInterface $input, OutputInterface $output): int
28
    {
29
        $this->initRepositoryLoader();
30
        $activeRepositories = $this->getRepositoryLoader()->getIdentifiers();
31
32
        try {
33
            $directories = $this->getDirectories();
34
        } catch (DirectoriesLoadException $exception) {
35
            $directories = $exception->getDirectories();
36
            $messages = [];
37
            foreach ($exception->getExceptions() as $exception) {
38
                $messages[] = $exception->getMessage();
39
            }
40
            OutputHelper::writeErrorBox($output, $messages);
41
        }
42
43
        $repositories = [];
44
45
        foreach ($directories as $identifier => $directory) {
46
            if (!array_key_exists('repositories', $directory)) {
47
                throw new \RuntimeException('No repositories found in the given directory (' . $identifier . ').');
48
            }
49
50
            foreach ($directory['repositories'] as $repoIdentifier => $repository) {
51
                $repositories[$identifier][$repoIdentifier] = $repository;
52
            }
53
54
            ksort($repositories[$identifier]);
55
        }
56
57
        ksort($repositories);
58
59
        $rows = [];
60
61
        $all = $input->getOption('all') !== false;
62
63
        $unofficialCount = 0;
64
65
        $dirCount = 0;
66
        foreach ($repositories as $directoryIdentifier => $directoryRepositories) {
67
            foreach ($directoryRepositories as $identifier => $repository) {
68
                if ($all || (array_key_exists('official', $repository) && $repository['official'] === true)) {
69
                    $row = [
70
                        $identifier,
71
                        $repository['name'],
72
                        wordwrap($repository['description'], 55),
73
                    ];
74
75
                    if (count($directories) > 1) {
76
                        array_unshift($row, $directoryIdentifier);
77
                    }
78
79
                    if (in_array($identifier, $activeRepositories)) {
80
                        $row[] = 'x';
81
                    } else {
82
                        $row[] = '';
83
                    }
84
85
                    if ($all) {
86
                        if (array_key_exists('official', $repository) && $repository['official'] === true) {
87
                            $row[] = 'x';
88
                        } else {
89
                            $row[] = '';
90
                        }
91
                    }
92
93
                    $rows [] = $row;
94
                } else {
95
                    $unofficialCount++;
96
                }
97
            }
98
            if ($dirCount != count($directories) - 1) {
99
                $rows[] = new TableSeparator();
100
            }
101
            $dirCount++;
102
        }
103
104
        $headers = ['Identifier', 'Name', 'Description', 'Installed'];
105
106
        if (count($directories) > 1) {
107
            array_unshift($headers, 'Directory');
108
        }
109
110
        if ($all) {
111
            $headers[] = 'Official';
112
        }
113
114
        TableOutputHelper::renderTable($output, $headers, $rows);
115
116
        if ($unofficialCount > 0) {
117
            $output->writeln([
118
                    '',
119
                    'This list only contains official repositories. If you also want to see',
120
                    'the other ' . $unofficialCount . ' unofficial repositories please use the --all option.'
121
                ]);
122
        }
123
124
        return SymfonyCommand::SUCCESS;
125
    }
126
}
127