Passed
Push — dbal ( c97573...8bdc09 )
by Greg
06:48
created

UserTreeSetting::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 11
rs 9.9666
c 0
b 0
f 0
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\DB;
23
use Symfony\Component\Console\Command\Command;
24
use Symfony\Component\Console\Helper\Table;
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 UserTreeSetting extends Command
32
{
33
    protected function configure(): void
34
    {
35
        $this
36
            ->setName(name: 'user-tree-setting')
37
            ->setDescription(description: 'Configure user-tree settings')
38
            ->addOption(name: 'list', shortcut: 'l', mode: InputOption::VALUE_NONE, description: 'List user-tree settings (optionally filtered by setting name)')
39
            ->addOption(name: 'delete', shortcut: 'd', mode: InputOption::VALUE_NONE, description: 'Delete a user-tree setting')
40
            ->addArgument(name: 'user-name', mode: InputArgument::REQUIRED, description: 'The user to update')
41
            ->addArgument(name: 'tree-name', mode: InputArgument::REQUIRED, description: 'The tree to update')
42
            ->addArgument(name: 'setting-name', mode: InputArgument::OPTIONAL, description: 'The setting to update')
43
            ->addArgument(name: 'setting-value', mode: InputArgument::OPTIONAL, description: 'The new value of the setting.');
44
    }
45
46
    protected function execute(InputInterface $input, OutputInterface $output): int
47
    {
48
        $quiet  = (bool) $input->getOption(name: 'quiet');
49
        $list   = (bool) $input->getOption(name: 'list');
50
        $delete = (bool) $input->getOption(name: 'delete');
51
52
        /** @var string $user_name */
53
        $user_name = $input->getArgument(name: 'user-name');
54
55
        /** @var string $tree_name */
56
        $tree_name = $input->getArgument(name: 'tree-name');
57
58
        /** @var string|null $setting_name */
59
        $setting_name = $input->getArgument(name: 'setting-name');
60
61
        /** @var string|null $setting_value */
62
        $setting_value = $input->getArgument(name: 'setting-value');
63
64
        $io = new SymfonyStyle(input: $input, output: $output);
65
66
        $user_id = DB::table('user')
67
            ->where(column: 'user_name', operator: '=', value: $user_name)
68
            ->value(column: 'user_id');
69
70
        if ($user_id === null) {
71
            $io->error(message: 'User ‘' . $user_name . '’ not found.');
72
73
            return Command::FAILURE;
74
        }
75
76
        $tree_id = DB::table('gedcom')
77
            ->where(column: 'gedcom_name', operator: '=', value: $tree_name)
78
            ->value(column: 'gedcom_id');
79
80
        if ($tree_id === null) {
81
            $io->error(message: 'Tree ‘' . $tree_name . '’ not found.');
82
83
            return Command::FAILURE;
84
        }
85
86
        if ($list) {
87
            if ($delete) {
88
                $io->error(message: 'Cannot specify --list and --delete together.');
89
90
                return Command::FAILURE;
91
            }
92
93
            if ($setting_value !== null) {
94
                $io->error(message: 'Cannot specify --list and a new value together.');
95
96
                return Command::FAILURE;
97
            }
98
99
            $table = new Table(output: $output);
100
            $table->setHeaders(headers: ['Setting name', 'Setting value']);
101
102
            /** @var array<object{setting_name:string,setting_value:string}> $settings */
103
            $settings = DB::table(table: 'user_gedcom_setting')
104
                ->where(column: 'user_id', operator: '=', value: $user_id)
105
                ->where(column: 'gedcom_id', operator: '=', value: $tree_id)
106
                ->orderBy(column: 'setting_name')
107
                ->select(columns: ['setting_name', 'setting_value'])
108
                ->get()
109
                ->all();
110
111
            foreach ($settings as $setting) {
112
                if ($setting_name === null || str_contains(haystack: $setting->setting_name, needle: $setting_name)) {
113
                    $table->addRow(row: [$setting->setting_name, $setting->setting_value]);
114
                }
115
            }
116
117
            $table->render();
118
119
            return Command::SUCCESS;
120
        }
121
122
        /** @var string|null $old_setting_value */
123
        $old_setting_value = DB::table('user_gedcom_setting')
124
            ->where(column: 'user_id', operator: '=', value: $user_id)
125
            ->where(column: 'gedcom_id', operator: '=', value: $tree_id)
126
            ->where(column: 'setting_name', operator: '=', value: $setting_name)
127
            ->value(column: 'setting_value');
128
129
        if ($delete) {
130
            if ($setting_name === null) {
131
                $io->error(message: 'Setting name must be specified for --delete.');
132
133
                return Command::FAILURE;
134
            }
135
136
            if ($setting_value !== null) {
137
                $io->error(message: 'Cannot specify --delete and a new value together.');
138
139
                return Command::FAILURE;
140
            }
141
142
            if ($old_setting_value === null) {
143
                $io->warning(message: 'User-tree setting ‘' . $setting_name . '’ not found.  Nothing to delete.');
144
            } else {
145
                DB::table('user_gedcom_setting')
146
                    ->where(column: 'user_id', operator: '=', value: $user_id)
147
                    ->where(column: 'gedcom_id', operator: '=', value: $tree_id)
148
                    ->where('setting_name', '=', $setting_name)
149
                    ->delete();
150
151
                $io->success(message: 'User-tree setting ‘' . $setting_name . '’ deleted.  Previous value was ‘' . $old_setting_value . '’.');
152
            }
153
154
            return Command::SUCCESS;
155
        }
156
157
158
        if ($setting_name === null) {
159
            $io->error(message: 'A setting name is required, unless the --list option is used.');
160
161
            return Command::FAILURE;
162
        }
163
164
        if ($setting_value === null) {
165
            if ($old_setting_value === null) {
166
                $io->info(message: 'User-tree setting ‘' . $setting_name . '’ is not currently set.');
167
            } elseif ($quiet) {
168
                $verbosity = $io->getVerbosity();
169
                $io->setVerbosity(level: OutputInterface::VERBOSITY_NORMAL);
170
                $io->writeln(messages: $old_setting_value);
171
                $io->setVerbosity(level: $verbosity);
172
            } else {
173
                $io->info(message: 'User-tree setting ‘' . $setting_name . '’ is currently set to ‘' . $old_setting_value . '’.');
174
            }
175
176
            return Command::SUCCESS;
177
        }
178
179
        if ($old_setting_value === $setting_value) {
180
            $io->warning(message: 'User-tree setting ' . $setting_name . ' is already set to ' . $setting_value);
181
182
            return Command::SUCCESS;
183
        }
184
185
        if ($old_setting_value === null) {
186
            DB::table(table: 'user_gedcom_setting')
187
                ->insert(values: [
188
                    'user_id'       => $user_id,
189
                    'gedcom_id'     => $tree_id,
190
                    'setting_name'  => $setting_name,
191
                    'setting_value' => $setting_value,
192
                ]);
193
194
            $io->success(message: 'User-tree setting ‘' . $setting_name . '’ was created as ‘' . $setting_value . '’.');
195
        } else {
196
            DB::table(table: 'user_gedcom_setting')
197
                ->where(column: 'user_id', operator: '=', value: $user_id)
198
                ->where(column: 'gedcom_id', operator: '=', value: $tree_id)
199
                ->where(column: 'setting_name', operator: '=', value: $setting_name)
200
                ->update(values: ['setting_value' => $setting_value]);
201
202
            $io->success(message: 'User-tree setting ‘' . $setting_name . '’ was changed from ‘' . $old_setting_value . '’ to ‘' . $setting_value . '’.');
203
        }
204
205
        return Command::SUCCESS;
206
    }
207
}
208