Completed
Push — master ( 997265...54aa45 )
by Yannick
01:44 queued 37s
created

Version20250905073102::up()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 52
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 40
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 52
rs 9.28

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 Version20250905073102 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Insert or update platform setting title/comment for disclose_ai_assistance.';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        $settings = [
22
            [
23
                'name' => 'disclose_ai_assistance',
24
                'title' => 'Disclose AI assistance',
25
                'comment' => 'Show a tag on any content or feedback that has been generated or co-generated by any AI system, evidencing to the user that the content was built with the help of some AI system. Details about which AI system was used in which case are kept inside the database for audit, but are not directly accessible by the final user.',
26
            ],
27
        ];
28
29
        foreach ($settings as $setting) {
30
            $variable = addslashes($setting['name']);
31
            $title = addslashes($setting['title']);
32
            $comment = addslashes($setting['comment']);
33
34
            $sqlCheck = \sprintf(
35
                "SELECT COUNT(*) AS count
36
                 FROM settings
37
                 WHERE variable = '%s'
38
                   AND subkey IS NULL
39
                   AND access_url = 1",
40
                $variable
41
            );
42
43
            $stmt = $this->connection->executeQuery($sqlCheck);
44
            $result = $stmt->fetchAssociative();
45
46
            if ($result && (int) $result['count'] > 0) {
47
                $this->addSql(\sprintf(
48
                    "UPDATE settings
49
                     SET title = '%s',
50
                         comment = '%s',
51
                         category = 'ai_helpers'
52
                     WHERE variable = '%s'
53
                       AND subkey IS NULL
54
                       AND access_url = 1",
55
                    $title,
56
                    $comment,
57
                    $variable
58
                ));
59
                $this->write(\sprintf('Updated setting: %s', $setting['name']));
60
            } else {
61
                $this->addSql(\sprintf(
62
                    "INSERT INTO settings
63
                        (variable, subkey, type, category, selected_value, title, comment, access_url_changeable, access_url_locked, access_url)
64
                     VALUES
65
                        ('%s', NULL, NULL, 'ai_helpers', 'true', '%s', '%s', 1, 0, 1)",
66
                    $variable,
67
                    $title,
68
                    $comment
69
                ));
70
                $this->write(\sprintf('Inserted setting: %s', $setting['name']));
71
            }
72
        }
73
    }
74
75
    public function down(Schema $schema): void
76
    {
77
        // Rollback: remove the setting entry (keeps system functional; defaults can apply)
78
        $variables = [
79
            'disclose_ai_assistance',
80
        ];
81
82
        foreach ($variables as $variable) {
83
            $this->addSql(\sprintf(
84
                "DELETE FROM settings
85
                 WHERE variable = '%s'
86
                   AND subkey IS NULL
87
                   AND access_url = 1",
88
                addslashes($variable)
89
            ));
90
            $this->write(\sprintf('Removed setting: %s.', $variable));
91
        }
92
    }
93
}
94