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

Version20250905185500::down()   C

Complexity

Conditions 13
Paths 81

Size

Total Lines 75
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 54
c 1
b 0
f 0
nc 81
nop 1
dl 0
loc 75
rs 6.6166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Version20250905185500 extends AbstractMigrationChamilo
16
{
17
    public function getDescription(): string
18
    {
19
        return "Purge all legacy plugin entries from `settings` (category 'plugins'/'Plugins') and provide best-effort down().";
20
    }
21
22
    public function up(Schema $schema): void
23
    {
24
        $conn = $this->connection;
25
26
        // Delete everything under category 'plugins' (case variants included).
27
        $deleted = $conn->executeStatement(
28
            "DELETE FROM settings WHERE category IN ('plugins','Plugins')"
29
        );
30
31
        $this->write("Removed {$deleted} rows from settings with category IN ('plugins','Plugins').");
32
    }
33
34
    public function down(Schema $schema): void
35
    {
36
        $conn = $this->connection;
37
38
        // Fetch all plugin + url configs
39
        $rows = $conn->fetchAllAssociative(
40
            'SELECT p.title AS plugin_title,
41
                    p.installed AS plugin_installed,
42
                    r.url_id AS access_url,
43
                    r.active AS rel_active,
44
                    r.configuration AS cfg
45
               FROM plugin p
46
          LEFT JOIN access_url_rel_plugin r ON r.plugin_id = p.id
47
           ORDER BY p.title, r.url_id'
48
        );
49
50
        $recreated = 0;
51
52
        foreach ($rows as $row) {
53
            $title      = (string) $row['plugin_title'];
54
            $installed  = (int) ($row['plugin_installed'] ?? 0);
55
            $accessUrl  = $row['access_url'] !== null ? (int) $row['access_url'] : 1; // default URL=1 if missing
56
            $cfgRaw     = $row['cfg'];
57
58
            // Restore 'status' row (legacy semantics)
59
            $conn->insert('settings', [
60
                'variable'              => 'status',
61
                'subkey'                => $title,
62
                'type'                  => 'setting',
63
                'category'              => 'plugins',
64
                'selected_value'        => $installed ? 'installed' : 'uninstalled',
65
                'title'                 => $title,
66
                'comment'               => '',
67
                'access_url_changeable' => 1,
68
                'access_url_locked'     => 0,
69
                'access_url'            => $accessUrl,
70
            ]);
71
            $recreated++;
72
73
            // Restore configuration rows from JSON
74
            $cfg = [];
75
            if (is_string($cfgRaw) && $cfgRaw !== '') {
76
                $decoded = json_decode($cfgRaw, true);
77
                if (is_array($decoded)) {
78
                    $cfg = $decoded;
79
                }
80
            } elseif (is_array($cfgRaw)) {
81
                $cfg = $cfgRaw;
82
            }
83
84
            if (!empty($cfg)) {
85
                foreach ($cfg as $key => $val) {
86
                    $variable      = $title . '_' . (string) $key;
87
                    $selectedValue =
88
                        is_bool($val) ? ($val ? 'true' : 'false') :
89
                            (is_scalar($val) ? (string) $val : json_encode($val, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
90
91
                    $conn->insert('settings', [
92
                        'variable'              => $variable,
93
                        'subkey'                => $title,
94
                        'type'                  => 'setting',
95
                        'category'              => 'plugins',
96
                        'selected_value'        => $selectedValue,
97
                        'title'                 => $title,
98
                        'comment'               => '',
99
                        'access_url_changeable' => 1,
100
                        'access_url_locked'     => 0,
101
                        'access_url'            => $accessUrl,
102
                    ]);
103
                    $recreated++;
104
                }
105
            }
106
        }
107
108
        $this->write("Down(): recreated {$recreated} legacy 'plugins' setting rows in `settings`. Non-setting rows were not restored.");
109
    }
110
}
111