Passed
Pull Request — master (#6455)
by
unknown
07:50
created

Version20250709161800::up()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 65
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

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

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\Migrations\AbstractMigrationChamilo;
10
use Doctrine\DBAL\Schema\Schema;
11
12
final class Version20250709161800 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Insert or update admin settings for Chamilo latest news and support block.';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        $settings = [
22
            [
23
                'variable' => 'chamilo_latest_news',
24
                'selected_value' => 'true',
25
                'title' => 'Latest news',
26
                'comment' => 'Get the latest news from Chamilo, including security vulnerabilities and events, directly inside your administration panel. These pieces of news will be checked on the Chamilo news server every time you load the administration page and are only visible to administrators.',
27
                'category' => 'admin',
28
            ],
29
            [
30
                'variable' => 'chamilo_support',
31
                'selected_value' => 'true',
32
                'title' => 'Chamilo support block',
33
                'comment' => 'Get pro tips and an easy way to contact official service providers for professional support, directly from the makers of Chamilo. This block appears on your administration page, is only visible by administrators, and refreshes every time you load the administration page.',
34
                'category' => 'admin',
35
            ],
36
        ];
37
38
        foreach ($settings as $setting) {
39
            // Check if the setting exists for access_url = 1
40
            $sqlCheck = sprintf(
41
                "SELECT COUNT(*) as count
42
                 FROM settings
43
                 WHERE variable = '%s'
44
                   AND subkey IS NULL
45
                   AND access_url = 1",
46
                addslashes($setting['variable'])
47
            );
48
49
            $stmt = $this->connection->executeQuery($sqlCheck);
50
            $result = $stmt->fetchAssociative();
51
52
            if ($result && (int)$result['count'] > 0) {
53
                // UPDATE existing setting
54
                $this->addSql(sprintf(
55
                    "UPDATE settings
56
                     SET selected_value = '%s',
57
                         title = '%s',
58
                         comment = '%s',
59
                         category = '%s'
60
                     WHERE variable = '%s'
61
                       AND subkey IS NULL
62
                       AND access_url = 1",
63
                    addslashes($setting['selected_value']),
64
                    addslashes($setting['title']),
65
                    addslashes($setting['comment']),
66
                    addslashes($setting['category']),
67
                    addslashes($setting['variable'])
68
                ));
69
                $this->write(sprintf("Updated setting: %s", $setting['variable']));
70
            } else {
71
                // INSERT new setting
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, NULL, '%s', '%s', '%s', '%s', 1, 0, 1)",
77
                    addslashes($setting['variable']),
78
                    addslashes($setting['category']),
79
                    addslashes($setting['selected_value']),
80
                    addslashes($setting['title']),
81
                    addslashes($setting['comment'])
82
                ));
83
                $this->write(sprintf("Inserted setting: %s", $setting['variable']));
84
            }
85
        }
86
    }
87
88
    public function down(Schema $schema): void
89
    {
90
        $this->addSql("
91
            DELETE FROM settings
92
             WHERE variable IN ('chamilo_latest_news', 'chamilo_support')
93
               AND subkey IS NULL
94
               AND access_url = 1
95
        ");
96
97
        $this->write("Removed chamilo_latest_news and chamilo_support settings.");
98
    }
99
}
100