Passed
Push — main ( a22652...94920f )
by Greg
08:47
created

TreeEdit::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
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\Input\InputArgument;
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
final class TreeEdit extends AbstractCommand
32
{
33
    public function __construct(private readonly TreeService $tree_service)
34
    {
35
        parent::__construct();
36
    }
37
38
    protected function configure(): void
39
    {
40
        $this
41
            ->setName(name: 'tree')
42
            ->setDescription(description: 'Create/delete/edit a tree')
43
            ->addArgument(name: 'name', mode: InputArgument::REQUIRED, description: 'The name of the tree')
44
            ->addOption(name: 'create', mode: InputOption::VALUE_NONE, description: 'Create a new tree')
45
            ->addOption(name: 'delete', mode: InputOption::VALUE_NONE, description: 'Delete an existing tree')
46
            ->addOption(name: 'title', mode: InputOption::VALUE_REQUIRED, description: 'Set the title of the tree');
47
    }
48
49
    protected function execute(InputInterface $input, OutputInterface $output): int
50
    {
51
        $io = new SymfonyStyle(input: $input, output: $output);
52
53
        $name   = $this->stringArgument(input: $input, name: 'name');
54
        $title  = $this->stringOption(input: $input, name: 'title');
55
        $create = $this->boolOption(input: $input, name: 'create');
56
        $delete = $this->boolOption(input: $input, name: 'delete');
57
58
        if ($name === '') {
59
            $io->error(message: 'The tree name cannot be empty.');
60
61
            return Command::INVALID;
62
        }
63
64
        if ($delete && $create) {
65
            $io->error(message: 'Invalid options: cannot use --delete and --create at the same time.');
66
67
            return Command::INVALID;
68
        }
69
70
        if ($delete && $title !== '') {
71
            $io->error(message: 'Invalid options: cannot use --delete and --title at the same time.');
72
73
            return Command::INVALID;
74
        }
75
76
        $tree = $this->tree_service->all()->get('name');
77
78
        if ($create) {
79
            if ($tree instanceof Tree) {
80
                $io->error(message: 'A tree with the name "' . $name . '" already exists.');
81
82
                return Command::FAILURE;
83
            }
84
85
            if ($title === '') {
86
                $io->error(message: 'Invalid options: --title is required when using --create.');
87
88
                return Command::FAILURE;
89
            }
90
91
            $this->tree_service->create(name: $name, title: $title);
92
            $io->info(message: 'Tree ‘' . $name . '’ was created with title ‘' . $title . '’.');
93
94
            return self::SUCCESS;
95
        }
96
97
        if ($tree === null) {
98
            $io->error(message: 'Tree ‘' . $name . '’ does not exist.');
99
100
            return Command::FAILURE;
101
        }
102
103
        if ($delete) {
104
            $this->tree_service->delete($tree);
105
            $io->success(message: 'Tree ‘' . $name . '’ was deleted..');
106
107
            return self::SUCCESS;
108
        }
109
110
        if ($title === '') {
111
            $io->info(message: 'Nothing to do. Specify --title, --create or --delete.');
112
113
            return Command::INVALID;
114
        }
115
116
        $tree->setPreference('title', $title);
117
        $io->info(message: 'Tree title set to ‘' . $title . '’.');
118
119
        return self::SUCCESS;
120
    }
121
}
122