Passed
Pull Request — master (#6495)
by Angel Fernando Quiroz
12:49 queued 04:45
created

Version20250721200725   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 62
dl 0
loc 90
rs 10
c 1
b 0
f 0
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C up() 0 85 10
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
use Symfony\Component\Dotenv\Dotenv;
12
13
class Version20250721200725 extends AbstractMigrationChamilo
14
{
15
    /**
16
     * @inheritDoc
17
     */
18
    public function up(Schema $schema): void
19
    {
20
        $projectDir = $this->container->getParameter('kernel.project_dir');
0 ignored issues
show
Bug introduced by
The method getParameter() does not exist on null. ( Ignorable by Annotation )

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

20
        /** @scrutinizer ignore-call */ 
21
        $projectDir = $this->container->getParameter('kernel.project_dir');

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...
21
        $updateRootPath = $this->getUpdateRootPath();
22
        $oldMailConfPath = $updateRootPath.'/app/config/mail.conf.php';
23
24
        $legacyMailConfig = file_exists($oldMailConfPath);
25
26
        if ($legacyMailConfig) {
27
            include $oldMailConfPath;
28
29
            global $platform_email;
30
        }
31
32
        $envFile = $projectDir.'.env';
33
34
        $dotenv = new Dotenv();
35
        $dotenv->loadEnv($envFile);
36
37
        $mailerScheme = 'null';
38
        $smtpSecure = $_ENV['SMTP_SECURE'] ?? '';
39
        $query = '';
40
41
        if (!empty($smtpSecure)) {
42
            $mailerScheme = 'smtp';
43
44
            if ($smtpSecure === 'ssl') {
45
                $mailerScheme = 'smtps';
46
            } elseif ($smtpSecure === 'tls') {
47
                $query = '?encryption=tls';
48
            }
49
        }
50
51
        $dsn = sprintf(
52
            '%s://%s%s@%s:%s%s',
53
            $mailerScheme,
54
            !empty($_ENV['SMTP_AUTH']) ? ($_ENV['SMTP_USER'] ?? '') : '',
55
            !empty($_ENV['SMTP_AUTH']) ? ':'.($_ENV['SMTP_PASS'] ?? '') : '',
56
            $_ENV['SMTP_HOST'] ?? '',
57
            $_ENV['SMTP_PORT'] ?? '',
58
            $query
59
        );
60
61
        $settings = [
62
            'mailer_from_email' => $_ENV['SMTP_FROM_EMAIL'] ?? '',
63
            'mailer_from_name' => $_ENV['SMTP_FROM_NAME'] ?? '',
64
            'mailer_dsn' => $dsn,
65
            'mailer_mails_charset' => $_ENV['SMTP_CHARSET'] ?? 'UTF-8',
66
            'mailer_debug_enable' => !empty($_ENV['SMTP_DEBUG']) ? 'true' : 'false',
67
            'mailer_exclude_json' => $platform_email['EXCLUDE_JSON'] ?? false,
68
            'mailer_dkim' => '',
69
            'mailer_xoauth2' => '',
70
        ];
71
72
        if ($legacyMailConfig) {
73
            $dkim = [
74
                'enable' => $platform_email['DKIM'] ?? false,
75
                'selector' => $platform_email['DKIM_SELECTOR'] ?? '',
76
                'domain' => $platform_email['DKIM_DOMAIN'] ?? '',
77
                'private_key_string' => $platform_email['DKIM_PRIVATE_KEY_STRING'] ?? '',
78
                'private_key' => $platform_email['DKIM_PRIVATE_KEY'] ?? '',
79
                'passphrase' => $platform_email['DKIM_PASSPHRASE'] ?? '',
80
            ];
81
82
            $xoauth2 = [
83
                'method' => $platform_email['XOAUTH2_METHOD'] ?? false,
84
                'url_authorize' => $platform_email['XOAUTH2_URL_AUTHORIZE'] ?? '',
85
                'url_access_token' => $platform_email['XOAUTH2_URL_ACCES_TOKEN'] ?? '',
86
                'url_resource_owner_details' => $platform_email['XOAUTH2_URL_RESOURCE_OWNER_DETAILS'] ?? '',
87
                'scopes' => $platform_email['XOAUTH2_SCOPES'] ?? '',
88
                'client_id' => $platform_email['XOAUTH2_CLIENT_ID'] ?? '',
89
                'client_secret' => $platform_email['XOAUTH2_CLIENT_SECRET'] ?? '',
90
                'refresh_token' => $platform_email['XOAUTH2_REFRESH_TOKEN'] ?? '',
91
            ];
92
93
            $settings['mailer_dkim'] = json_encode($dkim);
94
            $settings['mailer_xoauth2'] = json_encode($xoauth2);
95
96
        }
97
        foreach ($settings as $variable => $value) {
98
            $this->addSql(
99
                sprintf(
100
                    "UPDATE settings SET selected_value = '%s' WHERE variable = '%s' AND category = 'mail'",
101
                    $value,
102
                    $variable
103
                )
104
            );
105
        }
106
    }
107
}