Passed
Pull Request — master (#6445)
by Yannick
12:16 queued 04:01
created

Version20250707212800::up()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
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 .env';
20
    }
21
22
    public function up(Schema $schema): void
23
    {
24
        $rootPath = $this->getRootPath();
25
        $updateRootPath = $this->getUpdateRootPath();
26
        $oldMailConfPath = $updateRootPath.'/app/config/mail.conf.php';
27
28
        $envSettings = $this->migrateMailConf($oldMailConfPath);
29
30
        if (!empty($envSettings)) {
31
            $this->updateEnvFiles($rootPath, $envSettings);
32
        }
33
    }
34
35
    private function migrateMailConf(string $oldMailConfPath): array
36
    {
37
        $mailerEnv = [];
38
39
        if (file_exists($oldMailConfPath)) {
40
            include $oldMailConfPath;
41
42
            global $platform_email;
43
44
            $mailerEnv = [
45
                'MAILER_FROM_EMAIL' => $platform_email['SMTP_FROM_EMAIL'] ?? '',
46
                'MAILER_FROM_NAME' => $platform_email['SMTP_FROM_NAME'] ?? '',
47
                'MAILER_TRANSPORT' => $platform_email['SMTP_MAILER'] ?? 'mail',
48
                'MAILER_HOST' => $platform_email['SMTP_HOST'] ?? '',
49
                'MAILER_PORT' => $platform_email['SMTP_PORT'] ?? '',
50
                'MAILER_SMTP_AUTH' => !empty($platform_email['SMTP_AUTH']) ? 'true' : 'false',
51
                'MAILER_USER' => $platform_email['SMTP_USER'] ?? '',
52
                'MAILER_PASSWORD' => $platform_email['SMTP_PASS'] ?? '',
53
                'MAILER_CHARSET' => $platform_email['SMTP_CHARSET'] ?? 'UTF-8',
54
                'MAILER_SMTP_DEBUG' => !empty($platform_email['SMTP_DEBUG']) ? 'true' : 'false',
55
                'MAILER_SMTP_SECURE' => $platform_email['SMTP_SECURE'] ?? '',
56
                'MAILER_SMTP_UNIQUE_REPLY_TO' => !empty($platform_email['SMTP_UNIQUE_REPLY_TO']) ? 'true' : 'false',
57
            ];
58
59
            // SMTP_UNIQUE_SENDER intentionally ignored as requested.
60
        }
61
62
        return $mailerEnv;
63
    }
64
65
    private function getRootPath(): string
66
    {
67
        return $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

67
        return $this->container->/** @scrutinizer ignore-call */ 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...
68
    }
69
70
    private function updateEnvFiles(string $rootPath, array $envSettings): void
71
    {
72
        $envFiles = [$rootPath.'/.env'];
73
74
        foreach ($envFiles as $envFile) {
75
            if (file_exists($envFile)) {
76
                $lines = file($envFile, FILE_IGNORE_NEW_LINES);
77
                $updatedLines = [];
78
                $existingKeys = [];
79
80
                foreach ($lines as $line) {
81
                    if (str_contains($line, '=')) {
82
                        [$key, $value] = explode('=', $line, 2);
83
                        $key = trim($key);
84
                        if (\array_key_exists($key, $envSettings)) {
85
                            $value = $envSettings[$key];
86
                            unset($envSettings[$key]);
87
                        }
88
                        $updatedLines[] = "{$key}={$value}";
89
                        $existingKeys[] = $key;
90
                    } else {
91
                        $updatedLines[] = $line;
92
                    }
93
                }
94
95
                foreach ($envSettings as $key => $value) {
96
                    if (!\in_array($key, $existingKeys)) {
97
                        $updatedLines[] = "{$key}={$value}";
98
                    }
99
                }
100
101
                file_put_contents($envFile, implode(PHP_EOL, $updatedLines).PHP_EOL);
102
            } else {
103
                $newContent = [];
104
                foreach ($envSettings as $key => $value) {
105
                    $newContent[] = "{$key}={$value}";
106
                }
107
                file_put_contents($envFile, implode(PHP_EOL, $newContent).PHP_EOL);
108
            }
109
        }
110
    }
111
}
112