Passed
Push — dependabot/npm_and_yarn/microm... ( e84ba6...f2f212 )
by
unknown
10:03
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
12
use const FILE_IGNORE_NEW_LINES;
13
use const PHP_EOL;
14
15
final class Version20240806120000 extends AbstractMigrationChamilo
16
{
17
    public function getDescription(): string
18
    {
19
        return 'Migrate settings from configuration.php to .env and hosting_limits.yml files';
20
    }
21
22
    public function up(Schema $schema): void
23
    {
24
        global $_configuration;
25
26
        $rootPath = $this->getRootPath();
27
        $updateRootPath = $this->getUpdateRootPath();
28
        $oldConfigPath = $updateRootPath.'/app/config/configuration.php';
29
        if (!\in_array($oldConfigPath, get_included_files(), true)) {
30
            include_once $oldConfigPath;
31
        }
32
33
        // Update .env and .env.local files
34
        $this->updateEnvFiles($rootPath, [
35
            'DB_MANAGER_ENABLED' => !empty($_configuration['db_manager_enabled']) ? '1' : '0',
36
            'SOFTWARE_NAME' => $_configuration['software_name'] ?? '',
37
            'SOFTWARE_URL' => $_configuration['software_url'] ?? '',
38
            'DENY_DELETE_USERS' => !empty($_configuration['deny_delete_users']) ? '1' : '0',
39
            'HOSTING_TOTAL_SIZE_LIMIT' => $_configuration['hosting_total_size_limit'] ?? 0,
40
        ]);
41
42
        // Ensure the hosting_limits.yml file exists
43
        $hostingLimitsFile = $rootPath.'/config/hosting_limits.yml';
44
        $hostingLimits = ['hosting_limits' => ['urls' => []]];
45
46
        // Prepare hosting limits
47
        if (\is_array($_configuration)) {
48
            foreach ($_configuration as $key => $config) {
49
                if (is_numeric($key) && \is_array($config)) {
50
                    // Handle configurations specific to URL IDs
51
                    $hostingLimits['hosting_limits']['urls'][$key] = [
52
                        ['hosting_limit_users' => $config['hosting_limit_users'] ?? 0],
53
                        ['hosting_limit_teachers' => $config['hosting_limit_teachers'] ?? 0],
54
                        ['hosting_limit_courses' => $config['hosting_limit_courses'] ?? 0],
55
                        ['hosting_limit_sessions' => $config['hosting_limit_sessions'] ?? 0],
56
                        ['hosting_limit_disk_space' => $config['hosting_limit_disk_space'] ?? 0],
57
                        ['hosting_limit_active_courses' => $config['hosting_limit_active_courses'] ?? 0],
58
                        ['hosting_total_size_limit' => $_configuration['hosting_total_size_limit'] ?? 0],
59
                    ];
60
                }
61
            }
62
        }
63
64
        // Format hosting limits as YAML
65
        $yamlContent = "parameters:\n  hosting_limits:\n    urls:\n";
66
        foreach ($hostingLimits['hosting_limits']['urls'] as $urlId => $limits) {
67
            $yamlContent .= "      {$urlId}:\n";
68
            foreach ($limits as $limit) {
69
                foreach ($limit as $key => $value) {
70
                    $yamlContent .= "        - {$key}: {$value}\n";
71
                }
72
            }
73
        }
74
75
        // Write hosting limits to hosting_limits.yml
76
        file_put_contents($hostingLimitsFile, $yamlContent);
77
    }
78
79
    public function down(Schema $schema): void {}
80
81
    private function getRootPath(): string
82
    {
83
        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

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