Passed
Pull Request — master (#6495)
by Angel Fernando Quiroz
08:00
created

Version20250707212800   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 58
c 2
b 0
f 0
dl 0
loc 94
rs 10
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
B migrateMailConf() 0 64 8
A updateEnvFiles() 0 8 2
A up() 0 9 2
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
10
use Doctrine\DBAL\Schema\Schema;
11
12
use const FILE_IGNORE_NEW_LINES;
13
use const PHP_EOL;
14
15
final class Version20250707212800 extends AbstractMigrationChamilo
16
{
17
    public function getDescription(): string
18
    {
19
        return 'Migrate settings from mail.conf.php to settings';
20
    }
21
22
    public function up(Schema $schema): void
23
    {
24
        $updateRootPath = $this->getUpdateRootPath();
25
        $oldMailConfPath = $updateRootPath.'/app/config/mail.conf.php';
26
27
        $envSettings = $this->migrateMailConf($oldMailConfPath);
28
29
        if (!empty($envSettings)) {
30
            $this->updateEnvFiles($envSettings);
31
        }
32
    }
33
34
    private function migrateMailConf(string $oldMailConfPath): array
35
    {
36
        if (!file_exists($oldMailConfPath)) {
37
            return [];
38
        }
39
40
        include $oldMailConfPath;
41
42
        global $platform_email;
43
44
        $mailerScheme = 'null';
45
        $query = '';
46
47
        if (!empty($smtpSecure)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $smtpSecure seems to never exist and therefore empty should always be true.
Loading history...
48
            $mailerScheme = 'smtp';
49
50
            if ($smtpSecure === 'ssl') {
51
                $mailerScheme = 'smtps';
52
            } elseif ($smtpSecure === 'tls') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $smtpSecure seems to be never defined.
Loading history...
53
                $query = '?encryption=tls';
54
            }
55
        }
56
57
        $dsn = sprintf(
58
            '%s://%s%s@%s:%s%s',
59
            $mailerScheme,
60
            !empty($platform_email['SMTP_AUTH']) ? ($platform_email['SMTP_USER'] ?? '') : '',
61
            !empty($platform_email['SMTP_AUTH']) ? ':'.($platform_email['SMTP_PASS'] ?? '') : '',
62
            $platform_email['SMTP_HOST'] ?? '',
63
            $platform_email['SMTP_PORT'] ?? '',
64
            $query
65
        );
66
67
        $dkim = [
68
            'enable' => $platform_email['DKIM'] ?? false,
69
            'selector' => $platform_email['DKIM_SELECTOR'] ?? '',
70
            'domain' => $platform_email['DKIM_DOMAIN'] ?? '',
71
            'private_key_string' => $platform_email['DKIM_PRIVATE_KEY_STRING'] ?? '',
72
            'private_key' => $platform_email['DKIM_PRIVATE_KEY'] ?? '',
73
            'passphrase' => $platform_email['DKIM_PASSPHRASE'] ?? '',
74
        ];
75
76
        $xoauth2 = [
77
            'method' => $platform_email['XOAUTH2_METHOD'] ?? false,
78
            'url_authorize' => $platform_email['XOAUTH2_URL_AUTHORIZE'] ?? '',
79
            'url_access_token' => $platform_email['XOAUTH2_URL_ACCES_TOKEN'] ?? '',
80
            'url_resource_owner_details' => $platform_email['XOAUTH2_URL_RESOURCE_OWNER_DETAILS'] ?? '',
81
            'scopes' => $platform_email['XOAUTH2_SCOPES'] ?? '',
82
            'client_id' => $platform_email['XOAUTH2_CLIENT_ID'] ?? '',
83
            'client_secret' => $platform_email['XOAUTH2_CLIENT_SECRET'] ?? '',
84
            'refresh_token' => $platform_email['XOAUTH2_REFRESH_TOKEN'] ?? '',
85
        ];
86
87
        // SMTP_UNIQUE_SENDER intentionally ignored as requested.
88
89
        return [
90
            'mailer_from_email' => $platform_email['SMTP_FROM_EMAIL'] ?? '',
91
            'mailer_from_name' => $platform_email['SMTP_FROM_NAME'] ?? '',
92
            'mailer_dsn' => $dsn,
93
            'mailer_mails_charset' => $platform_email['SMTP_CHARSET'] ?? 'UTF-8',
94
            'mailer_debug_enable' => !empty($platform_email['SMTP_DEBUG']) ? 'true' : 'false',
95
            'mailer_exclude_json' => $platform_email['EXCLUDE_JSON'] ?? false,
96
            'mailer_dkim' => json_encode($dkim),
97
            'mailer_xoauth2' => json_encode($xoauth2),
98
        ];
99
    }
100
101
    private function updateEnvFiles(array $envSettings): void
102
    {
103
        foreach ($envSettings as $variable => $value) {
104
            $this->addSql(
105
                sprintf(
106
                    "UPDATE settings SET selected_value = '%s' WHERE variable = '%s' AND category = 'mail'",
107
                    $value,
108
                    $variable
109
                )
110
            );
111
        }
112
    }
113
}
114