Passed
Pull Request — master (#6703)
by
unknown
08:09
created

Version20250905181500   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 67
dl 0
loc 112
rs 10
c 1
b 0
f 0
wmc 29

3 Methods

Rating   Name   Duplication   Size   Complexity  
B down() 0 37 10
D up() 0 64 18
A getDescription() 0 3 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
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 JSON_UNESCAPED_SLASHES;
13
use const JSON_UNESCAPED_UNICODE;
14
15
final class Version20250905181500 extends AbstractMigrationChamilo
16
{
17
    public function getDescription(): string
18
    {
19
        return 'Move legacy tool_enable from configuration JSON to active column and clean configuration.';
20
    }
21
22
    public function up(Schema $schema): void
23
    {
24
        $conn = $this->connection;
25
26
        $rows = $conn->fetchAllAssociative('SELECT id, active, configuration FROM access_url_rel_plugin');
27
28
        foreach ($rows as $row) {
29
            $id     = (int) $row['id'];
30
            $active = isset($row['active']) ? (int) $row['active'] : 0;
31
            $cfgRaw = $row['configuration'];
32
            $cfg    = [];
33
34
            // configuration may be a JSON string (common) or an array (driver dependent)
35
            if (is_string($cfgRaw) && $cfgRaw !== '') {
36
                $decoded = json_decode($cfgRaw, true);
37
                if (is_array($decoded)) {
38
                    $cfg = $decoded;
39
                }
40
            } elseif (is_array($cfgRaw)) {
41
                $cfg = $cfgRaw;
42
            }
43
44
            if (!array_key_exists('tool_enable', $cfg)) {
45
                continue; // nothing to migrate for this row
46
            }
47
48
            $val       = $cfg['tool_enable'];
49
            $newActive = null;
50
51
            // Normalize accepted legacy values
52
            if ($val === true || $val === 1 || $val === '1') {
53
                $newActive = 1;
54
            } elseif ($val === false || $val === 0 || $val === '0') {
55
                $newActive = 0;
56
            } elseif (is_string($val)) {
57
                $v = strtolower(trim($val));
58
                if (in_array($v, ['true', 'on', 'yes', 'y'], true)) {
59
                    $newActive = 1;
60
                } elseif (in_array($v, ['false', 'off', 'no', 'n'], true)) {
61
                    $newActive = 0;
62
                }
63
            }
64
65
            // Remove the legacy key from configuration
66
            unset($cfg['tool_enable']);
67
            $payload = json_encode($cfg, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
68
69
            // Update row: set active if we could infer it; otherwise just clean configuration
70
            if ($newActive !== null) {
71
                $conn->update(
72
                    'access_url_rel_plugin',
73
                    ['active' => $newActive, 'configuration' => $payload],
74
                    ['id' => $id],
75
                    ['configuration' => \PDO::PARAM_STR]
76
                );
77
                $this->write("Row {$id}: moved tool_enable => active={$newActive}; configuration cleaned.");
78
            } else {
79
                $conn->update(
80
                    'access_url_rel_plugin',
81
                    ['configuration' => $payload],
82
                    ['id' => $id],
83
                    ['configuration' => \PDO::PARAM_STR]
84
                );
85
                $this->write("Row {$id}: tool_enable removed from configuration; active left unchanged={$active}.");
86
            }
87
        }
88
    }
89
90
    public function down(Schema $schema): void
91
    {
92
        // Re-introduce configuration.tool_enable from active as 'true'/'false'
93
        $conn = $this->connection;
94
95
        $rows = $conn->fetchAllAssociative('SELECT id, active, configuration FROM access_url_rel_plugin');
96
97
        foreach ($rows as $row) {
98
            $id     = (int) $row['id'];
99
            $active = isset($row['active']) ? (int) $row['active'] : 0;
100
            $cfgRaw = $row['configuration'];
101
            $cfg    = [];
102
103
            if (is_string($cfgRaw) && $cfgRaw !== '') {
104
                $decoded = json_decode($cfgRaw, true);
105
                if (is_array($decoded)) {
106
                    $cfg = $decoded;
107
                }
108
            } elseif (is_array($cfgRaw)) {
109
                $cfg = $cfgRaw;
110
            }
111
112
            // Do not overwrite if it already exists (unlikely after up())
113
            if (!array_key_exists('tool_enable', $cfg)) {
114
                $cfg['tool_enable'] = $active ? 'true' : 'false';
115
            }
116
117
            $payload = json_encode($cfg, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
118
119
            $conn->update(
120
                'access_url_rel_plugin',
121
                ['configuration' => $payload],
122
                ['id' => $id],
123
                ['configuration' => \PDO::PARAM_STR]
124
            );
125
126
            $this->write("Row {$id}: restored configuration.tool_enable='".($active ? 'true' : 'false')."' from active.");
127
        }
128
    }
129
}
130