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

Version20250707212800::migrateMailConf()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 65
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 8
eloc 46
c 3
b 0
f 0
nc 9
nop 1
dl 0
loc 65
rs 7.9337

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        $smtpSecure = $platform_email['SMTP_SECURE'] ?? '';
46
        $query = '';
47
48
        if (!empty($smtpSecure)) {
49
            $mailerScheme = 'smtp';
50
51
            if ($smtpSecure === 'ssl') {
52
                $mailerScheme = 'smtps';
53
            } elseif ($smtpSecure === 'tls') {
54
                $query = '?encryption=tls';
55
            }
56
        }
57
58
        $dsn = sprintf(
59
            '%s://%s%s@%s:%s%s',
60
            $mailerScheme,
61
            !empty($platform_email['SMTP_AUTH']) ? ($platform_email['SMTP_USER'] ?? '') : '',
62
            !empty($platform_email['SMTP_AUTH']) ? ':'.($platform_email['SMTP_PASS'] ?? '') : '',
63
            $platform_email['SMTP_HOST'] ?? '',
64
            $platform_email['SMTP_PORT'] ?? '',
65
            $query
66
        );
67
68
        $dkim = [
69
            'enable' => $platform_email['DKIM'] ?? false,
70
            'selector' => $platform_email['DKIM_SELECTOR'] ?? '',
71
            'domain' => $platform_email['DKIM_DOMAIN'] ?? '',
72
            'private_key_string' => $platform_email['DKIM_PRIVATE_KEY_STRING'] ?? '',
73
            'private_key' => $platform_email['DKIM_PRIVATE_KEY'] ?? '',
74
            'passphrase' => $platform_email['DKIM_PASSPHRASE'] ?? '',
75
        ];
76
77
        $xoauth2 = [
78
            'method' => $platform_email['XOAUTH2_METHOD'] ?? false,
79
            'url_authorize' => $platform_email['XOAUTH2_URL_AUTHORIZE'] ?? '',
80
            'url_access_token' => $platform_email['XOAUTH2_URL_ACCES_TOKEN'] ?? '',
81
            'url_resource_owner_details' => $platform_email['XOAUTH2_URL_RESOURCE_OWNER_DETAILS'] ?? '',
82
            'scopes' => $platform_email['XOAUTH2_SCOPES'] ?? '',
83
            'client_id' => $platform_email['XOAUTH2_CLIENT_ID'] ?? '',
84
            'client_secret' => $platform_email['XOAUTH2_CLIENT_SECRET'] ?? '',
85
            'refresh_token' => $platform_email['XOAUTH2_REFRESH_TOKEN'] ?? '',
86
        ];
87
88
        // SMTP_UNIQUE_SENDER intentionally ignored as requested.
89
90
        return [
91
            'mailer_from_email' => $platform_email['SMTP_FROM_EMAIL'] ?? '',
92
            'mailer_from_name' => $platform_email['SMTP_FROM_NAME'] ?? '',
93
            'mailer_dsn' => $dsn,
94
            'mailer_mails_charset' => $platform_email['SMTP_CHARSET'] ?? 'UTF-8',
95
            'mailer_debug_enable' => !empty($platform_email['SMTP_DEBUG']) ? 'true' : 'false',
96
            'mailer_exclude_json' => $platform_email['EXCLUDE_JSON'] ?? false,
97
            'mailer_dkim' => json_encode($dkim),
98
            'mailer_xoauth2' => json_encode($xoauth2),
99
        ];
100
    }
101
102
    private function updateEnvFiles(array $envSettings): void
103
    {
104
        foreach ($envSettings as $variable => $value) {
105
            $this->addSql(
106
                sprintf(
107
                    "UPDATE settings SET selected_value = '%s' WHERE variable = '%s' AND category = 'mail'",
108
                    $value,
109
                    $variable
110
                )
111
            );
112
        }
113
    }
114
}
115