Completed
Pull Request — master (#220)
by Alejandro
03:52
created

DatabaseConfigCustomizer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B process() 0 38 6
A getDefaultDbPort() 0 4 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Installer\Config\Plugin;
5
6
use Shlinkio\Shlink\Installer\Model\CustomizableAppConfig;
7
use Symfony\Component\Console\Style\SymfonyStyle;
8
use Symfony\Component\Filesystem\Exception\IOException;
9
use Symfony\Component\Filesystem\Filesystem;
10
11
class DatabaseConfigCustomizer implements ConfigCustomizerInterface
12
{
13
    private const DATABASE_DRIVERS = [
14
        'MySQL' => 'pdo_mysql',
15
        'PostgreSQL' => 'pdo_pgsql',
16
        'SQLite' => 'pdo_sqlite',
17
    ];
18
19
    /**
20
     * @var Filesystem
21
     */
22
    private $filesystem;
23
24
    public function __construct(Filesystem $filesystem)
25
    {
26
        $this->filesystem = $filesystem;
27
    }
28
29
    /**
30
     * @throws IOException
31
     */
32
    public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void
33
    {
34
        $io->title('DATABASE');
35
36
        if ($appConfig->hasDatabase() && $io->confirm('Do you want to keep imported database config?')) {
37
            // If the user selected to keep DB config and is configured to use sqlite, copy DB file
38
            if ($appConfig->getDatabase()['DRIVER'] === self::DATABASE_DRIVERS['SQLite']) {
39
                try {
40
                    $this->filesystem->copy(
41
                        $appConfig->getImportedInstallationPath() . '/' . CustomizableAppConfig::SQLITE_DB_PATH,
42
                        CustomizableAppConfig::SQLITE_DB_PATH
43
                    );
44
                } catch (IOException $e) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Filesystem\Exception\IOException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
45
                    $io->error('It wasn\'t possible to import the SQLite database');
46
                    throw $e;
47
                }
48
            }
49
50
            return;
51
        }
52
53
        // Select database type
54
        $params = [];
55
        $databases = \array_keys(self::DATABASE_DRIVERS);
56
        $dbType = $io->choice('Select database type', $databases, $databases[0]);
57
        $params['DRIVER'] = self::DATABASE_DRIVERS[$dbType];
58
59
        // Ask for connection params if database is not SQLite
60
        if ($params['DRIVER'] !== self::DATABASE_DRIVERS['SQLite']) {
61
            $params['NAME'] = $io->ask('Database name', 'shlink');
62
            $params['USER'] = $io->ask('Database username');
63
            $params['PASSWORD'] = $io->ask('Database password');
64
            $params['HOST'] = $io->ask('Database host', 'localhost');
65
            $params['PORT'] = $io->ask('Database port', $this->getDefaultDbPort($params['DRIVER']));
66
        }
67
68
        $appConfig->setDatabase($params);
69
    }
70
71
    private function getDefaultDbPort(string $driver): string
72
    {
73
        return $driver === 'pdo_mysql' ? '3306' : '5432';
74
    }
75
}
76