Completed
Pull Request — master (#220)
by Alejandro
07:25
created

DatabaseConfigCustomizer::process()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 2
dl 0
loc 38
rs 8.6897
c 0
b 0
f 0
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) {
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