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

TreeSetting::execute()   D

Complexity

Conditions 18
Paths 17

Size

Total Lines 142
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 84
nc 17
nop 2
dl 0
loc 142
rs 4.8666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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