Passed
Pull Request — master (#6805)
by
unknown
08:23
created

Version20250924220200::up()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 65
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 51
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 65
rs 9.069

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
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 Version20250924220200 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Ensure default platform settings for global chat: allow_global_chat=false, hide_chat_video=true.';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        $settings = [
22
            [
23
                'name'     => 'allow_global_chat',
24
                'title'    => 'Allow global chat',
25
                'comment'  => 'Users can chat with each other',
26
                'category' => 'chat',
27
                'default'  => 'false',
28
            ],
29
            [
30
                'name'     => 'hide_chat_video',
31
                'title'    => 'Hide videochat option in global chat',
32
                'comment'  => '',
33
                'category' => 'chat',
34
                'default'  => 'true',
35
            ],
36
        ];
37
38
        foreach ($settings as $setting) {
39
            $variable = addslashes($setting['name']);
40
            $title    = addslashes($setting['title']);
41
            $comment  = addslashes($setting['comment']);
42
            $category = addslashes($setting['category']);
43
            $default  = addslashes($setting['default']);
44
45
            $sqlCheck = \sprintf(
46
                "SELECT COUNT(*) AS count
47
                 FROM settings
48
                 WHERE variable = '%s'",
49
                $variable
50
            );
51
52
            $stmt   = $this->connection->executeQuery($sqlCheck);
53
            $result = $stmt->fetchAssociative();
54
55
            if ($result && (int) $result['count'] > 0) {
56
                $this->addSql(\sprintf(
57
                    "UPDATE settings
58
                     SET title = '%s',
59
                         comment = '%s',
60
                         category = '%s',
61
                         type = COALESCE(type, 'radio'),
62
                         selected_value = '%s'
63
                     WHERE variable = '%s'",
64
                    $title,
65
                    $comment,
66
                    $category,
67
                    $default,
68
                    $variable
69
                ));
70
                $this->write(\sprintf('Updated setting: %s (selected_value set to %s)', $setting['name'], $setting['default']));
71
            } else {
72
                $this->addSql(\sprintf(
73
                    "INSERT INTO settings
74
                        (variable, subkey, type, category, selected_value, title, comment, access_url_changeable, access_url_locked, access_url)
75
                     VALUES
76
                        ('%s', NULL, 'radio', '%s', '%s', '%s', '%s', 1, 0, 1)",
77
                    $variable,
78
                    $category,
79
                    $default,
80
                    $title,
81
                    $comment
82
                ));
83
                $this->write(\sprintf('Inserted setting: %s (%s)', $setting['name'], $setting['default']));
84
            }
85
        }
86
    }
87
88
    public function down(Schema $schema): void
89
    {
90
        $variables = ['allow_global_chat', 'hide_chat_video'];
91
92
        foreach ($variables as $variable) {
93
            $this->addSql(\sprintf(
94
                "DELETE FROM settings
95
                 WHERE variable = '%s'
96
                   AND subkey IS NULL
97
                   AND access_url = 1",
98
                addslashes($variable)
99
            ));
100
            $this->write(\sprintf('Removed setting: %s.', $variable));
101
        }
102
    }
103
}
104