File size: 1,595 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php

declare(strict_types=1);

namespace Mautic\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Mautic\CoreBundle\Configurator\Configurator;
use Mautic\CoreBundle\Doctrine\PreUpAssertionMigration;
use Mautic\CoreBundle\Helper\Dsn\Dsn;

final class Version20230615101328 extends PreUpAssertionMigration
{
    protected function preUpAssertions(): void
    {
        $configurator = $this->getConfigurator();

        $this->skipAssertion(
            fn () => !$configurator->isFileWritable(),
            'The local.php file is not writable. Skipping the email configuration migration.'
        );

        $this->skipAssertion(
            fn () => array_key_exists('mailer_dsn', $configurator->getParameters()),
            'The mailer_dsn parameter is already set. Skipping the email configuration migration.'
        );
    }

    public function up(Schema $schema): void
    {
        $configurator = $this->getConfigurator();
        $parameters   = $configurator->getParameters();
        $dsn          = new Dsn(
            $parameters['mailer_transport'] ?? 'smtp',
            $parameters['mailer_host'] ?? 'localhost',
            $parameters['mailer_user'] ?? null,
            $parameters['mailer_password'] ?? null,
            (int) ($parameters['mailer_port'] ?? 25),
        );

        $parameters['mailer_dsn'] = str_replace('%', '%%', (string) $dsn);

        $configurator->mergeParameters($parameters);
        $configurator->write();
    }

    private function getConfigurator(): Configurator
    {
        return $this->container->get(Configurator::class);
    }
}