Passed
Push — main ( 9c2569...fd54af )
by Greg
09:05
created

TreeExport::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2023 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\Auth;
23
use Fisharebest\Webtrees\DB;
24
use Fisharebest\Webtrees\Encodings\UTF8;
25
use Fisharebest\Webtrees\Services\GedcomExportService;
26
use Fisharebest\Webtrees\Services\TreeService;
27
use Symfony\Component\Console\Command\Command;
28
use Symfony\Component\Console\Completion\CompletionInput;
29
use Symfony\Component\Console\Input\InputArgument;
30
use Symfony\Component\Console\Input\InputInterface;
31
use Symfony\Component\Console\Input\InputOption;
32
use Symfony\Component\Console\Output\OutputInterface;
33
use Symfony\Component\Console\Style\SymfonyStyle;
34
35
use function addcslashes;
36
use function stream_get_contents;
37
38
class TreeExport extends Command
39
{
40
    public function __construct(
41
        private readonly GedcomExportService $gedcom_export_service,
42
        private readonly TreeService $tree_service,
43
    ) {
44
        parent::__construct();
45
    }
46
47
    protected function configure(): void
48
    {
49
        $this
50
            ->setName(name: 'tree-export')
51
            ->addArgument(name: 'tree_name', mode: InputArgument::REQUIRED, description: 'The name of the tree', suggestedValues: self::autoCompleteTreeName(...))
52
            ->addOption(name: 'format', shortcut: null, mode: InputOption::VALUE_REQUIRED, description: 'Export format')
53
            ->addOption(name: 'filename', shortcut: null, mode: InputOption::VALUE_REQUIRED, description: 'Export filename')
54
            ->setDescription(description: 'Export a tree to a GEDCOM file');
55
    }
56
57
    private function autoCompleteTreeName(CompletionInput $input): array
0 ignored issues
show
Unused Code introduced by
The method autoCompleteTreeName() 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...
58
    {
59
        return DB::table('tree')
60
            ->where('tree_name', 'LIKE', addcslashes($input->getCompletionValue(), '%_\\') .'%')
61
            ->pluck('name')
62
            ->all();
63
    }
64
65
    protected function execute(InputInterface $input, OutputInterface $output): int
66
    {
67
        $io = new SymfonyStyle(input: $input, output: $output);
68
69
        $tree_name = $input->getArgument(name: 'tree_name');
70
        $format   = $input->getOption(name: 'format');
0 ignored issues
show
Unused Code introduced by
The assignment to $format is dead and can be removed.
Loading history...
71
        $filename = $input->getOption(name: 'filename');
0 ignored issues
show
Unused Code introduced by
The assignment to $filename is dead and can be removed.
Loading history...
72
73
        $tree = $this->tree_service->all()[$tree_name] ?? null;
74
75
        if ($tree === null) {
76
            $io->error(message: 'Tree "' . $tree_name . '" not found.');
77
78
            return Command::FAILURE;
79
        }
80
81
        $stream = $this->gedcom_export_service->export(
82
            tree: $tree,
83
            sort_by_xref: false,
84
            encoding: UTF8::NAME,
85
            access_level: Auth::PRIV_HIDE,
86
            line_endings: 'CRLF',
87
            records: null,
88
            zip_filesystem: null,
89
            media_path: null,
90
        );
91
92
        echo stream_get_contents($stream);
93
94
        $io->success('File exported successfully.');
95
96
        return Command::SUCCESS;
97
    }
98
}
99