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

TreeCreate::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
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\Contracts\TreeInterface;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Contracts\TreeInterface 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\DB;
24
use Fisharebest\Webtrees\Services\TreeService;
25
use Symfony\Component\Console\Command\Command;
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 bin2hex;
32
use function random_bytes;
33
34
class TreeCreate extends Command
35
{
36
    public function __construct(private readonly TreeService $tree_service)
37
    {
38
        parent::__construct();
39
    }
40
41
    protected function configure(): void
42
    {
43
        $this
44
            ->setName(name: 'tree-create')
45
            ->setDescription(description: 'Create a new tree')
46
            ->addOption(name: 'name', shortcut: null, mode: InputOption::VALUE_REQUIRED, description: 'The name of the new tree')
47
            ->addOption(name: 'title', shortcut: null, mode: InputOption::VALUE_REQUIRED, description: 'The title of the new tree');
48
    }
49
50
    protected function execute(InputInterface $input, OutputInterface $output): int
51
    {
52
        $io = new SymfonyStyle(input: $input, output: $output);
53
54
        $name  = $input->getOption(name: 'name');
55
        $title = $input->getOption(name: 'title');
56
57
        $missing = false;
58
59
        if ($name === null) {
60
            $io->error(message: 'Missing required option: --name');
61
            $missing = true;
62
        }
63
64
        if ($title === null) {
65
            $io->error(message: 'Missing required option: --title');
66
            $missing = true;
67
        }
68
69
        if ($missing) {
70
            return Command::INVALID;
71
        }
72
73
        $tree = $this->tree_service->all()[$name] ?? null;
74
75
        if ($tree !== null) {
76
            $io->error(message: 'A tree with the name "' . $name . '" already exists.');
77
78
            return Command::FAILURE;
79
        }
80
81
        $tree = $this->tree_service->create(name: $name, title: $title);
0 ignored issues
show
Unused Code introduced by
The assignment to $tree is dead and can be removed.
Loading history...
82
83
        DB::exec(sql: 'COMMIT');
84
85
        return Command::SUCCESS;
86
    }
87
}
88