Passed
Push — master ( 1f25b2...538c60 )
by
unknown
16:08 queued 07:37
created

Version20250724180700::up()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 191
Code Lines 134

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 134
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 191
rs 7.6888

How to fix   Long Method   

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\DataFixtures\SettingsValueTemplateFixtures;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use Doctrine\DBAL\Schema\Schema;
12
13
final class Version20250724180700 extends AbstractMigrationChamilo
14
{
15
    public function getDescription(): string
16
    {
17
        return 'Insert or update catalog settings and synchronize their JSON templates.';
18
    }
19
20
    public function up(Schema $schema): void
21
    {
22
        // 1) Insert or update catalog settings
23
        $settings = [
24
            [
25
                'variable'       => 'course_catalog_settings',
26
                'selected_value' => '',
27
                'title'          => 'Course Catalog Settings',
28
                'comment'        => 'JSON configuration for course catalog: link settings, filters, sort options, and more.',
29
                'category'       => 'catalog',
30
            ],
31
            [
32
                'variable'       => 'session_catalog_settings',
33
                'selected_value' => '',
34
                'title'          => 'Session Catalog Settings',
35
                'comment'        => 'JSON configuration for session catalog: filters and display options.',
36
                'category'       => 'catalog',
37
            ],
38
            [
39
                'variable'       => 'show_courses_descriptions_in_catalog',
40
                'selected_value' => 'false',
41
                'title'          => 'Show Course Descriptions',
42
                'comment'        => 'Display course descriptions within the catalog listing.',
43
                'category'       => 'catalog',
44
            ],
45
            [
46
                'variable'       => 'course_catalog_published',
47
                'selected_value' => 'false',
48
                'title'          => 'Published Courses Only',
49
                'comment'        => 'Limit the catalog to only courses marked as published.',
50
                'category'       => 'catalog',
51
            ],
52
            [
53
                'variable'       => 'course_catalog_display_in_home',
54
                'selected_value' => 'true',
55
                'title'          => 'Display Catalog on Homepage',
56
                'comment'        => 'Show the course catalog block on the platform homepage.',
57
                'category'       => 'catalog',
58
            ],
59
            [
60
                'variable'       => 'hide_public_link',
61
                'selected_value' => 'false',
62
                'title'          => 'Hide Public Link',
63
                'comment'        => 'Remove the public URL link from course cards.',
64
                'category'       => 'catalog',
65
            ],
66
            [
67
                'variable'       => 'only_show_selected_courses',
68
                'selected_value' => 'false',
69
                'title'          => 'Only Selected Courses',
70
                'comment'        => 'Show only manually selected courses in the catalog.',
71
                'category'       => 'catalog',
72
            ],
73
            [
74
                'variable'       => 'only_show_course_from_selected_category',
75
                'selected_value' => 'false',
76
                'title'          => 'Only Selected Category',
77
                'comment'        => 'Show only courses from a specific selected category.',
78
                'category'       => 'catalog',
79
            ],
80
            [
81
                'variable'       => 'allow_students_to_browse_courses',
82
                'selected_value' => 'true',
83
                'title'          => 'Allow Student Browsing',
84
                'comment'        => 'Permit students to browse and filter the course catalog.',
85
                'category'       => 'catalog',
86
            ],
87
            [
88
                'variable'       => 'course_catalog_hide_private',
89
                'selected_value' => 'true',
90
                'title'          => 'Hide Private Courses',
91
                'comment'        => 'Exclude private courses from the catalog display.',
92
                'category'       => 'catalog',
93
            ],
94
            [
95
                'variable'       => 'show_courses_sessions',
96
                'selected_value' => 'true',
97
                'title'          => 'Show Courses & Sessions',
98
                'comment'        => 'Include both courses and sessions in catalog results.',
99
                'category'       => 'catalog',
100
            ],
101
            [
102
                'variable'       => 'allow_session_auto_subscription',
103
                'selected_value' => 'false',
104
                'title'          => 'Auto Session Subscription',
105
                'comment'        => 'Enable automatic subscription to sessions for users.',
106
                'category'       => 'catalog',
107
            ],
108
            [
109
                'variable'       => 'course_subscription_in_user_s_session',
110
                'selected_value' => 'false',
111
                'title'          => 'Subscription in Session View',
112
                'comment'        => 'Allow users to subscribe to courses directly from their session page.',
113
                'category'       => 'catalog',
114
            ],
115
        ];
116
117
        foreach ($settings as $setting) {
118
            $variable = addslashes($setting['variable']);
119
            $count = (int) $this->connection->fetchOne(
120
                "SELECT COUNT(*) FROM settings
121
                   WHERE variable = '$variable'
122
                     AND subkey IS NULL
123
                     AND access_url = 1"
124
            );
125
126
            if ($count > 0) {
127
                // UPDATE existing setting
128
                $this->addSql(sprintf(
129
                    "UPDATE settings
130
                        SET selected_value = '%s',
131
                            title = '%s',
132
                            comment = '%s',
133
                            category = '%s'
134
                      WHERE variable = '%s'
135
                        AND subkey IS NULL
136
                        AND access_url = 1",
137
                    addslashes($setting['selected_value']),
138
                    addslashes($setting['title']),
139
                    addslashes($setting['comment']),
140
                    addslashes($setting['category']),
141
                    $variable
142
                ));
143
                $this->write("Updated setting: {$variable}");
144
            } else {
145
                // INSERT new setting
146
                $this->addSql(sprintf(
147
                    "INSERT INTO settings
148
                        (variable, subkey, type, category, selected_value, title, comment,
149
                         access_url_changeable, access_url_locked, access_url)
150
                     VALUES
151
                        ('%s', NULL, NULL, '%s', '%s', '%s', '%s', 1, 0, 1)",
152
                    $variable,
153
                    addslashes($setting['category']),
154
                    addslashes($setting['selected_value']),
155
                    addslashes($setting['title']),
156
                    addslashes($setting['comment'])
157
                ));
158
                $this->write("Inserted setting: {$variable}");
159
            }
160
        }
161
162
        // 2) Synchronize JSON templates for catalog settings
163
        $groupedTemplates = SettingsValueTemplateFixtures::getTemplatesGrouped();
164
        $catalogTemplates = $groupedTemplates['catalog'] ?? [];
165
166
        foreach ($catalogTemplates as $templateData) {
167
            $fullVariable = $templateData['variable'];  // e.g. "catalog.course_catalog_settings"
168
            $jsonExample  = json_encode(
169
                $templateData['json_example'],
170
                JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
171
            );
172
173
            // Create or update the template record
174
            $templateId = $this->connection->fetchOne(
175
                'SELECT id FROM settings_value_template WHERE variable = ?',
176
                [$fullVariable]
177
            );
178
179
            if ($templateId) {
180
                $this->connection->executeStatement(
181
                    'UPDATE settings_value_template
182
                        SET json_example = ?, updated_at = NOW()
183
                      WHERE id = ?',
184
                    [$jsonExample, $templateId]
185
                );
186
                $this->write("Updated JSON template for '{$fullVariable}'.");
187
            } else {
188
                $this->connection->executeStatement(
189
                    'INSERT INTO settings_value_template
190
                        (variable, json_example, created_at, updated_at)
191
                     VALUES (?, ?, NOW(), NOW())',
192
                    [$fullVariable, $jsonExample]
193
                );
194
                $templateId = (int)$this->connection->lastInsertId();
195
                $this->write("Inserted JSON template for '{$fullVariable}'.");
196
            }
197
198
            // Strip the "catalog." prefix** to match the settings.variable column
199
            $strippedVar = preg_replace('/^catalog\./', '', $fullVariable);
200
201
            // Link the template to the matching settings rows
202
            $linkedRows = $this->connection->executeStatement(
203
                "UPDATE settings
204
                    SET value_template_id = ?
205
                  WHERE variable = ?
206
                    AND subkey IS NULL
207
                    AND access_url = 1",
208
                [$templateId, $strippedVar]
209
            );
210
            $this->write("Linked {$linkedRows} settings rows to template '{$fullVariable}'.");
211
        }
212
    }
213
214
    public function down(Schema $schema): void
215
    {
216
        // Remove all catalog settings
217
        $this->addSql("
218
            DELETE FROM settings
219
             WHERE variable IN (
220
               'course_catalog_settings',
221
               'session_catalog_settings',
222
               'show_courses_descriptions_in_catalog',
223
               'course_catalog_published',
224
               'course_catalog_display_in_home',
225
               'hide_public_link',
226
               'only_show_selected_courses',
227
               'only_show_course_from_selected_category',
228
               'allow_students_to_browse_courses',
229
               'course_catalog_hide_private',
230
               'show_courses_sessions',
231
               'allow_session_auto_subscription',
232
               'course_subscription_in_user_s_session'
233
             )
234
               AND subkey IS NULL
235
               AND access_url = 1
236
        ");
237
238
        $this->write('Removed all catalog.* settings.');
239
    }
240
}
241