Passed
Pull Request — master (#6127)
by Angel Fernando Quiroz
08:00
created

Version20250306101000::insertPluginSettingsByUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
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 AppPlugin;
10
use Chamilo\CoreBundle\Entity\Plugin;
11
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
12
use Doctrine\DBAL\Exception;
13
use Doctrine\DBAL\Schema\Schema;
14
15
final class Version20250306101000 extends AbstractMigrationChamilo
16
{
17
    public function getDescription(): string
18
    {
19
        return 'Migrate data from the settings table to the new plugin table.';
20
    }
21
22
    public function up(Schema $schema): void
23
    {
24
        foreach ($this->getPluginTitles() as $pluginTitle) {
25
            $pluginId = $this->insertPlugin($pluginTitle);
26
27
            $settingsByUrl = $this->getPluginSettingsByUrl($pluginTitle);
28
29
            $this->insertPluginSettingsByUrl($pluginId, $settingsByUrl);
30
        }
31
    }
32
33
    public function down(Schema $schema): void
34
    {
35
    }
36
37
    /**
38
     * @throws Exception
39
     */
40
    private function getPluginTitles(): array
41
    {
42
        return $this->connection
43
            ->executeQuery(
44
                "SELECT title FROM settings
45
                    WHERE category = 'plugins' AND type = 'setting' AND subkey IS NOT NULL AND variable <> 'status'
46
                    GROUP BY subkey"
47
            )
48
            ->fetchAllAssociative()
49
        ;
50
    }
51
52
    /**
53
     * @throws Exception
54
     */
55
    private function insertPlugin(string $pluginTitle): int
56
    {
57
        $pluginSource = in_array($pluginTitle, AppPlugin::getOfficialPlugins())
58
            ? Plugin::SOURCE_OFFICIAL
59
            : Plugin::SOURCE_THIRD_PARTY;
60
61
        $this->connection->insert(
62
            'plugin',
63
            [
64
                'title' => $pluginTitle,
65
                'installed' => 1,
66
                'installed_version' => '1.0.0',
67
                'source' => $pluginSource,
68
            ]
69
        );
70
71
        return $this->connection->lastInsertId();
72
    }
73
74
    /**
75
     * @throws Exception
76
     */
77
    private function getPluginSettingsByUrl(string $pluginTitle): array
78
    {
79
        $settingsByUrl = [];
80
81
        $pluginSettings = $this->connection
82
            ->executeQuery(
83
                "SELECT variable, selected_value, access_url
84
                        FROM settings
85
                        WHERE category = 'plugins'
86
                            AND type = 'setting'
87
                            AND subkey IS NOT NULL
88
                            AND variable <> 'status'
89
                            AND subkey = '$pluginTitle'"
90
            )
91
            ->fetchAllAssociative()
92
        ;
93
94
        foreach ($pluginSettings as $pluginSetting) {
95
            if (!isset($settingsByUrl[$pluginSetting['access_url']])) {
96
                $settingsByUrl[$pluginSetting['access_url']] = [];
97
            }
98
99
            $variable = str_replace($pluginSetting['title'].'_', '', $pluginSetting['variable']);
100
101
            $settingsByUrl[$pluginSetting['access_url']][$variable] = $pluginSetting['selected_value'];
102
        }
103
104
        return $settingsByUrl;
105
    }
106
107
    /**
108
     * @param int $pluginId
109
     * @param array<int, array<string, mixed>> $settingsByUrl
110
     *
111
     * @return void
112
     *
113
     * @throws Exception
114
     */
115
    private function insertPluginSettingsByUrl(int $pluginId, array $settingsByUrl): void
116
    {
117
        foreach ($settingsByUrl as $accessUrlId => $pluginSettings) {
118
            $this->connection->insert(
119
                'access_url_rel_plugin',
120
                [
121
                    'plugin_id' => $pluginId,
122
                    'url_id' => $accessUrlId,
123
                    'active' => 1,
124
                    'configuration' => json_encode($pluginSettings),
125
                ]
126
            );
127
        }
128
    }
129
}
130