Passed
Push — master ( c910de...a2879d )
by
unknown
18:31 queued 11:35
created

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

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