Passed
Push — master ( bd1da1...a65303 )
by Jan
04:33
created

AbstractMultiPlatformMigration::down()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 14
rs 9.9
c 1
b 0
f 0
1
<?php
2
3
4
namespace App\Migrations;
5
6
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\Schema\Schema;
9
use Doctrine\Migrations\AbstractMigration;
10
11
abstract class AbstractMultiPlatformMigration extends AbstractMigration
12
{
13
    public const ADMIN_PW_LENGTH = 10;
14
15
    protected $permissions_updated = false;
16
    protected $admin_pw = "";
17
18
    public function up(Schema $schema): void
19
    {
20
        $db_type = $this->connection->getDatabasePlatform()->getName();
21
22
        switch ($db_type) {
23
            case 'mysql':
24
                $this->mySQLUp($schema);
25
                break;
26
            case 'sqlite':
27
                $this->sqLiteUp($schema);
28
                break;
29
            default:
30
                $this->abortIf(true, "Database type '$db_type' is not supported!");
31
                break;
32
        }
33
    }
34
35
    public function down(Schema $schema): void
36
    {
37
        $db_type = $this->connection->getDatabasePlatform()->getName();
38
39
        switch ($db_type) {
40
            case 'mysql':
41
                $this->mySQLDown($schema);
42
                break;
43
            case 'sqlite':
44
                $this->sqLiteDown($schema);
45
                break;
46
            default:
47
                $this->abortIf(true, "Database type '$db_type' is not supported!");
48
                break;
49
        }
50
    }
51
52
    /**
53
     * Gets the legacy Part-DB version number. Returns 0, if target database is not an legacy Part-DB database.
54
     * @return int
55
     */
56
    public function getOldDBVersion(): int
57
    {
58
        if ($this->connection->getDatabasePlatform()->getName() !== "mysql") {
59
            //Old Part-DB version only supported MySQL therefore only
60
            return 0;
61
        }
62
63
        try {
64
            return (int) $this->connection->fetchColumn("SELECT keyValue AS version FROM `internal` WHERE `keyName` = 'dbVersion'");
65
        } catch (DBALException $dBALException) {
66
            //when the table was not found, we can proceed, because we have an empty DB!
67
            return 0;
68
        }
69
    }
70
71
    /**
72
     * Returns the hash of a new random password, created for the initial admin user, which can be written to DB.
73
     * The plaintext version of the password will be outputed to user after this migration.
74
     * @return string
75
     */
76
    public function getInitalAdminPW(): string
77
    {
78
        if (empty($this->admin_pw)) {
79
            if (!empty($_ENV['INITIAL_ADMIN_PW'])) {
80
                $this->admin_pw = $_ENV['INITIAL_ADMIN_PW'];
81
            } else {
82
                $this->admin_pw = substr(md5(random_bytes(10)), 0, static::ADMIN_PW_LENGTH);
83
            }
84
        }
85
86
        //As we dont have access to container, just use the default PHP pw hash function
87
        return password_hash($this->admin_pw, PASSWORD_DEFAULT);
88
    }
89
90
    public function printPermissionUpdateMessage(): void
91
    {
92
        $this->permissions_updated = true;
93
    }
94
95
    public function postUp(Schema $schema): void
96
    {
97
        parent::postUp($schema);
98
        $this->write('<question>[!!!] Permissions were updated! Please check if they fit your expectations!</question>');
99
100
        if (!empty($this->admin_pw)) {
101
            $this->write('');
102
            $this->write('<bg=yellow;fg=black>The initial password for the "admin" user is: ' . $this->admin_pw . '</>');
103
        }
104
    }
105
106
    abstract public function mySQLUp(Schema $schema): void;
107
108
    abstract public function mySQLDown(Schema $schema): void;
109
110
    abstract public function sqLiteUp(Schema $schema): void;
111
112
    abstract public function sqLiteDown(Schema $schema): void;
113
}