Version20180320164351::up()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 25
rs 9.7666
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the mailserver-admin package.
6
 * (c) Jeffrey Boehm <https://github.com/jeboehm/mailserver-admin>
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace App\Migrations;
12
13
use Doctrine\DBAL\Schema\Schema;
14
use Doctrine\Migrations\AbstractMigration;
15
use Exception;
16
17
class Version20180320164351 extends AbstractMigration
18
{
19
    private array $users = [];
20
21
    private array $aliases = [];
22
23
    public function preUp(Schema $schema): void
24
    {
25
        $this->fillUsers();
26
        $this->fillAliases();
27
    }
28
29
    public function up(Schema $schema): void
30
    {
31
        $this->abortIf(
32
            'mysql' !== $this->connection->getDatabasePlatform()->getName(),
33
            "Migration can only be executed safely on 'mysql'."
34
        );
35
36
        $this->addSql('SET FOREIGN_KEY_CHECKS = 0');
37
38
        $this->addSql('RENAME TABLE virtual_domains TO mail_domains');
39
40
        $this->addSql(
41
            'CREATE TABLE mail_aliases (id INT AUTO_INCREMENT NOT NULL, domain_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, destination VARCHAR(255) NOT NULL, INDEX IDX_5F12BB39115F0EE5 (domain_id), UNIQUE INDEX alias_idx (domain_id, name, destination), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'
42
        );
43
        $this->addSql(
44
            'CREATE TABLE mail_users (id INT AUTO_INCREMENT NOT NULL, domain_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, UNIQUE INDEX UNIQ_1483A5E95E237E06 (name), INDEX IDX_1483A5E9115F0EE5 (domain_id), UNIQUE INDEX user_idx (name, domain_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'
45
        );
46
        $this->addSql(
47
            'ALTER TABLE mail_aliases ADD CONSTRAINT FK_5F12BB39115F0EE5 FOREIGN KEY (domain_id) REFERENCES mail_domains (id)'
48
        );
49
        $this->addSql(
50
            'ALTER TABLE mail_users ADD CONSTRAINT FK_1483A5E9115F0EE5 FOREIGN KEY (domain_id) REFERENCES mail_domains (id)'
51
        );
52
        $this->addSql('DROP TABLE virtual_aliases');
53
        $this->addSql('DROP TABLE virtual_users');
54
    }
55
56
    public function postUp(Schema $schema): void
57
    {
58
        foreach ($this->users as $user) {
59
            $this->connection->insert('mail_users', $user);
60
        }
61
62
        foreach ($this->aliases as $alias) {
63
            $this->connection->insert('mail_aliases', $alias);
64
        }
65
    }
66
67
    public function down(Schema $schema): void
68
    {
69
        throw new Exception('Not implemented');
70
    }
71
72
    private function fillUsers(): void
73
    {
74
        $qb = $this->connection->createQueryBuilder();
75
        $qb
76
            ->addSelect('email')
77
            ->addSelect('domain_id')
78
            ->addSelect('password')
79
            ->from('virtual_users');
80
81
        $result = $qb->execute()->fetchAll();
0 ignored issues
show
Bug introduced by
The method fetchAll() does not exist on integer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        $result = $qb->execute()->/** @scrutinizer ignore-call */ fetchAll();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
83
        foreach ($result as $row) {
84
            $this->users[] = [
85
                'name' => explode('@', $row['email'], 2)[0],
86
                'domain_id' => $row['domain_id'],
87
                'password' => $row['password'],
88
            ];
89
        }
90
    }
91
92
    private function fillAliases(): void
93
    {
94
        $qb = $this->connection->createQueryBuilder();
95
        $qb
96
            ->addSelect('source')
97
            ->addSelect('domain_id')
98
            ->addSelect('destination')
99
            ->from('virtual_aliases');
100
101
        $result = $qb->execute()->fetchAll();
102
103
        foreach ($result as $row) {
104
            $this->aliases[] = [
105
                'name' => explode('@', $row['source'], 2)[0],
106
                'domain_id' => $row['domain_id'],
107
                'destination' => $row['destination'],
108
            ];
109
        }
110
    }
111
}
112