Passed
Pull Request — master (#5711)
by
unknown
08:23
created

Version20240806120000::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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
use Symfony\Component\Yaml\Yaml;
12
13
final class Version20240806120000 extends AbstractMigrationChamilo
14
{
15
    public function getDescription(): string
16
    {
17
        return 'Migrate settings from configuration.php to .env and hosting_limits.yml files';
18
    }
19
20
    public function up(Schema $schema): void
21
    {
22
        global $_configuration;
23
24
        $rootPath = $this->getRootPath();
25
        $oldConfigPath = $rootPath . '/app/config/configuration.php';
26
        if (!\in_array($oldConfigPath, get_included_files(), true)) {
27
            include_once $oldConfigPath;
28
        }
29
30
        // Update .env and .env.local files
31
        $this->updateEnvFiles($rootPath, [
32
            "DB_MANAGER_ENABLED" => $_configuration['db_manager_enabled'] ? '1' : '0',
33
            "SECURITY_KEY" => $_configuration['security_key'],
34
            "SOFTWARE_NAME" => $_configuration['software_name'],
35
            "SOFTWARE_URL" => $_configuration['software_url'],
36
            "DENY_DELETE_USERS" => $_configuration['deny_delete_users'] ? '1' : '0',
37
            "HOSTING_TOTAL_SIZE_LIMIT" => $_configuration['hosting_total_size_limit']
38
        ]);
39
40
        // Ensure the hosting_limits.yml file exists
41
        $hostingLimitsFile = $rootPath . '/config/hosting_limits.yml';
42
        $hostingLimits = ['hosting_limits' => ['urls' => []]];
43
44
        // Prepare hosting limits
45
        foreach ($_configuration as $urlId => $config) {
46
            if (is_array($config)) {
47
                $hostingLimits['hosting_limits']['urls'][$urlId] = [
48
                    ['hosting_limit_users' => $config['hosting_limit_users'] ?? 0],
49
                    ['hosting_limit_teachers' => $config['hosting_limit_teachers'] ?? 0],
50
                    ['hosting_limit_courses' => $config['hosting_limit_courses'] ?? 0],
51
                    ['hosting_limit_sessions' => $config['hosting_limit_sessions'] ?? 0],
52
                    ['hosting_limit_disk_space' => $config['hosting_limit_disk_space'] ?? 0],
53
                    ['hosting_limit_active_courses' => $config['hosting_limit_active_courses'] ?? 0],
54
                    ['hosting_total_size_limit' => $_configuration['hosting_total_size_limit'] ?? 0]
55
                ];
56
            }
57
        }
58
59
        // Format hosting limits as YAML
60
        $yamlContent = "hosting_limits:\n    urls:\n";
61
        foreach ($hostingLimits['hosting_limits']['urls'] as $urlId => $limits) {
62
            $yamlContent .= "        {$urlId}:\n";
63
            foreach ($limits as $limit) {
64
                foreach ($limit as $key => $value) {
65
                    $yamlContent .= "            - {$key}: {$value}\n";
66
                }
67
            }
68
        }
69
70
        // Write hosting limits to hosting_limits.yml
71
        file_put_contents($hostingLimitsFile, $yamlContent);
72
    }
73
74
    public function down(Schema $schema): void
75
    {}
76
77
    private function getRootPath(): string
78
    {
79
        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

79
        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...
80
    }
81
82
    private function updateEnvFiles(string $rootPath, array $envSettings): void
83
    {
84
        $envFiles = [$rootPath . '/.env', $rootPath . '/.env.local'];
85
86
        foreach ($envFiles as $envFile) {
87
            if (file_exists($envFile)) {
88
                $lines = file($envFile, FILE_IGNORE_NEW_LINES);
89
                $updatedLines = [];
90
                $existingKeys = [];
91
92
                foreach ($lines as $line) {
93
                    if (strpos($line, '=') !== false) {
94
                        [$key, $value] = explode('=', $line, 2);
95
                        $key = trim($key);
96
                        if (array_key_exists($key, $envSettings)) {
97
                            $value = $envSettings[$key];
98
                            unset($envSettings[$key]);
99
                        }
100
                        $updatedLines[] = "{$key}={$value}";
101
                        $existingKeys[] = $key;
102
                    } else {
103
                        $updatedLines[] = $line;
104
                    }
105
                }
106
107
                // Add remaining new settings
108
                foreach ($envSettings as $key => $value) {
109
                    if (!in_array($key, $existingKeys)) {
110
                        $updatedLines[] = "{$key}={$value}";
111
                    }
112
                }
113
114
                file_put_contents($envFile, implode(PHP_EOL, $updatedLines) . PHP_EOL);
115
            } else {
116
                // If the file does not exist, create it with the settings
117
                $newContent = [];
118
                foreach ($envSettings as $key => $value) {
119
                    $newContent[] = "{$key}={$value}";
120
                }
121
                file_put_contents($envFile, implode(PHP_EOL, $newContent) . PHP_EOL);
122
            }
123
        }
124
    }
125
}
126