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

Version20250721200725::up()   C

Complexity

Conditions 10
Paths 64

Size

Total Lines 86
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 62
c 1
b 0
f 0
nc 64
nop 1
dl 0
loc 86
rs 6.9624

How to fix   Long Method    Complexity   

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
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
                    "INSERT IGNORE INTO settings (variable, subkey, type, category, selected_value, title, comment, scope, subkeytext, access_url, access_url_changeable, access_url_locked) VALUES ('%s', null, null, 'mail', %s, '%s', null, '', null, 1, 1, 1)",
101
                    $variable,
102
                    $value,
103
                    $variable
104
                ),
105
            );
106
        }
107
    }
108
}