Passed
Push — main ( 950f85...4bfa5d )
by Greg
07:33
created

ConfigIni::execute()   B

Complexity

Conditions 6
Paths 64

Size

Total Lines 59
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 42
c 1
b 0
f 0
nc 64
nop 2
dl 0
loc 59
rs 8.6257

How to fix   Long Method   

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) 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
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 Fisharebest\Webtrees\Webtrees;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Webtrees 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...
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Input\InputOption;
26
use Symfony\Component\Console\Output\OutputInterface;
27
use Symfony\Component\Console\Style\SymfonyStyle;
28
use Throwable;
29
30
use function file_exists;
31
use function file_put_contents;
32
use function parse_ini_file;
33
34
final class ConfigIni extends AbstractCommand
35
{
36
    protected function configure(): void
37
    {
38
        if (file_exists(filename: Webtrees::CONFIG_FILE)) {
39
            $config = parse_ini_file(filename: Webtrees::CONFIG_FILE);
40
41
            if ($config === false) {
42
                $config = [];
43
            }
44
        } else {
45
            $config = [];
46
        }
47
48
        $this
49
            ->setName(name: 'config-ini')
50
            ->setDescription(description: 'Set values in data/config.ini.php')
51
            ->addOption(name: 'dbtype', mode: InputOption::VALUE_OPTIONAL, description: 'Database type', default: $config['dbtype'] ?? 'mysql')
52
            ->addOption(name: 'dbhost', mode: InputOption::VALUE_OPTIONAL, description: 'Database host', default: $config['dbhost'] ?? '')
53
            ->addOption(name: 'dbport', mode: InputOption::VALUE_OPTIONAL, description: 'Database port', default: $config['dbport'] ?? '')
54
            ->addOption(name: 'dbuser', mode: InputOption::VALUE_OPTIONAL, description: 'Database user', default: $config['dbuser'] ?? '')
55
            ->addOption(name: 'dbpass', mode: InputOption::VALUE_OPTIONAL, description: 'Database password', default: $config['dbpass'] ?? '')
56
            ->addOption(name: 'dbname', mode: InputOption::VALUE_OPTIONAL, description: 'Database name', default: $config['dbname'] ?? 'webtrees')
57
            ->addOption(name: 'dbkey', mode: InputOption::VALUE_OPTIONAL, description: 'Location of SSL key for encrypted database connection', default: $config['dbkey'] ?? '')
58
            ->addOption(name: 'dbcert', mode: InputOption::VALUE_OPTIONAL, description: 'Location of SSL certificate for encrypted database connection', default: $config['dbcert'] ?? '')
59
            ->addOption(name: 'dbca', mode: InputOption::VALUE_OPTIONAL, description: 'Location of certificate authority file for encrypted database connection', default: $config['dbca'] ?? '')
60
            ->addOption(name: 'dbverify', mode: InputOption::VALUE_NEGATABLE, description: 'Verify SSL certificate', default: (bool) ($config['dbverify'] ?? false))
61
            ->addOption(name: 'tblpfx', mode: InputOption::VALUE_OPTIONAL, description: 'Table prefix', default: $config['tblpfx'] ?? '')
62
            ->addOption(name: 'base-url', mode: InputOption::VALUE_OPTIONAL, description: 'Base URL', default: $config['base_url'] ?? '')
63
            ->addOption(name: 'rewrite-urls', mode: InputOption::VALUE_NEGATABLE, description: 'Use pretty URLs', default: (bool) ($config['rewrite_urls'] ?? false))
64
            ->addOption(name: 'block-asn', mode: InputOption::VALUE_OPTIONAL, description: 'List of ASNs to block', default: $config['block_asn'] ?? '');
65
    }
66
67
    protected function execute(InputInterface $input, OutputInterface $output): int
68
    {
69
        $io = new SymfonyStyle(input: $input, output: $output);
70
71
        $data = '; <?php return; ?> DO NOT DELETE THIS LINE' . PHP_EOL;
72
73
        $config = [
74
            'dbtype'       => $this->stringOption(input: $input, name: 'dbtype'),
75
            'dbhost'       => $this->stringOption(input: $input, name: 'dbhost'),
76
            'dbport'       => $this->stringOption(input: $input, name: 'dbport'),
77
            'dbuser'       => $this->stringOption(input: $input, name: 'dbuser'),
78
            'dbpass'       => $this->stringOption(input: $input, name: 'dbpass'),
79
            'dbname'       => $this->stringOption(input: $input, name: 'dbname'),
80
            'dbkey'        => $this->stringOption(input: $input, name: 'dbkey'),
81
            'dbcert'       => $this->stringOption(input: $input, name: 'dbcert'),
82
            'dbca'         => $this->stringOption(input: $input, name: 'dbca'),
83
            'dbverify'     => $this->boolOption(input: $input, name: 'dbverify') ? '1' : '0',
84
            'tblpfx'       => $this->stringOption(input: $input, name: 'tblpfx'),
85
            'base_url'     => rtrim(string: $this->stringOption(input: $input, name: 'base-url'), characters: '/'),
86
            'rewrite_urls' => $this->boolOption(input: $input, name: 'rewrite-urls') ? '1' : '0',
87
            'block_asn'    => $this->stringOption(input: $input, name: 'block-asn'),
88
        ];
89
90
        foreach ($config as $key => $value) {
91
            $data .= $key . ' = "' . addcslashes(string: $value, characters: '"') . '"' . PHP_EOL;
92
        }
93
94
        $io->info(message: $data);
95
        file_put_contents(filename: Webtrees::CONFIG_FILE, data: $data);
96
97
        if ($config['base_url'] === '') {
98
            $io->warning(message: 'You must set the base URL');
99
        }
100
101
        try {
102
            $config = parse_ini_file(filename: Webtrees::CONFIG_FILE);
103
104
            DB::connect(
105
                driver: $config['dbtype'],
106
                host: $config['dbhost'],
107
                port: $config['dbport'],
108
                database: $config['dbname'],
109
                username: $config['dbuser'],
110
                password: $config['dbpass'],
111
                prefix: $config['tblpfx'],
112
                key: $config['dbkey'],
113
                certificate: $config['dbcert'],
114
                ca: $config['dbca'],
115
                verify_certificate: (bool) $config['dbverify'],
116
            );
117
118
            $io->success(message: 'Database connection successful');
119
        } catch (Throwable $ex) {
120
            $io->error(message: 'Database connection failed: ' . $ex->getMessage());
121
122
            return self::FAILURE;
123
        }
124
125
        return self::SUCCESS;
126
    }
127
}
128