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

ConfigIni   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 66
c 1
b 0
f 0
dl 0
loc 94
rs 10
wmc 7

2 Methods

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