xref: /webtrees/app/Cli/Commands/TreeCreate.php (revision e873f434551745f888937263ff89e80db3b0f785)
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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Cli\Commands;
21
22use Fisharebest\Webtrees\Contracts\TreeInterface;
23use Fisharebest\Webtrees\DB;
24use Fisharebest\Webtrees\Services\TreeService;
25use Symfony\Component\Console\Command\Command;
26use Symfony\Component\Console\Input\InputInterface;
27use Symfony\Component\Console\Input\InputOption;
28use Symfony\Component\Console\Output\OutputInterface;
29use Symfony\Component\Console\Style\SymfonyStyle;
30
31use function bin2hex;
32use function random_bytes;
33
34class 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);
82
83        DB::exec(sql: 'COMMIT');
84
85        return Command::SUCCESS;
86    }
87}
88