Issues (2560)

app/Cli/Commands/SiteSetting.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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;
0 ignored issues
show
The type Fisharebest\Webtrees\DB was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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