xref: /webtrees/app/Cli/Commands/TreeCreate.php (revision bb87bb9b28b5fe236541dbaa2be497e0e0e81f05)
1*bb87bb9bSGreg Roach<?php
2*bb87bb9bSGreg Roach
3*bb87bb9bSGreg Roach/**
4*bb87bb9bSGreg Roach * webtrees: online genealogy
5*bb87bb9bSGreg Roach * Copyright (C) 2023 webtrees development team
6*bb87bb9bSGreg Roach * This program is free software: you can redistribute it and/or modify
7*bb87bb9bSGreg Roach * it under the terms of the GNU General Public License as published by
8*bb87bb9bSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*bb87bb9bSGreg Roach * (at your option) any later version.
10*bb87bb9bSGreg Roach * This program is distributed in the hope that it will be useful,
11*bb87bb9bSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*bb87bb9bSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*bb87bb9bSGreg Roach * GNU General Public License for more details.
14*bb87bb9bSGreg Roach * You should have received a copy of the GNU General Public License
15*bb87bb9bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16*bb87bb9bSGreg Roach */
17*bb87bb9bSGreg Roach
18*bb87bb9bSGreg Roachdeclare(strict_types=1);
19*bb87bb9bSGreg Roach
20*bb87bb9bSGreg Roachnamespace Fisharebest\Webtrees\Cli\Commands;
21*bb87bb9bSGreg Roach
22*bb87bb9bSGreg Roachuse Fisharebest\Webtrees\Contracts\TreeInterface;
23*bb87bb9bSGreg Roachuse Fisharebest\Webtrees\DB;
24*bb87bb9bSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
25*bb87bb9bSGreg Roachuse Symfony\Component\Console\Command\Command;
26*bb87bb9bSGreg Roachuse Symfony\Component\Console\Input\InputInterface;
27*bb87bb9bSGreg Roachuse Symfony\Component\Console\Input\InputOption;
28*bb87bb9bSGreg Roachuse Symfony\Component\Console\Output\OutputInterface;
29*bb87bb9bSGreg Roachuse Symfony\Component\Console\Style\SymfonyStyle;
30*bb87bb9bSGreg Roach
31*bb87bb9bSGreg Roachuse function bin2hex;
32*bb87bb9bSGreg Roachuse function random_bytes;
33*bb87bb9bSGreg Roach
34*bb87bb9bSGreg Roachclass TreeCreate extends Command
35*bb87bb9bSGreg Roach{
36*bb87bb9bSGreg Roach    public function __construct(private readonly TreeService $tree_service)
37*bb87bb9bSGreg Roach    {
38*bb87bb9bSGreg Roach        parent::__construct();
39*bb87bb9bSGreg Roach    }
40*bb87bb9bSGreg Roach
41*bb87bb9bSGreg Roach    protected function configure(): void
42*bb87bb9bSGreg Roach    {
43*bb87bb9bSGreg Roach        $this
44*bb87bb9bSGreg Roach            ->setName(name: 'tree-create')
45*bb87bb9bSGreg Roach            ->setDescription(description: 'Create a new tree')
46*bb87bb9bSGreg Roach            ->addOption(name: 'name', shortcut: null, mode: InputOption::VALUE_REQUIRED, description: 'The name of the new tree')
47*bb87bb9bSGreg Roach            ->addOption(name: 'title', shortcut: null, mode: InputOption::VALUE_REQUIRED, description: 'The title of the new tree');
48*bb87bb9bSGreg Roach    }
49*bb87bb9bSGreg Roach
50*bb87bb9bSGreg Roach    protected function execute(InputInterface $input, OutputInterface $output): int
51*bb87bb9bSGreg Roach    {
52*bb87bb9bSGreg Roach        $io = new SymfonyStyle(input: $input, output: $output);
53*bb87bb9bSGreg Roach
54*bb87bb9bSGreg Roach        $name  = $input->getOption(name: 'name');
55*bb87bb9bSGreg Roach        $title = $input->getOption(name: 'title');
56*bb87bb9bSGreg Roach
57*bb87bb9bSGreg Roach        $missing = false;
58*bb87bb9bSGreg Roach
59*bb87bb9bSGreg Roach        if ($name === null) {
60*bb87bb9bSGreg Roach            $io->error(message: 'Missing required option: --name');
61*bb87bb9bSGreg Roach            $missing = true;
62*bb87bb9bSGreg Roach        }
63*bb87bb9bSGreg Roach
64*bb87bb9bSGreg Roach        if ($title === null) {
65*bb87bb9bSGreg Roach            $io->error(message: 'Missing required option: --title');
66*bb87bb9bSGreg Roach            $missing = true;
67*bb87bb9bSGreg Roach        }
68*bb87bb9bSGreg Roach
69*bb87bb9bSGreg Roach        if ($missing) {
70*bb87bb9bSGreg Roach            return Command::INVALID;
71*bb87bb9bSGreg Roach        }
72*bb87bb9bSGreg Roach
73*bb87bb9bSGreg Roach        $tree = $this->tree_service->all()[$name] ?? null;
74*bb87bb9bSGreg Roach
75*bb87bb9bSGreg Roach        if ($tree !== null) {
76*bb87bb9bSGreg Roach            $io->error(message: 'A tree with the name "' . $name . '" already exists.');
77*bb87bb9bSGreg Roach
78*bb87bb9bSGreg Roach            return Command::FAILURE;
79*bb87bb9bSGreg Roach        }
80*bb87bb9bSGreg Roach
81*bb87bb9bSGreg Roach        $tree = $this->tree_service->create(name: $name, title: $title);
82*bb87bb9bSGreg Roach
83*bb87bb9bSGreg Roach        DB::exec(sql: 'COMMIT');
84*bb87bb9bSGreg Roach
85*bb87bb9bSGreg Roach        return Command::SUCCESS;
86*bb87bb9bSGreg Roach    }
87*bb87bb9bSGreg Roach}
88