Passed
Push — main ( dfe98c...068396 )
by Greg
07:59
created

SiteSetting::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 9
rs 10
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;
0 ignored issues
show
Bug introduced by
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\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
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_value !== null) {
99
                $io->error(message: 'Cannot specify --delete and a new value together.');
100
101
                return Command::FAILURE;
102
            }
103
104
            if ($old_setting_value === null) {
105
                $io->warning(message: 'Site setting ‘' . $setting_name . '’ not found.  Nothing to delete.');
106
            } else {
107
                DB::table('site_setting')
108
                    ->where('setting_name', '=', $setting_name)
109
                    ->delete();
110
111
                $io->success(message: 'Site setting ‘' . $setting_name . '’ deleted.  Previous value was ‘' . $old_setting_value . '’.');
112
            }
113
114
            return Command::SUCCESS;
115
        }
116
117
118
        if ($setting_name === null) {
119
            $io->error(message: 'A setting name is required, unless the --list option is used.');
120
121
            return Command::FAILURE;
122
        }
123
124
        if ($setting_value === null) {
125
            if ($old_setting_value === null) {
126
                $io->info(message: 'Site setting ‘' . $setting_name . '’ is not currently set.');
127
            } elseif ($quiet) {
128
                $verbosity = $io->getVerbosity();
129
                $io->setVerbosity(level: OutputInterface::VERBOSITY_NORMAL);
130
                $io->writeln(messages: $old_setting_value);
131
                $io->setVerbosity(level: $verbosity);
132
            } else {
133
                $io->info(message: 'Site setting ‘' . $setting_name . '’ is currently set to ‘' . $old_setting_value . '’.');
134
            }
135
136
            return Command::SUCCESS;
137
        }
138
139
        if ($old_setting_value === $setting_value) {
140
            $io->warning(message: 'Site setting ' . $setting_name . ' is already set to ' . $setting_value);
141
142
            return Command::SUCCESS;
143
        }
144
145
        if ($old_setting_value === null) {
146
            DB::table('site_setting')
147
                ->insert(['setting_name' => $setting_name, 'setting_value' => $setting_value]);
148
149
            $io->success(message: 'Site setting ‘' . $setting_name . '’ was created as ‘' . $setting_value . '’.');
150
        } else {
151
            DB::table('site_setting')
152
                ->where(column: 'setting_name', operator: '=', value: $setting_name)
153
                ->update(values: ['setting_value' => $setting_value]);
154
155
            $io->success(message: 'Site setting ‘' . $setting_name . '’ was changed from ‘' . $old_setting_value . '’ to ‘' . $setting_value . '’.');
156
        }
157
158
        return Command::SUCCESS;
159
    }
160
}
161