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

SiteSetting::execute()   C

Complexity

Conditions 17
Paths 16

Size

Total Lines 124
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

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