Passed
Push — main ( 742cd1...bef85d )
by Greg
06:43
created

TreeList::execute()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 31
nc 4
nop 2
dl 0
loc 47
rs 8.8017
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Cli\Commands;
21
22
use Fisharebest\Webtrees\Services\TreeService;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Services\TreeService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Fisharebest\Webtrees\Tree;
24
use Symfony\Component\Console\Command\Command;
25
use Symfony\Component\Console\Helper\Table;
26
use Symfony\Component\Console\Input\InputInterface;
27
use Symfony\Component\Console\Input\InputOption;
28
use Symfony\Component\Console\Output\OutputInterface;
29
use Symfony\Component\Console\Style\SymfonyStyle;
30
31
use function addcslashes;
32
use function array_map;
33
use function implode;
34
35
final class TreeList extends Command
36
{
37
    public function __construct(private readonly TreeService $tree_service)
38
    {
39
        parent::__construct();
40
    }
41
42
    protected function configure(): void
43
    {
44
        $this
45
            ->setName(name: 'tree-list')
46
            ->setDescription(description: 'List trees')
47
            ->addOption(
48
                name: 'format',
49
                shortcut: 'f',
50
                mode: InputOption::VALUE_REQUIRED,
51
                description: 'Output format (table, json, csv)',
52
                default: 'table',
53
            );
54
    }
55
56
    protected function execute(InputInterface $input, OutputInterface $output): int
57
    {
58
        $format = $input->getOption(name: 'format');
59
60
        $io = new SymfonyStyle(input: $input, output: $output);
61
62
        $trees = $this->tree_service->all()->sort(callback: fn ($a, $b) => $a->id() <=> $b->id());
63
64
        $headers = ['ID', 'Name', 'Title', 'Media directory', 'Imported'];
65
66
        $rows = $trees->map(callback: static fn (Tree $tree): array => [
67
            'id'              => $tree->id(),
68
            'name'            => $tree->name(),
69
            'title'           => $tree->title(),
70
            'media_directory' => $tree->getPreference(setting_name: 'MEDIA_DIRECTORY'),
71
            'imported'        => $tree->getPreference(setting_name: 'imported') ? 'yes' : 'no',
72
        ])
73
        ->values()
74
        ->all();
75
76
        switch ($format) {
77
            case 'table':
78
                $table = new Table(output: $output);
79
                $table->setHeaders(headers: $headers);
80
                $table->setRows(rows: $rows);
81
                $table->render();
82
                break;
83
84
            case 'csv':
85
                $output->writeln(messages: $this->quoteCsvRow(columns: $headers));
86
87
                foreach ($rows as $row) {
88
                    $output->writeln(messages: $this->quoteCsvRow(columns: $row));
89
                }
90
                break;
91
92
            case 'json':
93
                $output->writeln(messages: json_encode(value: $rows, flags: JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT));
94
                break;
95
96
            default:
97
                $io->error(message: 'Invalid format: ‘' . $format . '’');
98
99
                return Command::FAILURE;
100
        }
101
102
        return Command::SUCCESS;
103
    }
104
105
    /**
106
     * @param array<string|int> $columns
107
     */
108
    private function quoteCsvRow(array $columns): string
109
    {
110
        $columns = array_map(callback: $this->quoteCsvValue(...), array: $columns);
111
112
        return implode(separator: ',', array: $columns);
113
    }
114
115
    private function quoteCsvValue(string|int $value): string
0 ignored issues
show
Unused Code introduced by
The method quoteCsvValue() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
116
    {
117
        return '"' . addcslashes(string: (string) $value, characters: '"') . '"';
118
    }
119
}
120