Passed
Push — master ( d5a14e...f6cd03 )
by
unknown
09:13 queued 16s
created

Version20240806120000   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 66
c 3
b 0
f 0
dl 0
loc 116
rs 10
wmc 25

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
C up() 0 60 13
A down() 0 1 1
A getRootPath() 0 3 1
B updateEnvFiles() 0 40 9
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
            'THEME_FALLBACK' => $_configuration['theme_fallback'] ?? 'chamilo',
41
            'PACKAGER' => $_configuration['packager'] ?? 'chamilo',
42
            'DEFAULT_TEMPLATE' => $_configuration['default_template'] ?? 'default',
43
            'ADMIN_CHAMILO_ANNOUNCEMENTS_DISABLE' => !empty($_configuration['admin_chamilo_announcements_disable']) ? '1' : '0',
44
            'WEBSERVICE_ENABLE' => !empty($_configuration['webservice_enable']) ? '1' : '0',
45
        ]);
46
47
        // Ensure the hosting_limits.yml file exists
48
        $hostingLimitsFile = $rootPath.'/config/hosting_limits.yml';
49
        $hostingLimits = ['hosting_limits' => ['urls' => []]];
50
51
        // Prepare hosting limits
52
        if (\is_array($_configuration)) {
53
            foreach ($_configuration as $key => $config) {
54
                if (is_numeric($key) && \is_array($config)) {
55
                    // Handle configurations specific to URL IDs
56
                    $hostingLimits['hosting_limits']['urls'][$key] = [
57
                        ['hosting_limit_users' => $config['hosting_limit_users'] ?? 0],
58
                        ['hosting_limit_teachers' => $config['hosting_limit_teachers'] ?? 0],
59
                        ['hosting_limit_courses' => $config['hosting_limit_courses'] ?? 0],
60
                        ['hosting_limit_sessions' => $config['hosting_limit_sessions'] ?? 0],
61
                        ['hosting_limit_disk_space' => $config['hosting_limit_disk_space'] ?? 0],
62
                        ['hosting_limit_active_courses' => $config['hosting_limit_active_courses'] ?? 0],
63
                        ['hosting_total_size_limit' => $_configuration['hosting_total_size_limit'] ?? 0],
64
                    ];
65
                }
66
            }
67
        }
68
69
        // Format hosting limits as YAML
70
        $yamlContent = "parameters:\n  hosting_limits:\n    urls:\n";
71
        foreach ($hostingLimits['hosting_limits']['urls'] as $urlId => $limits) {
72
            $yamlContent .= "      {$urlId}:\n";
73
            foreach ($limits as $limit) {
74
                foreach ($limit as $key => $value) {
75
                    $yamlContent .= "        - {$key}: {$value}\n";
76
                }
77
            }
78
        }
79
80
        // Write hosting limits to hosting_limits.yml
81
        file_put_contents($hostingLimitsFile, $yamlContent);
82
    }
83
84
    public function down(Schema $schema): void {}
85
86
    private function getRootPath(): string
87
    {
88
        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

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